Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for nullable types #45

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/JSONParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,13 @@ static List<string> 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)
{
Expand Down Expand Up @@ -198,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<>))
{
Expand Down
52 changes: 52 additions & 0 deletions test/TestParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SimpleModelWithNulls>();

// 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<SimpleModelWithNulls>();

// 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<SimpleModelWithNulls>();

// Assert
Assert.AreEqual(value, actual.NullableFloat);
}
}
}