Add JSONObjectMapping.Assign()

master
Harald Wolff 2023-09-09 01:52:27 +02:00
parent fb6d4fdede
commit cf3952a9ba
4 changed files with 43 additions and 1 deletions

View File

@ -10,6 +10,10 @@
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Text;
using System.Text.Json;
using ln.type;
namespace ln.json
{
@ -48,6 +52,17 @@ namespace ln.json
public override string ToString() => throw new NotImplementedException();
public virtual void WriteTo(string filename)
{
using (var fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
{
WriteTo(fs);
}
}
public virtual void WriteTo(Stream stream)
{
stream.Write(Encoding.UTF8.GetBytes(this.ToString()!));
}
public static implicit operator JSONValue(string v) => new JSONString(v);
public static implicit operator JSONValue(float v) => new JSONNumber(v);

View File

@ -0,0 +1,7 @@
using System;
namespace ln.json.attributes;
public class DontAssign : Attribute
{
}

View File

@ -9,7 +9,7 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>0.1.0.0</AssemblyVersion>
<FileVersion>0.1.0.0</FileVersion>
<PackageVersion>1.3.0-preview4</PackageVersion>
<PackageVersion>1.3.0-preview7</PackageVersion>
<TargetFramework>net7.0</TargetFramework>
</PropertyGroup>

View File

@ -25,6 +25,7 @@ namespace ln.json.mapping
Dictionary<string, Action<object,object>> setters = new Dictionary<string, Action<object,object>>();
Dictionary<string, Func<object, object>> getters = new Dictionary<string, Func<object, object>>();
Dictionary<string, Type> types = new Dictionary<string, Type>();
private HashSet<string> _dontAssign = new HashSet<string>();
public JSONObjectMapping(Type type)
: this(type, JSONObjectMappingFlags.FIELDS | JSONObjectMappingFlags.PROPERTIES, BindingFlags.Instance | BindingFlags.Public)
@ -44,6 +45,8 @@ namespace ln.json.mapping
setters.Add(fieldInfo.Name, (object arg1, object arg2) => fieldInfo.SetValue(arg1, arg2));
getters.Add(fieldInfo.Name, (object arg) => fieldInfo.GetValue(arg));
types.Add(fieldInfo.Name, fieldInfo.FieldType);
if (fieldInfo.GetCustomAttribute<DontAssign>() != null)
_dontAssign.Add(fieldInfo.Name);
}
}
if ((mappingFlags & JSONObjectMappingFlags.PROPERTIES) == JSONObjectMappingFlags.PROPERTIES)
@ -55,6 +58,8 @@ namespace ln.json.mapping
setters.Add(propertyInfo.Name, (object arg1, object arg2) => { if (propertyInfo.CanWrite) propertyInfo.SetValue(arg1, arg2); });
getters.Add(propertyInfo.Name, (object arg) => propertyInfo.GetValue(arg));
types.Add(propertyInfo.Name, propertyInfo.PropertyType);
if (propertyInfo.GetCustomAttribute<DontAssign>() != null)
_dontAssign.Add(propertyInfo.Name);
}
}
}
@ -102,5 +107,20 @@ namespace ln.json.mapping
}
}
}
public bool Assign(JSONMapper mapper, object o, JSONObject json)
{
bool applied = false;
foreach (string name in setters.Keys)
{
if (json.ContainsKey(name) && !_dontAssign.Contains(name))
{
setters[name](o, mapper.FromJson(json[name], types[name]));
applied = true;
}
}
return applied;
}
}
}