From e9e4996f0ee139bb87532f019d93c540bc6d27a5 Mon Sep 17 00:00:00 2001 From: Nate Dahlquist Date: Tue, 30 Apr 2024 14:58:30 -0500 Subject: [PATCH] Fix #155 don't use ion escaping in JSON mode --- Amazon.IonDotnet.Tests/Internals/TextWriterJsonTest.cs | 10 ++++++++++ Amazon.IonDotnet/Internals/Text/IonTextWriter.cs | 7 ++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Amazon.IonDotnet.Tests/Internals/TextWriterJsonTest.cs b/Amazon.IonDotnet.Tests/Internals/TextWriterJsonTest.cs index 2187947..8a6ed66 100644 --- a/Amazon.IonDotnet.Tests/Internals/TextWriterJsonTest.cs +++ b/Amazon.IonDotnet.Tests/Internals/TextWriterJsonTest.cs @@ -188,6 +188,16 @@ public void TestMinutePrecisionTimestampUtc() Assert.AreEqual("{\"value\":\"2010-06-15T03:30Z\"}", this.sw.ToString()); } + [TestMethod] + public void TestStringEscapingDoesntUseShortForm() + { + value.SetField("value", factory.NewString("--" + (char)0x1f + "--")); + var reader = IonReaderBuilder.Build(value); + jsonWriter.WriteValues(reader); + // Json doesn't support the shorter \xNN form, only \uNNNN + Assert.AreEqual("{\"value\":\"--\\u001f--\"}", this.sw.ToString()); + } + [TestMethod] [ExpectedException(typeof(NotSupportedException))] public void TestClob() diff --git a/Amazon.IonDotnet/Internals/Text/IonTextWriter.cs b/Amazon.IonDotnet/Internals/Text/IonTextWriter.cs index 57ed571..71f26fc 100644 --- a/Amazon.IonDotnet/Internals/Text/IonTextWriter.cs +++ b/Amazon.IonDotnet/Internals/Text/IonTextWriter.cs @@ -263,7 +263,12 @@ public override void WriteTimestamp(Timestamp value) public override void WriteString(string value) { this.StartValue(); - if (value != null && !this.followingLongString && this.options.LongStringThreshold < value.Length) + if (this.options.JsonDowngrade) + { + this.textWriter.WriteJsonString(value); + this.CloseValue(); + } + else if (value != null && !this.followingLongString && this.options.LongStringThreshold < value.Length) { this.textWriter.WriteLongString(value); this.CloseValue();