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

Fixes JSON down converter decimal spec issue. #134

Merged
merged 3 commits into from
Nov 5, 2021
Merged
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
15 changes: 15 additions & 0 deletions Amazon.IonDotnet.Tests/Internals/TextWriterJsonTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ public void Cleanup()
this.sw.Dispose();
}

[TestMethod]
[DataRow("0.2")]
[DataRow("2.d-1")]
[DataRow("2d-1")]
public void TestInvalidJsonDecimalFromIon(string decimalString)
{
var bigDecimal = BigDecimal.Parse(decimalString);

value.SetField("value", factory.NewDecimal(bigDecimal));
var reader = IonReaderBuilder.Build(value);
jsonWriter.WriteValues(reader);

Assert.AreEqual("{\"value\":2e-1}", this.sw.ToString());
}

[TestMethod]
public void TestGenericNull()
{
Expand Down
10 changes: 10 additions & 0 deletions Amazon.IonDotnet/Internals/Text/IonTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ public override void WriteDecimal(BigDecimal value)
{
var decimalString = value.ToString();
decimalString = decimalString.Replace('d', 'e');

// Since Ion Decimal allows an exponent integer following '.' (e.g. 2.d-1) while JSON doesn't, we
// should make sure JSON down converter doesn't write any decimal number with '.' immediately
// followed by 'e'.
var index = decimalString.IndexOf(".e", 0);
if (index != -1)
{
decimalString = decimalString.Remove(index, 1);
}

this.textWriter.Write(decimalString);
}
else
Expand Down