From cf3952a9bafb48371311bd14155afbdd0e1c7b26 Mon Sep 17 00:00:00 2001 From: haraldwolff Date: Sat, 9 Sep 2023 01:52:27 +0200 Subject: [PATCH] Add JSONObjectMapping.Assign() --- ln.json/JSONValue.cs | 15 +++++++++++++++ ln.json/attributes/DontAssign.cs | 7 +++++++ ln.json/ln.json.csproj | 2 +- ln.json/mapping/JSONObjectMapping.cs | 20 ++++++++++++++++++++ 4 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 ln.json/attributes/DontAssign.cs diff --git a/ln.json/JSONValue.cs b/ln.json/JSONValue.cs index bbb414c..a8277cb 100644 --- a/ln.json/JSONValue.cs +++ b/ln.json/JSONValue.cs @@ -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); diff --git a/ln.json/attributes/DontAssign.cs b/ln.json/attributes/DontAssign.cs new file mode 100644 index 0000000..17cbb43 --- /dev/null +++ b/ln.json/attributes/DontAssign.cs @@ -0,0 +1,7 @@ +using System; + +namespace ln.json.attributes; + +public class DontAssign : Attribute +{ +} \ No newline at end of file diff --git a/ln.json/ln.json.csproj b/ln.json/ln.json.csproj index 09684e6..ebe7123 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.3.0-preview4 + 1.3.0-preview7 net7.0 diff --git a/ln.json/mapping/JSONObjectMapping.cs b/ln.json/mapping/JSONObjectMapping.cs index bba0443..dfa5c3a 100644 --- a/ln.json/mapping/JSONObjectMapping.cs +++ b/ln.json/mapping/JSONObjectMapping.cs @@ -25,6 +25,7 @@ namespace ln.json.mapping Dictionary> setters = new Dictionary>(); Dictionary> getters = new Dictionary>(); Dictionary types = new Dictionary(); + private HashSet _dontAssign = new HashSet(); 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() != 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() != 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; + } } }