@@ -124,6 +124,8 @@ public class JsonMapper
124124 private static readonly IDictionary < Type , ArrayMetadata > array_metadata ;
125125 private static readonly object array_metadata_lock = new Object ( ) ;
126126
127+ private static readonly object custom_table_lock = new Object ( ) ;
128+
127129 private static readonly IDictionary < Type ,
128130 IDictionary < Type , MethodInfo > > conv_ops ;
129131 private static readonly object conv_ops_lock = new Object ( ) ;
@@ -374,18 +376,22 @@ private static object ReadValue(Type inst_type, JsonReader reader)
374376 Type json_type = reader . Value . GetType ( ) ;
375377
376378 if ( value_type . IsAssignableFrom ( json_type ) )
379+ {
377380 return reader . Value ;
381+ }
378382
379383 // If there's a custom importer that fits, use it
380- if ( custom_importers_table . ContainsKey ( json_type ) &&
381- custom_importers_table [ json_type ] . ContainsKey (
382- value_type ) )
384+ lock ( custom_table_lock )
383385 {
386+ if ( custom_importers_table . ContainsKey ( json_type ) &&
387+ custom_importers_table [ json_type ] . ContainsKey (
388+ value_type ) )
389+ {
390+ ImporterFunc importer =
391+ custom_importers_table [ json_type ] [ value_type ] ;
384392
385- ImporterFunc importer =
386- custom_importers_table [ json_type ] [ value_type ] ;
387-
388- return importer ( reader . Value ) ;
393+ return importer ( reader . Value ) ;
394+ }
389395 }
390396
391397 // Maybe there's a base importer that works
@@ -920,12 +926,15 @@ private static void WriteValue(object obj, JsonWriter writer,
920926 Type obj_type = obj . GetType ( ) ;
921927
922928 // See if there's a custom exporter for the object
923- if ( custom_exporters_table . ContainsKey ( obj_type ) )
929+ lock ( custom_table_lock )
924930 {
925- ExporterFunc exporter = custom_exporters_table [ obj_type ] ;
926- exporter ( obj , writer ) ;
931+ if ( custom_exporters_table . ContainsKey ( obj_type ) )
932+ {
933+ ExporterFunc exporter = custom_exporters_table [ obj_type ] ;
934+ exporter ( obj , writer ) ;
927935
928- return ;
936+ return ;
937+ }
929938 }
930939
931940 // If not, maybe there's a base exporter
@@ -1088,7 +1097,10 @@ public static void RegisterExporter<T>(ExporterFunc<T> exporter)
10881097 exporter ( ( T ) obj , writer ) ;
10891098 } ;
10901099
1091- custom_exporters_table [ typeof ( T ) ] = exporter_wrapper ;
1100+ lock ( custom_table_lock )
1101+ {
1102+ custom_exporters_table [ typeof ( T ) ] = exporter_wrapper ;
1103+ }
10921104 }
10931105
10941106 public static void RegisterImporter < TJson , TValue > (
@@ -1100,18 +1112,27 @@ public static void RegisterImporter<TJson, TValue>(
11001112 return importer ( ( TJson ) input ) ;
11011113 } ;
11021114
1103- RegisterImporter ( custom_importers_table , typeof ( TJson ) ,
1104- typeof ( TValue ) , importer_wrapper ) ;
1115+ lock ( custom_table_lock )
1116+ {
1117+ RegisterImporter ( custom_importers_table , typeof ( TJson ) ,
1118+ typeof ( TValue ) , importer_wrapper ) ;
1119+ }
11051120 }
11061121
11071122 public static void UnregisterExporters ( )
11081123 {
1109- custom_exporters_table . Clear ( ) ;
1124+ lock ( custom_table_lock )
1125+ {
1126+ custom_exporters_table . Clear ( ) ;
1127+ }
11101128 }
11111129
11121130 public static void UnregisterImporters ( )
11131131 {
1114- custom_importers_table . Clear ( ) ;
1132+ lock ( custom_table_lock )
1133+ {
1134+ custom_importers_table . Clear ( ) ;
1135+ }
11151136 }
11161137 }
11171138}
0 commit comments