Added Clone(), switched to .NET7

master
Harald Wolff 2023-08-17 11:46:46 +02:00
parent ccd0a507bc
commit f1aca689be
10 changed files with 45 additions and 3 deletions

View File

@ -88,6 +88,7 @@ namespace ln.json.tests
TestEnumSerialization(testEnum3.C); TestEnumSerialization(testEnum3.C);
TestEnumSerialization(testEnum3.D); TestEnumSerialization(testEnum3.D);
TestEnumSerialization(testEnum3.E); TestEnumSerialization(testEnum3.E);
TestEnumSerialization((testEnum3)23);
} }
bool TestEnumSerialization(object enumValue) bool TestEnumSerialization(object enumValue)

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net5.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<IsPackable>false</IsPackable> <IsPackable>false</IsPackable>
</PropertyGroup> </PropertyGroup>

View File

@ -41,6 +41,14 @@ namespace ln.json
public JSONArray Add(JSONValue value){ values.Add(value); return this; } public JSONArray Add(JSONValue value){ values.Add(value); return this; }
public JSONArray Remove(int index) { values.RemoveAt(index); 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() public override string ToString()
{ {

View File

@ -57,6 +57,8 @@ namespace ln.json
{ {
this.decValue = decValue; this.decValue = decValue;
} }
public override JSONValue Clone() => new JSONNumber(this.decValue);
public override string ToString() public override string ToString()
{ {

View File

@ -71,6 +71,14 @@ namespace ln.json
throw new NotSupportedException(); 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() public override string ToString()
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();

View File

@ -5,6 +5,8 @@ namespace ln.json
{ {
public static JSONTrue Instance { get; } = new JSONTrue(); public static JSONTrue Instance { get; } = new JSONTrue();
private JSONTrue() : base(JSONValueType.TRUE) { } private JSONTrue() : base(JSONValueType.TRUE) { }
public override JSONValue Clone() => this;
public override string ToString() => "true"; public override string ToString() => "true";
public override object ToNative() => true; public override object ToNative() => true;
} }
@ -12,6 +14,8 @@ namespace ln.json
{ {
public static JSONFalse Instance { get; } = new JSONFalse(); public static JSONFalse Instance { get; } = new JSONFalse();
private JSONFalse() : base(JSONValueType.FALSE) { } private JSONFalse() : base(JSONValueType.FALSE) { }
public override JSONValue Clone() => this;
public override string ToString() => "false"; public override string ToString() => "false";
public override object ToNative() => false; public override object ToNative() => false;
} }
@ -19,6 +23,8 @@ namespace ln.json
{ {
public static JSONNull Instance { get; } = new JSONNull(); public static JSONNull Instance { get; } = new JSONNull();
private JSONNull() : base(JSONValueType.NULL) { } private JSONNull() : base(JSONValueType.NULL) { }
public override JSONValue Clone() => this;
public override string ToString() => "null"; public override string ToString() => "null";
public override object ToNative() => null; public override object ToNative() => null;
} }

View File

@ -29,6 +29,8 @@ namespace ln.json
Value = value; Value = value;
} }
public override JSONValue Clone() => new JSONString(Value);
public override string ToString() public override string ToString()
{ {
return String.Format("\"{0}\"", Escape(Value)); return String.Format("\"{0}\"", Escape(Value));

View File

@ -44,6 +44,8 @@ namespace ln.json
set => throw new NotSupportedException(); set => throw new NotSupportedException();
} }
public abstract JSONValue Clone();
public override string ToString() => throw new NotImplementedException(); public override string ToString() => throw new NotImplementedException();
} }
} }

View File

@ -9,8 +9,8 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>0.1.0.0</AssemblyVersion> <AssemblyVersion>0.1.0.0</AssemblyVersion>
<FileVersion>0.1.0.0</FileVersion> <FileVersion>0.1.0.0</FileVersion>
<PackageVersion>1.2.4</PackageVersion> <PackageVersion>1.3.0-preview1</PackageVersion>
<TargetFrameworks>net5.0;net6.0</TargetFrameworks> <TargetFramework>net7.0</TargetFramework>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -155,6 +155,12 @@ namespace ln.json.mapping
public virtual bool Serialize(object o, out JSONValue json) public virtual bool Serialize(object o, out JSONValue json)
{ {
if (o is JSONValue jValue)
{
json = jValue.Clone();
return true;
}
lock (mappings) lock (mappings)
{ {
if (object.ReferenceEquals(null, o)) if (object.ReferenceEquals(null, o))
@ -224,6 +230,13 @@ namespace ln.json.mapping
{ {
o = null; o = null;
if (nativeType.IsSubclassOf(typeof(JSONValue)))
{
o = json.Clone();
return true;
}
if (JSONNull.Instance.Equals(json)) if (JSONNull.Instance.Equals(json))
return true; return true;