mgholam / fastJSON

Smallest, fastest polymorphic JSON serializer
https://www.codeproject.com/Articles/159450/fastJSON-Smallest-Fastest-Polymorphic-JSON-Seriali
MIT License
478 stars 148 forks source link

Circular Reference Index doesn't match when deserializing #144

Closed frankinstien111 closed 8 months ago

frankinstien111 commented 8 months ago

When an object is serialized it gets an index if the object is referenced to an identical instance. But when it deserializes the indexes don't match in the _cirrev dictionary. Example:

"GeneralizedType": { "$type": "6", "IsComposite": false, "Parent": { "$i": 27 }, "Key": "organic", "Numeral": 10, "Description": " ", "PofSp": "NN" }

When the $i is looked up it's pointing at the wrong data type, the actual data type index when deserializing is 25

Do you have any idea why that could happen?

Appreciate any help, thx.

mgholam commented 8 months ago

The object lookup in the dictionary is by the hash returned by .net object, it probably to do with that.

frankinstien111 commented 8 months ago

But this is how the look-up is built in the ParseDictionary, it uses a counter to create the index:

` if (d.TryGetValue("$i", out tn)) { object v = null; _cirrev.TryGetValue((int)(long)tn, out v); return v; }

if (_circobj.TryGetValue(o, out circount) == false) { circount = _circobj.Count + 1; _circobj.Add(o, circount); _cirrev.Add(circount, o);
} `

frankinstien111 commented 8 months ago

I was able to set up an environment where I'm not multi-threading and test just one root object serialization and deserialization after being modified with a complex nested class and it works. So, I will continue to troubleshoot this on my end of the code.

thx for your suggestions.

frankinstien111 commented 8 months ago

I take it back the serialization did not work in the non-threaded environment, sorry for the confusion.

frankinstien111 commented 8 months ago

Turns out you were right. I changed the overridden Gethashcode to use the base Gethashcode if the fields in the class were not initialized.