Skip to content

Commit

Permalink
Fixes JSON down converter invalid decimal issue. (#134)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheqianh authored Nov 5, 2021
1 parent 11b71bf commit e13d44f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
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

0 comments on commit e13d44f

Please sign in to comment.