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

AVRO-2032: [C#] Add support for NaN, Infinity and -Infinity in JsonDecoder #3070

Merged
merged 3 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
57 changes: 52 additions & 5 deletions lang/csharp/src/apache/main/IO/JsonDecoder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
Expand Down Expand Up @@ -182,10 +182,25 @@ public override float ReadFloat()
reader.Read();
return result;
}
else
else if (reader.TokenType == JsonToken.String)
{
throw TypeError("float");
string str = Convert.ToString(reader.Value);
reader.Read();
if (IsNaNString(str))
{
return float.NaN;
}
else if (IsPositiveInfinityString(str))
{
return float.PositiveInfinity;
}
else if (IsNegativeInfinityString(str))
{
return float.NegativeInfinity;
}
}

throw TypeError("float");
}

/// <inheritdoc />
Expand All @@ -198,10 +213,25 @@ public override double ReadDouble()
reader.Read();
return result;
}
else
else if (reader.TokenType == JsonToken.String)
{
throw TypeError("double");
string str = Convert.ToString(reader.Value);
reader.Read();
if (IsNaNString(str))
{
return double.NaN;
}
else if (IsPositiveInfinityString(str))
{
return double.PositiveInfinity;
}
else if (IsNegativeInfinityString(str))
{
return double.NegativeInfinity;
}
}

throw TypeError("double");
}

/// <inheritdoc />
Expand Down Expand Up @@ -757,6 +787,23 @@ public override bool Read()
}
}

private bool IsNaNString(string str)
{
return str.Equals("NaN", StringComparison.Ordinal);
}

private bool IsPositiveInfinityString(string str)
{
return str.Equals("Infinity", StringComparison.Ordinal) ||
str.Equals("INF", StringComparison.Ordinal);
}

private bool IsNegativeInfinityString(string str)
{
return str.Equals("-Infinity", StringComparison.Ordinal) ||
str.Equals("-INF", StringComparison.Ordinal);
}

private AvroTypeException TypeError(string type)
{
return new AvroTypeException("Expected " + type + ". Got " + reader.TokenType);
Expand Down
55 changes: 55 additions & 0 deletions lang/csharp/src/apache/test/IO/JsonCodecTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,61 @@ public void TestJsonDecoderNumeric(string type, object value)
}
}

[Test]
[TestCase("float", "0", (float)0)]
[TestCase("float", "1", (float)1)]
[TestCase("float", "1.0", (float)1.0)]
[TestCase("double", "0", (double)0)]
[TestCase("double", "1", (double)1)]
[TestCase("double", "1.0", 1.0)]
[TestCase("float", "\"NaN\"", float.NaN)]
[TestCase("float", "\"Infinity\"", float.PositiveInfinity)]
[TestCase("float", "\"INF\"", float.PositiveInfinity)]
[TestCase("float", "\"-Infinity\"", float.NegativeInfinity)]
[TestCase("float", "\"-INF\"", float.NegativeInfinity)]
[TestCase("double", "\"NaN\"", double.NaN)]
[TestCase("double", "\"Infinity\"", double.PositiveInfinity)]
[TestCase("double", "\"INF\"", double.PositiveInfinity)]
[TestCase("double", "\"-Infinity\"", double.NegativeInfinity)]
[TestCase("double", "\"-INF\"", double.NegativeInfinity)]
[TestCase("float", "\"\"", null)]
[TestCase("float", "\"unknown\"", null)]
[TestCase("float", "\"nan\"", null)]
[TestCase("float", "\"infinity\"", null)]
[TestCase("float", "\"inf\"", null)]
[TestCase("float", "\"-infinity\"", null)]
[TestCase("float", "\"-inf\"", null)]
[TestCase("double", "\"\"", null)]
[TestCase("double", "\"unknown\"", null)]
[TestCase("double", "\"nan\"", null)]
[TestCase("double", "\"infinity\"", null)]
[TestCase("double", "\"inf\"", null)]
[TestCase("double", "\"-infinity\"", null)]
[TestCase("double", "\"-inf\"", null)]
[TestCase("double", "\"-inf\"", null)]
public void TestJsonDecodeFloatDouble(string typeStr, string valueStr, object expected)
{
string def = $"{{\"type\":\"record\",\"name\":\"X\",\"fields\":[{{\"type\":\"{typeStr}\",\"name\":\"Value\"}}]}}";
Schema schema = Schema.Parse(def);
DatumReader<GenericRecord> reader = new GenericDatumReader<GenericRecord>(schema, schema);

string record = $"{{\"Value\":{valueStr}}}";
Decoder decoder = new JsonDecoder(schema, record);
try
{
GenericRecord r = reader.Read(null, decoder);
Assert.AreEqual(expected, r["Value"]);
}
catch (AvroTypeException)
{
if (expected != null)
{
throw;
}
}

}

// Ensure that even if the order of fields in JSON is different from the order in schema, it works.
[Test]
public void TestJsonDecoderReorderFields()
Expand Down