Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit e13d44f

Browse files
authored
Fixes JSON down converter invalid decimal issue. (#134)
1 parent 11b71bf commit e13d44f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Amazon.IonDotnet.Tests/Internals/TextWriterJsonTest.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,21 @@ public void Cleanup()
4646
this.sw.Dispose();
4747
}
4848

49+
[TestMethod]
50+
[DataRow("0.2")]
51+
[DataRow("2.d-1")]
52+
[DataRow("2d-1")]
53+
public void TestInvalidJsonDecimalFromIon(string decimalString)
54+
{
55+
var bigDecimal = BigDecimal.Parse(decimalString);
56+
57+
value.SetField("value", factory.NewDecimal(bigDecimal));
58+
var reader = IonReaderBuilder.Build(value);
59+
jsonWriter.WriteValues(reader);
60+
61+
Assert.AreEqual("{\"value\":2e-1}", this.sw.ToString());
62+
}
63+
4964
[TestMethod]
5065
public void TestGenericNull()
5166
{

Amazon.IonDotnet/Internals/Text/IonTextWriter.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,16 @@ public override void WriteDecimal(BigDecimal value)
218218
{
219219
var decimalString = value.ToString();
220220
decimalString = decimalString.Replace('d', 'e');
221+
222+
// Since Ion Decimal allows an exponent integer following '.' (e.g. 2.d-1) while JSON doesn't, we
223+
// should make sure JSON down converter doesn't write any decimal number with '.' immediately
224+
// followed by 'e'.
225+
var index = decimalString.IndexOf(".e", 0);
226+
if (index != -1)
227+
{
228+
decimalString = decimalString.Remove(index, 1);
229+
}
230+
221231
this.textWriter.Write(decimalString);
222232
}
223233
else

0 commit comments

Comments
 (0)