From 0a11685aa42dbdb9f2367ae234c20fc60f5a0b2d Mon Sep 17 00:00:00 2001 From: Niklas Hedlund Date: Thu, 27 May 2021 15:45:56 +0200 Subject: [PATCH 1/3] Add return of null for null strings --- src/JSONParser.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/JSONParser.cs b/src/JSONParser.cs index 9db9821..eedd135 100644 --- a/src/JSONParser.cs +++ b/src/JSONParser.cs @@ -128,9 +128,13 @@ static List Split(string json) internal static object ParseValue(Type type, string json) { if (type == typeof(string)) - { + { + if (json == "null") + return null; + if (json.Length <= 2) return string.Empty; + StringBuilder parseStringBuilder = new StringBuilder(json.Length); for (int i = 1; i < json.Length - 1; ++i) { From 1968f0853f74a01bc5a4f2cdaeda9a4e66962944 Mon Sep 17 00:00:00 2001 From: Niklas Hedlund Date: Thu, 27 May 2021 15:46:44 +0200 Subject: [PATCH 2/3] Add support for nullable types --- src/JSONParser.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/JSONParser.cs b/src/JSONParser.cs index eedd135..8086984 100644 --- a/src/JSONParser.cs +++ b/src/JSONParser.cs @@ -202,6 +202,11 @@ internal static object ParseValue(Type type, string json) newArray.SetValue(ParseValue(arrayType, elems[i]), i); splitArrayPool.Push(elems); return newArray; + } + if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) + { + Type underlyingType = type.GetGenericArguments()[0]; + return ParseValue(underlyingType, json); } if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>)) { From 4d017cc8a2cb9e1a375329d237497c62d2867948 Mon Sep 17 00:00:00 2001 From: Niklas Hedlund Date: Thu, 27 May 2021 15:58:27 +0200 Subject: [PATCH 3/3] Add tests for nullable types --- test/TestParser.cs | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/test/TestParser.cs b/test/TestParser.cs index 14b7319..7eebacf 100644 --- a/test/TestParser.cs +++ b/test/TestParser.cs @@ -439,5 +439,57 @@ public void TestDuplicateKeysInAnonymousObject() */ Assert.AreEqual(dictionary["hello"], "hell", "The parser stored an incorrect value for the duplicated key"); } + + private class SimpleModelWithNulls + { + public string AString { get; set; } + public int? NullableInt { get; set; } + public float? NullableFloat { get; set; } + } + + [TestMethod] + public void FromJson_NullString_IsParsedCorrectly() + { + // Arrange + var json = "{\"AString\": null}"; + + // Act + var actual = json.FromJson(); + + // Assert + Assert.IsNull(actual.AString); + } + + [DataTestMethod] + [DataRow(null)] + [DataRow(5)] + [DataRow(-1)] + public void FromJson_NullableInt_IsParsedCorrectly(int? value) + { + // Arrange + var json = "{\"NullableInt\": " + (value?.ToString() ?? "null") + "}"; + + // Act + var actual = json.FromJson(); + + // Assert + Assert.AreEqual(value, actual.NullableInt); + } + + [DataTestMethod] + [DataRow(null)] + [DataRow(3.0f)] + [DataRow(2.5f)] + public void FromJson_NullableFloat_IsParsedCorrectly(float? value) + { + // Arrange + var json = "{\"NullableFloat\": " + (value?.ToString(System.Globalization.CultureInfo.InvariantCulture) ?? "null") + "}"; + + // Act + var actual = json.FromJson(); + + // Assert + Assert.AreEqual(value, actual.NullableFloat); + } } }