From ccd0a507bcff40150e75a16bb303cdd7022a657e Mon Sep 17 00:00:00 2001 From: Harald Wolff-Thobaben Date: Tue, 20 Dec 2022 13:33:46 +0100 Subject: [PATCH] Added one more critical section to JSONMapper.Serialize(..) --- ln.json/ln.json.csproj | 2 +- ln.json/mapping/JSONMapper.cs | 79 +++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 38 deletions(-) diff --git a/ln.json/ln.json.csproj b/ln.json/ln.json.csproj index c0b6510..403b8d5 100644 --- a/ln.json/ln.json.csproj +++ b/ln.json/ln.json.csproj @@ -9,7 +9,7 @@ true 0.1.0.0 0.1.0.0 - 1.2.3 + 1.2.4 net5.0;net6.0 diff --git a/ln.json/mapping/JSONMapper.cs b/ln.json/mapping/JSONMapper.cs index e42ea04..08a0824 100644 --- a/ln.json/mapping/JSONMapper.cs +++ b/ln.json/mapping/JSONMapper.cs @@ -155,51 +155,56 @@ namespace ln.json.mapping public virtual bool Serialize(object o, out JSONValue json) { - if (object.ReferenceEquals(null, o)) + lock (mappings) { - json = JSONNull.Instance; - return true; - } - - Type type = o.GetType(); - - if (RequestCustomSerialization(o, out json)) - return true; - - if (TryGetMapping(type, out JSONMapping mapping)) - { - json = mapping.ToJson(this, o); - return true; - } - - if (type.IsEnum) - { - if (type.GetCustomAttribute() != null) + if (object.ReferenceEquals(null, o)) { - Type enumBaseType = type.GetEnumUnderlyingType(); - o = Convert.ChangeType(o, enumBaseType); - ConstructorInfo constructor = typeof(JSONNumber).GetConstructor(new Type[]{ enumBaseType }); - json = (JSONNumber)constructor.Invoke(new object[]{ o }); + json = JSONNull.Instance; + return true; } - else + + Type type = o.GetType(); + + if (RequestCustomSerialization(o, out json)) + return true; + + if (TryGetMapping(type, out JSONMapping mapping)) { - json = new JSONString(Enum.GetName(type, o)); + json = mapping.ToJson(this, o); + return true; } - return true; - } - if (type.IsPrimitive) - { - throw new NotSupportedException(String.Format("JSONMapperBase: Unsupported primitive type found: {0}", type)); - } + if (type.IsEnum) + { + if (type.GetCustomAttribute() != null) + { + Type enumBaseType = type.GetEnumUnderlyingType(); + o = Convert.ChangeType(o, enumBaseType); + ConstructorInfo constructor = typeof(JSONNumber).GetConstructor(new Type[] { enumBaseType }); + json = (JSONNumber)constructor.Invoke(new object[] { o }); + } + else + { + json = new JSONString(Enum.GetName(type, o)); + } - if (TryBuildRememberedMapping(type,out mapping)) - { - json = mapping.ToJson(this, o); - return true; - } + return true; + } - return false; + if (type.IsPrimitive) + { + throw new NotSupportedException( + String.Format("JSONMapperBase: Unsupported primitive type found: {0}", type)); + } + + if (TryBuildRememberedMapping(type, out mapping)) + { + json = mapping.ToJson(this, o); + return true; + } + + return false; + } } public virtual bool Deserialize(string jsonSource, out T v) => Deserialize(JSONParser.Parse(jsonSource), out v);