Compare commits
No commits in common. "master" and "master" have entirely different histories.
|
@ -1,46 +1,41 @@
|
||||||
# Autosave files
|
# Autosave files
|
||||||
*~
|
*~
|
||||||
|
|
||||||
# build
|
# build
|
||||||
[Oo]bj/
|
[Oo]bj/
|
||||||
[Bb]in/
|
[Bb]in/
|
||||||
packages/
|
packages/
|
||||||
TestResults/
|
TestResults/
|
||||||
|
|
||||||
# globs
|
# globs
|
||||||
Makefile.in
|
Makefile.in
|
||||||
*.DS_Store
|
*.DS_Store
|
||||||
*.sln.cache
|
*.sln.cache
|
||||||
*.suo
|
*.suo
|
||||||
*.cache
|
*.cache
|
||||||
*.pidb
|
*.pidb
|
||||||
*.userprefs
|
*.userprefs
|
||||||
*.usertasks
|
*.usertasks
|
||||||
config.log
|
config.log
|
||||||
config.make
|
config.make
|
||||||
config.status
|
config.status
|
||||||
aclocal.m4
|
aclocal.m4
|
||||||
install-sh
|
install-sh
|
||||||
autom4te.cache/
|
autom4te.cache/
|
||||||
*.user
|
*.user
|
||||||
*.tar.gz
|
*.tar.gz
|
||||||
tarballs/
|
tarballs/
|
||||||
test-results/
|
test-results/
|
||||||
Thumbs.db
|
Thumbs.db
|
||||||
.vs/
|
.vs/
|
||||||
|
|
||||||
# Mac bundle stuff
|
# Mac bundle stuff
|
||||||
*.dmg
|
*.dmg
|
||||||
*.app
|
*.app
|
||||||
|
|
||||||
# resharper
|
# resharper
|
||||||
*_Resharper.*
|
*_Resharper.*
|
||||||
*.Resharper
|
*.Resharper
|
||||||
|
|
||||||
# dotCover
|
# dotCover
|
||||||
*.dotCover
|
*.dotCover
|
||||||
|
|
||||||
*.log
|
|
||||||
*.log.old
|
|
||||||
.vscode
|
|
||||||
.build
|
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
# Default ignored files
|
|
||||||
/shelf/
|
|
||||||
/workspace.xml
|
|
||||||
# Rider ignored files
|
|
||||||
/.idea.ln.json.iml
|
|
||||||
/modules.xml
|
|
||||||
/projectSettingsUpdater.xml
|
|
||||||
/contentModel.xml
|
|
||||||
# Datasource local storage ignored files
|
|
||||||
/dataSources/
|
|
||||||
/dataSources.local.xml
|
|
||||||
# Editor-based HTTP Client requests
|
|
||||||
/httpRequests/
|
|
|
@ -1,4 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
|
|
||||||
</project>
|
|
|
@ -1,8 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="UserContentModel">
|
|
||||||
<attachedFolders />
|
|
||||||
<explicitIncludes />
|
|
||||||
<explicitExcludes />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
|
@ -1,6 +0,0 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
|
||||||
<project version="4">
|
|
||||||
<component name="VcsDirectoryMappings">
|
|
||||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
|
||||||
</component>
|
|
||||||
</project>
|
|
|
@ -32,35 +32,14 @@ namespace ln.json
|
||||||
public JSONArray()
|
public JSONArray()
|
||||||
: base(JSONValueType.ARRAY) { }
|
: base(JSONValueType.ARRAY) { }
|
||||||
|
|
||||||
public JSONArray(IEnumerable<JSONValue> values)
|
|
||||||
: base(JSONValueType.ARRAY)
|
|
||||||
{
|
|
||||||
this.values.AddRange(values);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static JSONArray From<T>(IEnumerable<T> values) where T:JSONValue
|
|
||||||
{
|
|
||||||
return new JSONArray(values);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public override JSONValue this[int index] {
|
public override JSONValue this[int index] {
|
||||||
get => values[index];
|
get => values[index];
|
||||||
set => values[index] = value;
|
set => values[index] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JSONArray Add(IEnumerable<JSONValue> values){ foreach (JSONValue value in values) this.values.Add(value); return this; }
|
|
||||||
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()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -82,14 +61,5 @@ namespace ln.json
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<OT> GetValues<OT>()
|
|
||||||
{
|
|
||||||
foreach (var value in values)
|
|
||||||
{
|
|
||||||
if (value is OT otValue)
|
|
||||||
yield return otValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -20,14 +20,6 @@ namespace ln.json
|
||||||
return (double)decValue;
|
return (double)decValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public JSONNumber(byte i)
|
|
||||||
: this((long)i) { }
|
|
||||||
public JSONNumber(sbyte i)
|
|
||||||
: this((long)i) { }
|
|
||||||
public JSONNumber(short i)
|
|
||||||
: this((long)i) { }
|
|
||||||
public JSONNumber(ushort i)
|
|
||||||
: this((long)i) { }
|
|
||||||
public JSONNumber(int i)
|
public JSONNumber(int i)
|
||||||
: this((long)i) { }
|
: this((long)i) { }
|
||||||
|
|
||||||
|
@ -49,7 +41,6 @@ namespace ln.json
|
||||||
public JSONNumber(double doubleValue)
|
public JSONNumber(double doubleValue)
|
||||||
: base(JSONValueType.NUMBER)
|
: base(JSONValueType.NUMBER)
|
||||||
{
|
{
|
||||||
|
|
||||||
decValue = doubleValue.ToDecimal();
|
decValue = doubleValue.ToDecimal();
|
||||||
}
|
}
|
||||||
public JSONNumber(decimal decValue)
|
public JSONNumber(decimal decValue)
|
||||||
|
@ -57,21 +48,10 @@ 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()
|
||||||
{
|
{
|
||||||
return decValue.ToString(CultureInfo.InvariantCulture);
|
return decValue.ToString(CultureInfo.InvariantCulture);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static implicit operator Decimal(JSONNumber j) => j.decValue;
|
|
||||||
public static implicit operator float(JSONNumber j) => (float)j.decValue;
|
|
||||||
public static implicit operator double(JSONNumber j) => (double)j.decValue;
|
|
||||||
public static implicit operator int(JSONNumber j) => (int)j.decValue;
|
|
||||||
public static implicit operator long(JSONNumber j) => (long)j.decValue;
|
|
||||||
public static implicit operator uint(JSONNumber j) => (uint)j.decValue;
|
|
||||||
public static implicit operator ulong(JSONNumber j) => (ulong)j.decValue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,7 +1,6 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Dynamic;
|
|
||||||
using ln.collections;
|
using ln.collections;
|
||||||
using ln.json.mapping;
|
using ln.json.mapping;
|
||||||
|
|
||||||
|
@ -41,27 +40,11 @@ namespace ln.json
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Remove(string propertyName) => values.Remove(propertyName);
|
|
||||||
|
|
||||||
public bool ContainsKey(string key)
|
public bool ContainsKey(string key)
|
||||||
{
|
{
|
||||||
return values.ContainsKey(key);
|
return values.ContainsKey(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool TryGetValue(string key, out JSONValue jsonValue) => values.TryGet(key, out jsonValue);
|
|
||||||
|
|
||||||
public bool TryGetValue<OT>(string key, out OT value) where OT : JSONValue
|
|
||||||
{
|
|
||||||
if (TryGetValue(key, out JSONValue jsonValue) && (jsonValue is OT otValue))
|
|
||||||
{
|
|
||||||
value = otValue;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
value = default;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public T ToObject<T>()
|
public T ToObject<T>()
|
||||||
{
|
{
|
||||||
if (JSONMapper.DefaultMapper.Deserialize(this, typeof(T), out object o))
|
if (JSONMapper.DefaultMapper.Deserialize(this, typeof(T), out object o))
|
||||||
|
@ -71,14 +54,6 @@ 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();
|
||||||
|
@ -103,28 +78,6 @@ namespace ln.json
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool TryGetMember(GetMemberBinder binder, out object? result)
|
|
||||||
{
|
|
||||||
if (values.TryGet(binder.Name, out JSONValue jsonValue))
|
|
||||||
{
|
|
||||||
result = jsonValue;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
result = null;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override bool TrySetMember(SetMemberBinder binder, object? value)
|
|
||||||
{
|
|
||||||
if (value is JSONValue jsonValue)
|
|
||||||
values[binder.Name] = jsonValue;
|
|
||||||
else
|
|
||||||
values[binder.Name] = JSONMapper.DefaultMapper.ToJson(value);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public static JSONObject From(object value)
|
public static JSONObject From(object value)
|
||||||
{
|
{
|
||||||
if (JSONMapper.DefaultMapper.Serialize(value, out JSONValue json))
|
if (JSONMapper.DefaultMapper.Serialize(value, out JSONValue json))
|
|
@ -184,8 +184,6 @@ namespace ln.json
|
||||||
JSONArray array = new JSONArray();
|
JSONArray array = new JSONArray();
|
||||||
|
|
||||||
reader.Read();
|
reader.Read();
|
||||||
SkipWhitespace(reader);
|
|
||||||
|
|
||||||
while (reader.Peek() != ']')
|
while (reader.Peek() != ']')
|
||||||
{
|
{
|
||||||
array.Add(ParseValue(reader));
|
array.Add(ParseValue(reader));
|
|
@ -5,8 +5,6 @@ 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;
|
||||||
}
|
}
|
||||||
|
@ -14,8 +12,6 @@ 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;
|
||||||
}
|
}
|
||||||
|
@ -23,8 +19,6 @@ 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;
|
||||||
}
|
}
|
|
@ -29,8 +29,6 @@ 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));
|
||||||
|
@ -97,7 +95,5 @@ namespace ln.json
|
||||||
}
|
}
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static implicit operator String(JSONString j) => j.Value;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -9,19 +9,13 @@
|
||||||
// **/
|
// **/
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Dynamic;
|
|
||||||
using System.IO;
|
|
||||||
using System.Text;
|
|
||||||
using System.Text.Json;
|
|
||||||
using ln.type;
|
|
||||||
|
|
||||||
namespace ln.json
|
namespace ln.json
|
||||||
{
|
{
|
||||||
public enum JSONValueType
|
public enum JSONValueType
|
||||||
{
|
{
|
||||||
NULL, OBJECT, ARRAY, STRING, NUMBER, TRUE, FALSE
|
NULL, OBJECT, ARRAY, STRING, NUMBER, TRUE, FALSE
|
||||||
}
|
}
|
||||||
public abstract class JSONValue : DynamicObject
|
public abstract class JSONValue
|
||||||
{
|
{
|
||||||
public JSONValueType ValueType { get; private set; }
|
public JSONValueType ValueType { get; private set; }
|
||||||
|
|
||||||
|
@ -30,8 +24,6 @@ namespace ln.json
|
||||||
|
|
||||||
public virtual object ToNative() => throw new NotImplementedException();
|
public virtual object ToNative() => throw new NotImplementedException();
|
||||||
|
|
||||||
public T As<T>() where T : JSONValue => this as T;
|
|
||||||
|
|
||||||
public JSONValue(JSONValueType valueType)
|
public JSONValue(JSONValueType valueType)
|
||||||
{
|
{
|
||||||
ValueType = valueType;
|
ValueType = valueType;
|
||||||
|
@ -48,30 +40,6 @@ 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();
|
||||||
|
|
||||||
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);
|
|
||||||
public static implicit operator JSONValue(double v) => new JSONNumber(v);
|
|
||||||
public static implicit operator JSONValue(decimal v) => new JSONNumber(v);
|
|
||||||
public static implicit operator JSONValue(int v) => new JSONNumber(v);
|
|
||||||
public static implicit operator JSONValue(long v) => new JSONNumber(v);
|
|
||||||
public static implicit operator JSONValue(uint v) => new JSONNumber(v);
|
|
||||||
public static implicit operator JSONValue(ulong v) => new JSONNumber(v);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
17
build.ln
17
build.ln
|
@ -1,17 +0,0 @@
|
||||||
{
|
|
||||||
"templates": [
|
|
||||||
"dotnet"
|
|
||||||
],
|
|
||||||
"env": {
|
|
||||||
"NUGET_SOURCE": "https://nexus.l--n.de/repository/ln.net/",
|
|
||||||
"CONFIGURATION": "Release"
|
|
||||||
},
|
|
||||||
"stages": [
|
|
||||||
{
|
|
||||||
"name": "prepare",
|
|
||||||
"commands": [
|
|
||||||
"dotnet prepare */*.csproj"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
|
@ -0,0 +1,15 @@
|
||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\ln.collections\ln.collections.csproj" />
|
||||||
|
<ProjectReference Include="..\ln.type\ln.type.csproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="System.Runtime" Version="4.3.1" />
|
||||||
|
</ItemGroup>
|
||||||
|
</Project>
|
48
ln.json.sln
48
ln.json.sln
|
@ -1,48 +0,0 @@
|
||||||
|
|
||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
|
||||||
# Visual Studio 15
|
|
||||||
VisualStudioVersion = 15.0.26124.0
|
|
||||||
MinimumVisualStudioVersion = 15.0.26124.0
|
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ln.json", "ln.json\ln.json.csproj", "{E756C797-C72B-45C0-8DB2-3594CE3C00A6}"
|
|
||||||
EndProject
|
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ln.json.tests", "ln.json.tests\ln.json.tests.csproj", "{0F450BF8-225C-4230-AF01-0E628A00E986}"
|
|
||||||
EndProject
|
|
||||||
Global
|
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
|
||||||
Debug|Any CPU = Debug|Any CPU
|
|
||||||
Debug|x64 = Debug|x64
|
|
||||||
Debug|x86 = Debug|x86
|
|
||||||
Release|Any CPU = Release|Any CPU
|
|
||||||
Release|x64 = Release|x64
|
|
||||||
Release|x86 = Release|x86
|
|
||||||
EndGlobalSection
|
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
|
||||||
HideSolutionNode = FALSE
|
|
||||||
EndGlobalSection
|
|
||||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
|
||||||
{E756C797-C72B-45C0-8DB2-3594CE3C00A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
|
||||||
{E756C797-C72B-45C0-8DB2-3594CE3C00A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
|
||||||
{E756C797-C72B-45C0-8DB2-3594CE3C00A6}.Debug|x64.ActiveCfg = Debug|Any CPU
|
|
||||||
{E756C797-C72B-45C0-8DB2-3594CE3C00A6}.Debug|x64.Build.0 = Debug|Any CPU
|
|
||||||
{E756C797-C72B-45C0-8DB2-3594CE3C00A6}.Debug|x86.ActiveCfg = Debug|Any CPU
|
|
||||||
{E756C797-C72B-45C0-8DB2-3594CE3C00A6}.Debug|x86.Build.0 = Debug|Any CPU
|
|
||||||
{E756C797-C72B-45C0-8DB2-3594CE3C00A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
|
||||||
{E756C797-C72B-45C0-8DB2-3594CE3C00A6}.Release|Any CPU.Build.0 = Release|Any CPU
|
|
||||||
{E756C797-C72B-45C0-8DB2-3594CE3C00A6}.Release|x64.ActiveCfg = Release|Any CPU
|
|
||||||
{E756C797-C72B-45C0-8DB2-3594CE3C00A6}.Release|x64.Build.0 = Release|Any CPU
|
|
||||||
{E756C797-C72B-45C0-8DB2-3594CE3C00A6}.Release|x86.ActiveCfg = Release|Any CPU
|
|
||||||
{E756C797-C72B-45C0-8DB2-3594CE3C00A6}.Release|x86.Build.0 = Release|Any CPU
|
|
||||||
{0F450BF8-225C-4230-AF01-0E628A00E986}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
|
||||||
{0F450BF8-225C-4230-AF01-0E628A00E986}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
|
||||||
{0F450BF8-225C-4230-AF01-0E628A00E986}.Debug|x64.ActiveCfg = Debug|Any CPU
|
|
||||||
{0F450BF8-225C-4230-AF01-0E628A00E986}.Debug|x64.Build.0 = Debug|Any CPU
|
|
||||||
{0F450BF8-225C-4230-AF01-0E628A00E986}.Debug|x86.ActiveCfg = Debug|Any CPU
|
|
||||||
{0F450BF8-225C-4230-AF01-0E628A00E986}.Debug|x86.Build.0 = Debug|Any CPU
|
|
||||||
{0F450BF8-225C-4230-AF01-0E628A00E986}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
|
||||||
{0F450BF8-225C-4230-AF01-0E628A00E986}.Release|Any CPU.Build.0 = Release|Any CPU
|
|
||||||
{0F450BF8-225C-4230-AF01-0E628A00E986}.Release|x64.ActiveCfg = Release|Any CPU
|
|
||||||
{0F450BF8-225C-4230-AF01-0E628A00E986}.Release|x64.Build.0 = Release|Any CPU
|
|
||||||
{0F450BF8-225C-4230-AF01-0E628A00E986}.Release|x86.ActiveCfg = Release|Any CPU
|
|
||||||
{0F450BF8-225C-4230-AF01-0E628A00E986}.Release|x86.Build.0 = Release|Any CPU
|
|
||||||
EndGlobalSection
|
|
||||||
EndGlobal
|
|
|
@ -1,210 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Reflection;
|
|
||||||
using ln.json.mapping;
|
|
||||||
using NUnit.Framework;
|
|
||||||
|
|
||||||
namespace ln.json.tests
|
|
||||||
{
|
|
||||||
public class JSONTests
|
|
||||||
{
|
|
||||||
|
|
||||||
enum testEnum1 { A, B, C, D, E, F }
|
|
||||||
enum testEnum2 : ushort {
|
|
||||||
NONE = 0,
|
|
||||||
A = 1,
|
|
||||||
B = 2,
|
|
||||||
C = 4,
|
|
||||||
D = 5,
|
|
||||||
E = 6
|
|
||||||
}
|
|
||||||
[Flags]
|
|
||||||
enum testEnum3 : ushort {
|
|
||||||
NONE = 0,
|
|
||||||
A = 1,
|
|
||||||
B = 2,
|
|
||||||
C = 4,
|
|
||||||
D = 5,
|
|
||||||
E = 6
|
|
||||||
}
|
|
||||||
|
|
||||||
[SetUp]
|
|
||||||
public void Setup()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void Test_00_Primitives()
|
|
||||||
{
|
|
||||||
JSONValue jsonFloat = JSONParser.Parse("1234.564");
|
|
||||||
|
|
||||||
Assert.IsTrue(jsonFloat is JSONNumber);
|
|
||||||
Assert.AreEqual(1234.564, jsonFloat.ToNative());
|
|
||||||
|
|
||||||
JSONValue jsonInteger = JSONParser.Parse("635462");
|
|
||||||
|
|
||||||
Assert.IsTrue(jsonFloat is JSONNumber);
|
|
||||||
Assert.AreEqual(635462, jsonInteger.ToNative());
|
|
||||||
|
|
||||||
|
|
||||||
Assert.Pass();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void TestArrayParser()
|
|
||||||
{
|
|
||||||
JSONParser.Parse("[]");
|
|
||||||
JSONParser.Parse("[ ]");
|
|
||||||
JSONParser.Parse("[\t]");
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void TestObjectParser()
|
|
||||||
{
|
|
||||||
JSONParser.Parse("{}");
|
|
||||||
JSONParser.Parse("{ }");
|
|
||||||
JSONParser.Parse("{\t}");
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void TestEnumMappings()
|
|
||||||
{
|
|
||||||
TestEnumSerialization(testEnum1.A);
|
|
||||||
TestEnumSerialization(testEnum1.B);
|
|
||||||
TestEnumSerialization(testEnum1.C);
|
|
||||||
TestEnumSerialization(testEnum1.D);
|
|
||||||
TestEnumSerialization(testEnum1.E);
|
|
||||||
TestEnumSerialization(testEnum1.F);
|
|
||||||
|
|
||||||
TestEnumSerialization(testEnum2.NONE);
|
|
||||||
TestEnumSerialization(testEnum2.A);
|
|
||||||
TestEnumSerialization(testEnum2.B);
|
|
||||||
TestEnumSerialization(testEnum2.C);
|
|
||||||
TestEnumSerialization(testEnum2.D);
|
|
||||||
TestEnumSerialization(testEnum2.E);
|
|
||||||
|
|
||||||
TestEnumSerialization(testEnum3.NONE);
|
|
||||||
TestEnumSerialization(testEnum3.A);
|
|
||||||
TestEnumSerialization(testEnum3.B);
|
|
||||||
TestEnumSerialization(testEnum3.C);
|
|
||||||
TestEnumSerialization(testEnum3.D);
|
|
||||||
TestEnumSerialization(testEnum3.E);
|
|
||||||
TestEnumSerialization((testEnum3)23);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool TestEnumSerialization(object enumValue)
|
|
||||||
{
|
|
||||||
Type enumType = enumValue.GetType();
|
|
||||||
|
|
||||||
Assert.IsTrue(JSONMapper.DefaultMapper.Serialize(enumValue, out JSONValue jsonEnum));
|
|
||||||
|
|
||||||
JSONMapper.DefaultMapper.Deserialize(jsonEnum, enumType, out object enunValueDeserialized);
|
|
||||||
|
|
||||||
Assert.AreEqual(enumType, enunValueDeserialized.GetType());
|
|
||||||
Assert.AreEqual(enumValue, enunValueDeserialized);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void TestMapper()
|
|
||||||
{
|
|
||||||
TestPrimitiveMapping((byte)0x80);
|
|
||||||
TestPrimitiveMapping((short)0x80);
|
|
||||||
TestPrimitiveMapping((ushort)0x80);
|
|
||||||
TestPrimitiveMapping((int)0x80);
|
|
||||||
TestPrimitiveMapping((uint)0x80);
|
|
||||||
TestPrimitiveMapping((long)0x80);
|
|
||||||
TestPrimitiveMapping((ulong)0x80);
|
|
||||||
|
|
||||||
|
|
||||||
TestDeserialization("4", testEnum3.C);
|
|
||||||
TestDeserialization("\"C\"", testEnum3.C);
|
|
||||||
TestDeserialization("4", testEnum2.C);
|
|
||||||
TestDeserialization("\"C\"", testEnum2.C);
|
|
||||||
|
|
||||||
TestDeserialization("7", testEnum3.E | testEnum3.A);
|
|
||||||
TestDeserialization("\"A,E\"", testEnum3.A | testEnum3.E);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void TestDeserialize()
|
|
||||||
{
|
|
||||||
Assert.IsTrue(JSONMapper.DefaultMapper.Deserialize("[\"A\",\"B\",\"C\"]", out string[] texts));
|
|
||||||
Assert.AreEqual(3, texts.Length);
|
|
||||||
Assert.AreEqual("A", texts[0]);
|
|
||||||
Assert.AreEqual("B", texts[1]);
|
|
||||||
Assert.AreEqual("C", texts[2]);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Test]
|
|
||||||
public void TestParameterMapping()
|
|
||||||
{
|
|
||||||
MethodInfo testMethodInfo = GetType().GetMethod("MappingTestMethod");
|
|
||||||
object[] methodArguments = null;
|
|
||||||
|
|
||||||
JSONArray jsonArguments = new JSONArray()
|
|
||||||
.Add(new JSONString("text parameter value"))
|
|
||||||
.Add(new JSONNumber(10))
|
|
||||||
.Add(new JSONNumber(-2))
|
|
||||||
.Add(new JSONNumber(-3))
|
|
||||||
;
|
|
||||||
|
|
||||||
Assert.IsTrue(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonArguments, out object[] arguments));
|
|
||||||
Assert.AreEqual("text parameter value", arguments[0]);
|
|
||||||
Assert.AreEqual(10, arguments[1]);
|
|
||||||
Assert.AreEqual(-2, arguments[2]);
|
|
||||||
Assert.AreEqual(-3, arguments[3]);
|
|
||||||
|
|
||||||
jsonArguments.Remove(3);
|
|
||||||
Assert.IsFalse(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonArguments, out arguments));
|
|
||||||
|
|
||||||
JSONObject jsonNamedArguments = new JSONObject()
|
|
||||||
.Add("n", new JSONNumber(10))
|
|
||||||
.Add("m", new JSONNumber(-2))
|
|
||||||
.Add("a", new JSONNumber(-3))
|
|
||||||
.Add("t", new JSONString("text parameter value"))
|
|
||||||
;
|
|
||||||
|
|
||||||
Assert.IsTrue(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonNamedArguments, out arguments));
|
|
||||||
Assert.AreEqual("text parameter value", arguments[0]);
|
|
||||||
Assert.AreEqual(10, arguments[1]);
|
|
||||||
Assert.AreEqual(-2, arguments[2]);
|
|
||||||
Assert.AreEqual(-3, arguments[3]);
|
|
||||||
|
|
||||||
jsonNamedArguments.Remove("m");
|
|
||||||
Assert.IsTrue(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonNamedArguments, out arguments));
|
|
||||||
|
|
||||||
jsonNamedArguments.Remove("a");
|
|
||||||
Assert.IsTrue(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonNamedArguments, out arguments));
|
|
||||||
|
|
||||||
jsonNamedArguments.Remove("n");
|
|
||||||
Assert.IsFalse(JSONMapper.DefaultMapper.MapMethodParameters(testMethodInfo, jsonNamedArguments, out arguments));
|
|
||||||
}
|
|
||||||
|
|
||||||
public void MappingTestMethod(string t, int n, short m = 2, long a = 3)
|
|
||||||
{}
|
|
||||||
|
|
||||||
|
|
||||||
void TestPrimitiveMapping(object primitiveValue)
|
|
||||||
{
|
|
||||||
Type primitiveType = primitiveValue.GetType();
|
|
||||||
Assert.IsTrue(JSONMapper.DefaultMapper.Serialize(primitiveValue, out JSONValue jsonPrimitive));
|
|
||||||
JSONMapper.DefaultMapper.Deserialize(jsonPrimitive, primitiveType, out object primitiveValue2);
|
|
||||||
Assert.AreEqual(primitiveValue, primitiveValue2);
|
|
||||||
Assert.AreEqual(primitiveType, primitiveValue2.GetType());
|
|
||||||
}
|
|
||||||
|
|
||||||
void TestDeserialization(string jsonSource, object targetValue)
|
|
||||||
{
|
|
||||||
Type targetType = targetValue.GetType();
|
|
||||||
JSONValue json = JSONParser.Parse(jsonSource);
|
|
||||||
Assert.IsTrue(JSONMapper.DefaultMapper.Deserialize(json, targetType, out object value));
|
|
||||||
Assert.AreEqual(targetType, value.GetType());
|
|
||||||
Assert.AreEqual(targetValue, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,19 +0,0 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<TargetFramework>net7.0</TargetFramework>
|
|
||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="NUnit" Version="3.12.0" />
|
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
|
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<ProjectReference Include="..\ln.json\ln.json.csproj" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
|
|
@ -1,7 +0,0 @@
|
||||||
using System;
|
|
||||||
|
|
||||||
namespace ln.json.attributes;
|
|
||||||
|
|
||||||
public class DontAssign : Attribute
|
|
||||||
{
|
|
||||||
}
|
|
|
@ -1,21 +0,0 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
|
||||||
|
|
||||||
<PropertyGroup>
|
|
||||||
<Version>1.0.8-ci</Version>
|
|
||||||
<Authors>Harald Wolff-Thobaben</Authors>
|
|
||||||
<Company>l--n.de</Company>
|
|
||||||
<Product>ln.json</Product>
|
|
||||||
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
|
|
||||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
|
||||||
<AssemblyVersion>0.1.0.0</AssemblyVersion>
|
|
||||||
<FileVersion>0.1.0.0</FileVersion>
|
|
||||||
<PackageVersion>1.3.0-preview8</PackageVersion>
|
|
||||||
<TargetFramework>net7.0</TargetFramework>
|
|
||||||
</PropertyGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="System.Runtime" Version="4.3.1" />
|
|
||||||
<PackageReference Include="ln.type" Version="0.1.9" />
|
|
||||||
<PackageReference Include="ln.collections" Version="0.2.2" />
|
|
||||||
</ItemGroup>
|
|
||||||
</Project>
|
|
|
@ -1,7 +1,7 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Net;
|
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace ln.json.mapping
|
namespace ln.json.mapping
|
||||||
{
|
{
|
||||||
|
@ -55,38 +55,21 @@ namespace ln.json.mapping
|
||||||
|
|
||||||
|
|
||||||
Dictionary<Type, JSONMapping> mappings = new Dictionary<Type, JSONMapping>();
|
Dictionary<Type, JSONMapping> mappings = new Dictionary<Type, JSONMapping>();
|
||||||
|
public virtual void Add(JSONMapping mapping) => mappings[mapping.TargetType] = mapping;
|
||||||
public virtual void Add(JSONMapping mapping)
|
|
||||||
{
|
|
||||||
lock (mappings)
|
|
||||||
{
|
|
||||||
mappings[mapping.TargetType] = mapping;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Dictionary<Type, MappingFactory> mappingFactories = new Dictionary<Type, MappingFactory>();
|
Dictionary<Type, MappingFactory> mappingFactories = new Dictionary<Type, MappingFactory>();
|
||||||
public virtual void AddMappingFactory(Type targetType, MappingFactory mappingFactory) => mappingFactories.Add(targetType, mappingFactory);
|
public virtual void AddMappingFactory(Type targetType, MappingFactory mappingFactory) => mappingFactories.Add(targetType, mappingFactory);
|
||||||
|
|
||||||
public virtual bool GetOrBuildMapping(Type nativeType, out JSONMapping mapping)
|
public virtual bool GetOrBuildMapping(Type nativeType, out JSONMapping mapping) => TryGetMapping(nativeType, out mapping) || TryBuildRememberedMapping(nativeType, out mapping);
|
||||||
{
|
|
||||||
lock (mappings)
|
|
||||||
{
|
|
||||||
return TryGetMapping(nativeType, out mapping) || TryBuildRememberedMapping(nativeType, out mapping);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual bool TryBuildRememberedMapping(Type nativeType,out JSONMapping mapping)
|
public virtual bool TryBuildRememberedMapping(Type nativeType,out JSONMapping mapping)
|
||||||
{
|
{
|
||||||
lock (mappings)
|
if (TryBuildMapping(nativeType, out mapping))
|
||||||
{
|
{
|
||||||
if (TryBuildMapping(nativeType, out mapping))
|
mappings.Add(nativeType, mapping);
|
||||||
{
|
return true;
|
||||||
mappings.Add(nativeType, mapping);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
public virtual bool TryBuildMapping(Type nativeType,out JSONMapping mapping)
|
public virtual bool TryBuildMapping(Type nativeType,out JSONMapping mapping)
|
||||||
{
|
{
|
||||||
|
@ -137,107 +120,70 @@ namespace ln.json.mapping
|
||||||
|
|
||||||
public virtual bool TryGetMapping(Type nativeType,out JSONMapping mapping)
|
public virtual bool TryGetMapping(Type nativeType,out JSONMapping mapping)
|
||||||
{
|
{
|
||||||
lock (mappings)
|
if (mappings.TryGetValue(nativeType, out mapping))
|
||||||
{
|
return true;
|
||||||
if (mappings.TryGetValue(nativeType, out mapping))
|
|
||||||
return true;
|
|
||||||
|
|
||||||
if (RequestCustomMapping(nativeType, out mapping))
|
if (RequestCustomMapping(nativeType, out mapping))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
if ((this != DefaultMapper) && DefaultMapper.TryGetMapping(nativeType, out mapping))
|
if ((this != DefaultMapper) && DefaultMapper.TryGetMapping(nativeType, out mapping))
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public virtual bool Serialize(object o, out JSONValue json)
|
public virtual bool Serialize(object o, out JSONValue json)
|
||||||
{
|
{
|
||||||
if (o is JSONValue jValue)
|
if (object.ReferenceEquals(null, o))
|
||||||
{
|
{
|
||||||
json = jValue.Clone();
|
json = JSONNull.Instance;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
lock (mappings)
|
Type type = o.GetType();
|
||||||
|
|
||||||
|
if (RequestCustomSerialization(o, out json))
|
||||||
|
return true;
|
||||||
|
|
||||||
|
if (TryGetMapping(type, out JSONMapping mapping))
|
||||||
{
|
{
|
||||||
if (object.ReferenceEquals(null, o))
|
json = mapping.ToJson(this, o);
|
||||||
{
|
|
||||||
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)
|
|
||||||
{
|
|
||||||
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
public virtual bool Deserialize<T>(JSONValue json, out T v)
|
|
||||||
{
|
|
||||||
if (Deserialize(json, typeof(T), out object o))
|
|
||||||
{
|
|
||||||
v = (T)o;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
v = default;
|
|
||||||
|
if (type.IsEnum)
|
||||||
|
{
|
||||||
|
if (type.GetCustomAttribute<FlagsAttribute>() != null)
|
||||||
|
{
|
||||||
|
json = new JSONNumber((int)o);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
json = new JSONString(Enum.GetName(type, o));
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual bool Deserialize(string jsonSource, Type nativeType, out object o) => Deserialize(JSONParser.Parse(jsonSource), nativeType, out o);
|
|
||||||
public virtual bool Deserialize(JSONValue json, Type nativeType, out object o)
|
public virtual bool Deserialize(JSONValue json, Type nativeType, out object o)
|
||||||
{
|
{
|
||||||
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;
|
||||||
|
|
||||||
|
@ -252,13 +198,14 @@ namespace ln.json.mapping
|
||||||
|
|
||||||
if (nativeType.IsEnum)
|
if (nativeType.IsEnum)
|
||||||
{
|
{
|
||||||
if (json is JSONNumber jsonNumber)
|
if (nativeType.GetCustomAttribute<FlagsAttribute>() != null)
|
||||||
o = Enum.ToObject(nativeType, jsonNumber.ToNative());
|
{
|
||||||
else if (json is JSONString jsonString)
|
o = Enum.ToObject(nativeType, (json as JSONNumber).AsInt);
|
||||||
o = Enum.Parse(nativeType, jsonString.Value);
|
}
|
||||||
else
|
else
|
||||||
throw new NotSupportedException(String.Format("Mapping from {0} to {1} is not supported", json.GetType().Name, nativeType.Name));
|
{
|
||||||
|
o = Enum.Parse(nativeType, (json as JSONString).Value);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -306,82 +253,16 @@ namespace ln.json.mapping
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public bool MapMethodParameters(MethodInfo methodInfo, JSONValue jsonArguments, out object[] methodArguments)
|
|
||||||
{
|
|
||||||
if (jsonArguments is JSONArray jsonArrayArguments)
|
|
||||||
return MapMethodParameters(methodInfo.GetParameters(), jsonArrayArguments, out methodArguments);
|
|
||||||
if (jsonArguments is JSONObject jsonNamedArguments)
|
|
||||||
return MapMethodParameters(methodInfo.GetParameters(), jsonNamedArguments, out methodArguments);
|
|
||||||
|
|
||||||
throw new ArgumentException(nameof(jsonArguments));
|
|
||||||
}
|
|
||||||
public bool MapMethodParameters(ParameterInfo[] parameterInfos, JSONValue jsonArguments, out object[] methodArguments)
|
|
||||||
{
|
|
||||||
if (jsonArguments is JSONArray jsonArrayArguments)
|
|
||||||
return MapMethodParameters(parameterInfos, jsonArrayArguments, out methodArguments);
|
|
||||||
if (jsonArguments is JSONObject jsonNamedArguments)
|
|
||||||
return MapMethodParameters(parameterInfos, jsonNamedArguments, out methodArguments);
|
|
||||||
|
|
||||||
throw new ArgumentException(nameof(jsonArguments));
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool MapMethodParameters(MethodInfo methodInfo, JSONObject namedArguments, out object[] methodArguments) => MapMethodParameters(methodInfo.GetParameters(), namedArguments, out methodArguments);
|
|
||||||
|
|
||||||
public bool MapMethodParameters(ParameterInfo[] parameterInfos, JSONObject namedArguments, out object[] methodArguments)
|
|
||||||
{
|
|
||||||
methodArguments = new object[parameterInfos.Length];
|
|
||||||
|
|
||||||
for (int n=0; n < methodArguments.Length; n++)
|
|
||||||
{
|
|
||||||
if (namedArguments.ContainsKey(parameterInfos[n].Name))
|
|
||||||
{
|
|
||||||
if (!JSONMapper.DefaultMapper.Deserialize(namedArguments[parameterInfos[n].Name], parameterInfos[n].ParameterType, out methodArguments[n]))
|
|
||||||
return false;
|
|
||||||
} else if (parameterInfos[n].IsOptional)
|
|
||||||
{
|
|
||||||
methodArguments[n] = parameterInfos[n].DefaultValue;
|
|
||||||
} else
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public bool MapMethodParameters(MethodInfo methodInfo, JSONArray jsonArguments, out object[] methodArguments) => MapMethodParameters(methodInfo.GetParameters(), jsonArguments, out methodArguments);
|
|
||||||
public bool MapMethodParameters(ParameterInfo[] parameterInfos, JSONArray jsonArguments, out object[] methodArguments)
|
|
||||||
{
|
|
||||||
methodArguments = new object[parameterInfos.Length];
|
|
||||||
|
|
||||||
if (jsonArguments.Count != methodArguments.Length)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
for (int n=0; n < methodArguments.Length; n++)
|
|
||||||
{
|
|
||||||
if (!JSONMapper.DefaultMapper.Deserialize(jsonArguments[n], parameterInfos[n].ParameterType, out methodArguments[n]))
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
static JSONMapper()
|
static JSONMapper()
|
||||||
{
|
{
|
||||||
DefaultMapper.Add(new JSONMapping(
|
DefaultMapper.Add(new JSONMapping(
|
||||||
typeof(byte),
|
typeof(byte),
|
||||||
(JSONMapper arg1, object arg2) => new JSONNumber((int)(byte)arg2),
|
(JSONMapper arg1, object arg2) => new JSONNumber((int)arg2),
|
||||||
(JSONMapper arg1, JSONValue arg2) => Decimal.ToByte(((JSONNumber)arg2).Decimal)
|
(JSONMapper arg1, JSONValue arg2) => Decimal.ToByte(((JSONNumber)arg2).Decimal)
|
||||||
));
|
));
|
||||||
DefaultMapper.Add(new JSONMapping(
|
|
||||||
typeof(sbyte),
|
|
||||||
(JSONMapper arg1, object arg2) => new JSONNumber((int)(sbyte)arg2),
|
|
||||||
(JSONMapper arg1, JSONValue arg2) => Decimal.ToSByte(((JSONNumber)arg2).Decimal)
|
|
||||||
));
|
|
||||||
DefaultMapper.Add(new JSONMapping(
|
DefaultMapper.Add(new JSONMapping(
|
||||||
typeof(short),
|
typeof(short),
|
||||||
(JSONMapper arg1, object arg2) => new JSONNumber((int)(short)arg2),
|
(JSONMapper arg1, object arg2) => new JSONNumber((int)arg2),
|
||||||
(JSONMapper arg1, JSONValue arg2) => Decimal.ToInt16(((JSONNumber)arg2).Decimal)
|
(JSONMapper arg1, JSONValue arg2) => Decimal.ToInt16(((JSONNumber)arg2).Decimal)
|
||||||
));
|
));
|
||||||
DefaultMapper.Add(new JSONMapping(
|
DefaultMapper.Add(new JSONMapping(
|
||||||
|
@ -396,12 +277,12 @@ namespace ln.json.mapping
|
||||||
));
|
));
|
||||||
DefaultMapper.Add(new JSONMapping(
|
DefaultMapper.Add(new JSONMapping(
|
||||||
typeof(ushort),
|
typeof(ushort),
|
||||||
(JSONMapper arg1, object arg2) => new JSONNumber((uint)(ushort)arg2),
|
(JSONMapper arg1, object arg2) => new JSONNumber((uint)arg2),
|
||||||
(JSONMapper arg1, JSONValue arg2) => Decimal.ToUInt16(((JSONNumber)arg2).Decimal)
|
(JSONMapper arg1, JSONValue arg2) => Decimal.ToUInt16(((JSONNumber)arg2).Decimal)
|
||||||
));
|
));
|
||||||
DefaultMapper.Add(new JSONMapping(
|
DefaultMapper.Add(new JSONMapping(
|
||||||
typeof(uint),
|
typeof(uint),
|
||||||
(JSONMapper arg1, object arg2) => new JSONNumber((uint)(uint)arg2),
|
(JSONMapper arg1, object arg2) => new JSONNumber((uint)arg2),
|
||||||
(JSONMapper arg1, JSONValue arg2) => Decimal.ToUInt32(((JSONNumber)arg2).Decimal)
|
(JSONMapper arg1, JSONValue arg2) => Decimal.ToUInt32(((JSONNumber)arg2).Decimal)
|
||||||
));
|
));
|
||||||
DefaultMapper.Add(new JSONMapping(
|
DefaultMapper.Add(new JSONMapping(
|
||||||
|
@ -447,7 +328,7 @@ namespace ln.json.mapping
|
||||||
DefaultMapper.Add(new JSONMapping(
|
DefaultMapper.Add(new JSONMapping(
|
||||||
typeof(bool),
|
typeof(bool),
|
||||||
(JSONMapper arg1, object arg2) => ((bool)arg2) ? (JSONValue)JSONTrue.Instance : (JSONValue)JSONFalse.Instance,
|
(JSONMapper arg1, object arg2) => ((bool)arg2) ? (JSONValue)JSONTrue.Instance : (JSONValue)JSONFalse.Instance,
|
||||||
(JSONMapper arg1, JSONValue arg2) => (arg2.ValueType == JSONValueType.TRUE) ? true : false
|
(JSONMapper arg1, JSONValue arg2) => (arg2.ValueType == JSONValueType.TRUE) || (arg2.ValueType == JSONValueType.FALSE) ? false : throw new NotSupportedException()
|
||||||
));
|
));
|
||||||
|
|
||||||
DefaultMapper.Add(new JSONByteArrayMapping());
|
DefaultMapper.Add(new JSONByteArrayMapping());
|
||||||
|
@ -457,16 +338,8 @@ namespace ln.json.mapping
|
||||||
DefaultMapper.Add(new JSONGuidMapping());
|
DefaultMapper.Add(new JSONGuidMapping());
|
||||||
DefaultMapper.Add(new JSONTimeSpanMapping());
|
DefaultMapper.Add(new JSONTimeSpanMapping());
|
||||||
|
|
||||||
DefaultMapper.Add(new JSONIPv6Mapping());
|
|
||||||
|
|
||||||
DefaultMapper.Add(new JSONRPCCallMapping());
|
DefaultMapper.Add(new JSONRPCCallMapping());
|
||||||
DefaultMapper.Add(new JSONRPCResultMapping());
|
DefaultMapper.Add(new JSONRPCResultMapping());
|
||||||
|
|
||||||
DefaultMapper.Add(new JSONMapping(
|
|
||||||
typeof(IPAddress),
|
|
||||||
((mapper, o) => ((IPAddress)o).ToString()),
|
|
||||||
(mapper, value) => IPAddress.Parse((value as JSONString).Value))
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -25,7 +25,6 @@ namespace ln.json.mapping
|
||||||
Dictionary<string, Action<object,object>> setters = new Dictionary<string, Action<object,object>>();
|
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, Func<object, object>> getters = new Dictionary<string, Func<object, object>>();
|
||||||
Dictionary<string, Type> types = new Dictionary<string, Type>();
|
Dictionary<string, Type> types = new Dictionary<string, Type>();
|
||||||
private HashSet<string> _dontAssign = new HashSet<string>();
|
|
||||||
|
|
||||||
public JSONObjectMapping(Type type)
|
public JSONObjectMapping(Type type)
|
||||||
: this(type, JSONObjectMappingFlags.FIELDS | JSONObjectMappingFlags.PROPERTIES, BindingFlags.Instance | BindingFlags.Public)
|
: this(type, JSONObjectMappingFlags.FIELDS | JSONObjectMappingFlags.PROPERTIES, BindingFlags.Instance | BindingFlags.Public)
|
||||||
|
@ -45,8 +44,6 @@ namespace ln.json.mapping
|
||||||
setters.Add(fieldInfo.Name, (object arg1, object arg2) => fieldInfo.SetValue(arg1, arg2));
|
setters.Add(fieldInfo.Name, (object arg1, object arg2) => fieldInfo.SetValue(arg1, arg2));
|
||||||
getters.Add(fieldInfo.Name, (object arg) => fieldInfo.GetValue(arg));
|
getters.Add(fieldInfo.Name, (object arg) => fieldInfo.GetValue(arg));
|
||||||
types.Add(fieldInfo.Name, fieldInfo.FieldType);
|
types.Add(fieldInfo.Name, fieldInfo.FieldType);
|
||||||
if (fieldInfo.GetCustomAttribute<DontAssign>() != null)
|
|
||||||
_dontAssign.Add(fieldInfo.Name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if ((mappingFlags & JSONObjectMappingFlags.PROPERTIES) == JSONObjectMappingFlags.PROPERTIES)
|
if ((mappingFlags & JSONObjectMappingFlags.PROPERTIES) == JSONObjectMappingFlags.PROPERTIES)
|
||||||
|
@ -58,8 +55,6 @@ namespace ln.json.mapping
|
||||||
setters.Add(propertyInfo.Name, (object arg1, object arg2) => { if (propertyInfo.CanWrite) propertyInfo.SetValue(arg1, arg2); });
|
setters.Add(propertyInfo.Name, (object arg1, object arg2) => { if (propertyInfo.CanWrite) propertyInfo.SetValue(arg1, arg2); });
|
||||||
getters.Add(propertyInfo.Name, (object arg) => propertyInfo.GetValue(arg));
|
getters.Add(propertyInfo.Name, (object arg) => propertyInfo.GetValue(arg));
|
||||||
types.Add(propertyInfo.Name, propertyInfo.PropertyType);
|
types.Add(propertyInfo.Name, propertyInfo.PropertyType);
|
||||||
if (propertyInfo.GetCustomAttribute<DontAssign>() != null)
|
|
||||||
_dontAssign.Add(propertyInfo.Name);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,20 +102,5 @@ 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue