From 02adec8cb842a121782ae94db18c990dbcf2ac77 Mon Sep 17 00:00:00 2001 From: Harald Wolff Date: Fri, 18 Dec 2020 21:17:59 +0100 Subject: [PATCH] added JSONMapper.Deserialize(..), added JSONMapper.Deserialize(string..) --- ln.json.tests/JSONTests.cs | 10 ++++++++++ ln.json/ln.json.csproj | 2 +- ln.json/mapping/JSONMapper.cs | 13 +++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/ln.json.tests/JSONTests.cs b/ln.json.tests/JSONTests.cs index 27916f4..c0c5e43 100644 --- a/ln.json.tests/JSONTests.cs +++ b/ln.json.tests/JSONTests.cs @@ -125,6 +125,16 @@ namespace ln.json.tests } + [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]); + } + void TestPrimitiveMapping(object primitiveValue) { Type primitiveType = primitiveValue.GetType(); diff --git a/ln.json/ln.json.csproj b/ln.json/ln.json.csproj index 992f27f..22204d8 100644 --- a/ln.json/ln.json.csproj +++ b/ln.json/ln.json.csproj @@ -2,7 +2,7 @@ netcoreapp3.1 - 1.0.5 + 1.0.6 Harald Wolff-Thobaben l--n.de ln.json diff --git a/ln.json/mapping/JSONMapper.cs b/ln.json/mapping/JSONMapper.cs index 2f2307d..0c25a70 100644 --- a/ln.json/mapping/JSONMapper.cs +++ b/ln.json/mapping/JSONMapper.cs @@ -182,6 +182,19 @@ namespace ln.json.mapping return false; } + public virtual bool Deserialize(string jsonSource, out T v) => Deserialize(JSONParser.Parse(jsonSource), out v); + public virtual bool Deserialize(JSONValue json, out T v) + { + if (Deserialize(json, typeof(T), out object o)) + { + v = (T)o; + return true; + } + v = default; + 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) { o = null;