LitJSON / litjson

JSON library for the .Net framework
https://litjson.net/
Other
1.37k stars 404 forks source link

ArgumentException: Duplicated Key in JsonMapper.AddObjectMetadata when instances of the same type are serialized to json for the first time in different threads #76

Open Filou1911 opened 6 years ago

Filou1911 commented 6 years ago

Hey,

Given a type T if, lets say 10 threads, create each one an instances of T (or more) when 2 or more threads attempt to serialize their own instances of T. Then a ArgumentException saying "duplicated key" is thrown.

Why: when the two thread serialize their instances of T, since none instances of T was serialized before, the method private static ObjectMetadata AddObjectMetadata ( Type type ) is called. The condition if ( JsonMapper.objectMetadata.ContainsKey( type ) ) will evaluate to false in both threads but at the end of the method: objectMetadata.Add( type, data ); one of the two executions (or more) of that line will throw

Do you plan to add thread safety to LitJson or should we take precausions about this in our programs ?

Thanks you :)

devlead commented 6 years ago

@Filou1911 I think making it thread safe in LitJson would make most sense.