From f1aca689be5ae0213d2bb0b965e20e31a6c0bafa Mon Sep 17 00:00:00 2001 From: Harald Wolff-Thobaben Date: Thu, 17 Aug 2023 11:46:46 +0200 Subject: [PATCH] Added Clone(), switched to .NET7 --- ln.json.tests/JSONTests.cs | 1 + ln.json.tests/ln.json.tests.csproj | 2 +- ln.json/JSONArray.cs | 8 ++++++++ ln.json/JSONNumber.cs | 2 ++ ln.json/JSONObject.cs | 8 ++++++++ ln.json/JSONSpecial.cs | 6 ++++++ ln.json/JSONString.cs | 2 ++ ln.json/JSONValue.cs | 2 ++ ln.json/ln.json.csproj | 4 ++-- ln.json/mapping/JSONMapper.cs | 13 +++++++++++++ 10 files changed, 45 insertions(+), 3 deletions(-) diff --git a/ln.json.tests/JSONTests.cs b/ln.json.tests/JSONTests.cs index 237f6cd..ed84b5e 100644 --- a/ln.json.tests/JSONTests.cs +++ b/ln.json.tests/JSONTests.cs @@ -88,6 +88,7 @@ namespace ln.json.tests TestEnumSerialization(testEnum3.C); TestEnumSerialization(testEnum3.D); TestEnumSerialization(testEnum3.E); + TestEnumSerialization((testEnum3)23); } bool TestEnumSerialization(object enumValue) diff --git a/ln.json.tests/ln.json.tests.csproj b/ln.json.tests/ln.json.tests.csproj index 9942f18..a60e58a 100644 --- a/ln.json.tests/ln.json.tests.csproj +++ b/ln.json.tests/ln.json.tests.csproj @@ -1,7 +1,7 @@ - net5.0 + net7.0 false diff --git a/ln.json/JSONArray.cs b/ln.json/JSONArray.cs index c98bf37..f4868bf 100644 --- a/ln.json/JSONArray.cs +++ b/ln.json/JSONArray.cs @@ -41,6 +41,14 @@ namespace ln.json public JSONArray Add(JSONValue value){ values.Add(value); return this; } public JSONArray Remove(int index) { values.RemoveAt(index); return this; } + public override JSONValue Clone() + { + JSONArray copy = new JSONArray(); + foreach (var value in values) + copy.Add(value.Clone()); + return copy; + } + public override string ToString() { diff --git a/ln.json/JSONNumber.cs b/ln.json/JSONNumber.cs index 00c8304..e179b78 100644 --- a/ln.json/JSONNumber.cs +++ b/ln.json/JSONNumber.cs @@ -57,6 +57,8 @@ namespace ln.json { this.decValue = decValue; } + + public override JSONValue Clone() => new JSONNumber(this.decValue); public override string ToString() { diff --git a/ln.json/JSONObject.cs b/ln.json/JSONObject.cs index 58f9eb5..c4b8687 100644 --- a/ln.json/JSONObject.cs +++ b/ln.json/JSONObject.cs @@ -71,6 +71,14 @@ namespace ln.json throw new NotSupportedException(); } + public override JSONValue Clone() + { + JSONObject copy = new JSONObject(); + foreach (var key in Keys) + copy[key] = this[key].Clone(); + return copy; + } + public override string ToString() { StringBuilder sb = new StringBuilder(); diff --git a/ln.json/JSONSpecial.cs b/ln.json/JSONSpecial.cs index 6b9ee14..7fde38c 100644 --- a/ln.json/JSONSpecial.cs +++ b/ln.json/JSONSpecial.cs @@ -5,6 +5,8 @@ namespace ln.json { public static JSONTrue Instance { get; } = new JSONTrue(); private JSONTrue() : base(JSONValueType.TRUE) { } + public override JSONValue Clone() => this; + public override string ToString() => "true"; public override object ToNative() => true; } @@ -12,6 +14,8 @@ namespace ln.json { public static JSONFalse Instance { get; } = new JSONFalse(); private JSONFalse() : base(JSONValueType.FALSE) { } + public override JSONValue Clone() => this; + public override string ToString() => "false"; public override object ToNative() => false; } @@ -19,6 +23,8 @@ namespace ln.json { public static JSONNull Instance { get; } = new JSONNull(); private JSONNull() : base(JSONValueType.NULL) { } + public override JSONValue Clone() => this; + public override string ToString() => "null"; public override object ToNative() => null; } diff --git a/ln.json/JSONString.cs b/ln.json/JSONString.cs index 4b4726a..cacadda 100644 --- a/ln.json/JSONString.cs +++ b/ln.json/JSONString.cs @@ -29,6 +29,8 @@ namespace ln.json Value = value; } + public override JSONValue Clone() => new JSONString(Value); + public override string ToString() { return String.Format("\"{0}\"", Escape(Value)); diff --git a/ln.json/JSONValue.cs b/ln.json/JSONValue.cs index a87a423..f564946 100644 --- a/ln.json/JSONValue.cs +++ b/ln.json/JSONValue.cs @@ -44,6 +44,8 @@ namespace ln.json set => throw new NotSupportedException(); } + public abstract JSONValue Clone(); + public override string ToString() => throw new NotImplementedException(); } } diff --git a/ln.json/ln.json.csproj b/ln.json/ln.json.csproj index 403b8d5..fbb179f 100644 --- a/ln.json/ln.json.csproj +++ b/ln.json/ln.json.csproj @@ -9,8 +9,8 @@ true 0.1.0.0 0.1.0.0 - 1.2.4 - net5.0;net6.0 + 1.3.0-preview1 + net7.0 diff --git a/ln.json/mapping/JSONMapper.cs b/ln.json/mapping/JSONMapper.cs index 08a0824..814f829 100644 --- a/ln.json/mapping/JSONMapper.cs +++ b/ln.json/mapping/JSONMapper.cs @@ -155,6 +155,12 @@ namespace ln.json.mapping public virtual bool Serialize(object o, out JSONValue json) { + if (o is JSONValue jValue) + { + json = jValue.Clone(); + return true; + } + lock (mappings) { if (object.ReferenceEquals(null, o)) @@ -224,6 +230,13 @@ namespace ln.json.mapping { o = null; + if (nativeType.IsSubclassOf(typeof(JSONValue))) + { + o = json.Clone(); + return true; + } + + if (JSONNull.Instance.Equals(json)) return true;