Added dynamic Object support, added JSONObject.TryGetValue(..)

master
Harald Wolff 2022-06-14 11:07:22 +02:00
parent fd9e11a182
commit 8d397ecbc6
9 changed files with 79 additions and 6 deletions

View File

@ -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/

View File

@ -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>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="UserContentModel">
<attachedFolders />
<explicitIncludes />
<explicitExcludes />
</component>
</project>

View File

@ -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>

View File

@ -3,7 +3,7 @@
"dotnet"
],
"env": {
"NUGET_SOURCE": "https://nexus.niclas-thobaben.de/repository/l--n.de/",
"NUGET_SOURCE": "https://nexus.l--n.de/repository/ln.net/",
"CONFIGURATION": "Release"
},
"stages": [

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
@ -9,7 +9,7 @@
<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"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
</ItemGroup>
<ItemGroup>

View File

@ -1,6 +1,7 @@
using System;
using System.Text;
using System.Collections.Generic;
using System.Dynamic;
using ln.collections;
using ln.json.mapping;
@ -47,6 +48,20 @@ namespace ln.json
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>()
{
if (JSONMapper.DefaultMapper.Deserialize(this, typeof(T), out object o))
@ -80,6 +95,28 @@ namespace ln.json
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)
{
if (JSONMapper.DefaultMapper.Serialize(value, out JSONValue json))

View File

@ -9,13 +9,15 @@
// **/
using System;
using System.Collections.Generic;
using System.Dynamic;
namespace ln.json
{
public enum JSONValueType
{
NULL, OBJECT, ARRAY, STRING, NUMBER, TRUE, FALSE
}
public abstract class JSONValue
public abstract class JSONValue : DynamicObject
{
public JSONValueType ValueType { get; private set; }
@ -24,6 +26,8 @@ namespace ln.json
public virtual object ToNative() => throw new NotImplementedException();
public T As<T>() where T : JSONValue => this as T;
public JSONValue(JSONValueType valueType)
{
ValueType = valueType;

View File

@ -1,8 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>1.0.7</Version>
<Version>1.0.8-ci</Version>
<Authors>Harald Wolff-Thobaben</Authors>
<Company>l--n.de</Company>
<Product>ln.json</Product>
@ -10,6 +9,8 @@
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<AssemblyVersion>0.1.0.0</AssemblyVersion>
<FileVersion>0.1.0.0</FileVersion>
<PackageVersion>1.1.0</PackageVersion>
<TargetFrameworks>net5.0;net6.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>