Added dynamic Object support, added JSONObject.TryGetValue(..)
parent
fd9e11a182
commit
8d397ecbc6
|
@ -0,0 +1,13 @@
|
||||||
|
# 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/
|
|
@ -0,0 +1,4 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="Encoding" addBOMForNewFiles="with BOM under Windows, with no BOM otherwise" />
|
||||||
|
</project>
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="UserContentModel">
|
||||||
|
<attachedFolders />
|
||||||
|
<explicitIncludes />
|
||||||
|
<explicitExcludes />
|
||||||
|
</component>
|
||||||
|
</project>
|
|
@ -0,0 +1,6 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
2
build.ln
2
build.ln
|
@ -3,7 +3,7 @@
|
||||||
"dotnet"
|
"dotnet"
|
||||||
],
|
],
|
||||||
"env": {
|
"env": {
|
||||||
"NUGET_SOURCE": "https://nexus.niclas-thobaben.de/repository/l--n.de/",
|
"NUGET_SOURCE": "https://nexus.l--n.de/repository/ln.net/",
|
||||||
"CONFIGURATION": "Release"
|
"CONFIGURATION": "Release"
|
||||||
},
|
},
|
||||||
"stages": [
|
"stages": [
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<TargetFramework>net5.0</TargetFramework>
|
||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
@ -9,7 +9,7 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="NUnit" Version="3.12.0" />
|
<PackageReference Include="NUnit" Version="3.12.0" />
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
|
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0"/>
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
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;
|
||||||
|
|
||||||
|
@ -47,6 +48,20 @@ namespace ln.json
|
||||||
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))
|
||||||
|
@ -80,6 +95,28 @@ 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))
|
||||||
|
|
|
@ -9,13 +9,15 @@
|
||||||
// **/
|
// **/
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Dynamic;
|
||||||
|
|
||||||
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
|
public abstract class JSONValue : DynamicObject
|
||||||
{
|
{
|
||||||
public JSONValueType ValueType { get; private set; }
|
public JSONValueType ValueType { get; private set; }
|
||||||
|
|
||||||
|
@ -24,6 +26,8 @@ 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;
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
<Version>1.0.8-ci</Version>
|
||||||
<Version>1.0.7</Version>
|
|
||||||
<Authors>Harald Wolff-Thobaben</Authors>
|
<Authors>Harald Wolff-Thobaben</Authors>
|
||||||
<Company>l--n.de</Company>
|
<Company>l--n.de</Company>
|
||||||
<Product>ln.json</Product>
|
<Product>ln.json</Product>
|
||||||
|
@ -10,6 +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.1.0</PackageVersion>
|
||||||
|
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
Loading…
Reference in New Issue