From 8d397ecbc6926d447cee79c67e90357f7793434c Mon Sep 17 00:00:00 2001 From: Harald Wolff-Thobaben Date: Tue, 14 Jun 2022 11:07:22 +0200 Subject: [PATCH] Added dynamic Object support, added JSONObject.TryGetValue(..) --- .idea/.idea.ln.json/.idea/.gitignore | 13 ++++++++ .idea/.idea.ln.json/.idea/encodings.xml | 4 +++ .idea/.idea.ln.json/.idea/indexLayout.xml | 8 +++++ .idea/.idea.ln.json/.idea/vcs.xml | 6 ++++ build.ln | 2 +- ln.json.tests/ln.json.tests.csproj | 4 +-- ln.json/JSONObject.cs | 37 +++++++++++++++++++++++ ln.json/JSONValue.cs | 6 +++- ln.json/ln.json.csproj | 5 +-- 9 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 .idea/.idea.ln.json/.idea/.gitignore create mode 100644 .idea/.idea.ln.json/.idea/encodings.xml create mode 100644 .idea/.idea.ln.json/.idea/indexLayout.xml create mode 100644 .idea/.idea.ln.json/.idea/vcs.xml diff --git a/.idea/.idea.ln.json/.idea/.gitignore b/.idea/.idea.ln.json/.idea/.gitignore new file mode 100644 index 0000000..84afd64 --- /dev/null +++ b/.idea/.idea.ln.json/.idea/.gitignore @@ -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/ diff --git a/.idea/.idea.ln.json/.idea/encodings.xml b/.idea/.idea.ln.json/.idea/encodings.xml new file mode 100644 index 0000000..df87cf9 --- /dev/null +++ b/.idea/.idea.ln.json/.idea/encodings.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/.idea.ln.json/.idea/indexLayout.xml b/.idea/.idea.ln.json/.idea/indexLayout.xml new file mode 100644 index 0000000..7b08163 --- /dev/null +++ b/.idea/.idea.ln.json/.idea/indexLayout.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/.idea.ln.json/.idea/vcs.xml b/.idea/.idea.ln.json/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/.idea.ln.json/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/build.ln b/build.ln index afa2528..2a0641a 100644 --- a/build.ln +++ b/build.ln @@ -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": [ diff --git a/ln.json.tests/ln.json.tests.csproj b/ln.json.tests/ln.json.tests.csproj index 60a908e..9942f18 100644 --- a/ln.json.tests/ln.json.tests.csproj +++ b/ln.json.tests/ln.json.tests.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net5.0 false @@ -9,7 +9,7 @@ - + diff --git a/ln.json/JSONObject.cs b/ln.json/JSONObject.cs index dafd85a..58f9eb5 100644 --- a/ln.json/JSONObject.cs +++ b/ln.json/JSONObject.cs @@ -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(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() { 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)) diff --git a/ln.json/JSONValue.cs b/ln.json/JSONValue.cs index f725b69..a87a423 100644 --- a/ln.json/JSONValue.cs +++ b/ln.json/JSONValue.cs @@ -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() where T : JSONValue => this as T; + public JSONValue(JSONValueType valueType) { ValueType = valueType; diff --git a/ln.json/ln.json.csproj b/ln.json/ln.json.csproj index d952ec0..22287c9 100644 --- a/ln.json/ln.json.csproj +++ b/ln.json/ln.json.csproj @@ -1,8 +1,7 @@  - netcoreapp3.1 - 1.0.7 + 1.0.8-ci Harald Wolff-Thobaben l--n.de ln.json @@ -10,6 +9,8 @@ true 0.1.0.0 0.1.0.0 + 1.1.0 + net5.0;net6.0