Added one more critical section to JSONMapper.Serialize(..)

master
Harald Wolff 2022-12-20 13:33:46 +01:00
parent 3677c6847b
commit ccd0a507bc
2 changed files with 43 additions and 38 deletions

View File

@ -9,7 +9,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>0.1.0.0</AssemblyVersion>
<FileVersion>0.1.0.0</FileVersion>
<PackageVersion>1.2.3</PackageVersion>
<PackageVersion>1.2.4</PackageVersion>
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
</PropertyGroup>

View File

@ -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<FlagsAttribute>() != 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<FlagsAttribute>() != 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<T>(string jsonSource, out T v) => Deserialize(JSONParser.Parse(jsonSource), out v);