{"record":{"id":"f21091b0bdb21b89","repo":"egametang/ET","slug":"serialization-mismatchedcount","errorCode":null,"errorMessage":"Serialization_MismatchedCount","messagePattern":"Serialization_MismatchedCount","errorType":"exception","errorClass":"SerializationException","httpStatus":null,"severity":"error","filePath":"Packages/cn.etetet.core/Scripts/Core/Share/Collection/SortedSet.cs","lineNumber":1735,"sourceCode":"            if (savedCount != 0)\n            {\n                T[] items = (T[])siInfo.GetValue(ItemsName, typeof(T[]));\n\n                if (items == null)\n                {\n                    throw new SerializationException(SR.Serialization_MissingValues);\n                }\n\n                for (int i = 0; i < items.Length; i++)\n                {\n                    Add(items[i]);\n                }\n            }\n\n            version = siInfo.GetInt32(VersionName);\n            if (count != savedCount)\n            {\n                throw new SerializationException(SR.Serialization_MismatchedCount);\n            }\n\n            siInfo = null;\n        }\n\n        #endregion\n\n        #region Helper classes\n\n        public sealed class Node\n        {\n            public Node(T item, NodeColor color)\n            {\n                Item = item;\n                Color = color;\n            }\n\n            private Node()","sourceCodeStart":1717,"sourceCodeEnd":1753,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.core/Scripts/Core/Share/Collection/SortedSet.cs#L1717-L1753","documentation":"Thrown at the end of OnDeserialization after all items are re-Added: the live count does not match CountName from the stream. Because SortedSet deduplicates via Add, a mismatch means duplicate items were stored (impossible for a normal set) or the stream's count is wrong. Surfaces as a SerializationException (Serialization_MismatchedCount).","triggerScenarios":"Serialized Items array contained duplicates and the comparer collapsed them (so count shrank); serialized Count was hand-set wrong; a non-strict comparer that treats distinct references as equal during rehydration.","commonSituations":"Data written by a buggy serializer that included duplicates; switching to an IEqualityComparer/IComparer that merges previously-distinct keys; partial re-serialization that updated Items but not Count.","solutions":["Regenerate the persisted data from a canonical SortedSet so Count and Items stay consistent.","Verify the comparer used at deserialize time matches the one used at serialize time.","If you must accept untrusted streams, deserialize into a List and rebuild the set rather than trusting the stored count."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// When authoring serialization, ensure Items has no duplicates and Count == distinct count:\nT[] arr = set.ToArray();\ninfo.AddValue(CountName, arr.Length);\ninfo.AddValue(ItemsName, arr);","typeGuard":null,"tryCatchPattern":"try { /* deserialize */ } catch (SerializationException ex) when (ex.Message.Contains(\"Serialization_MismatchedCount\")) { /* rebuild set from raw items, ignoring stored count */ }","preventionTips":["Keep the comparer identical across serialize and deserialize.","Never store duplicate items; SortedSet semantics forbid them.","Re-derive Count from Items in custom serialization."],"tags":["sortedset","serialization","data-integrity","csharp"],"backgroundTag":null,"analyzedSha":"5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f","analyzedAt":"2026-08-13T21:10:40.377Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}