ArgumentException: Duplicated Key in JsonMapper.AddObjectMetadata when instances of the same type are serialized to json for the first time in different threads #76
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 ?
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 throwDo you plan to add thread safety to LitJson or should we take precausions about this in our programs ?
Thanks you :)