From 0ff1ad008c6022ba90095c1273296b0c0f4b3b15 Mon Sep 17 00:00:00 2001 From: joyfullservice Date: Mon, 25 May 2020 14:54:29 -0500 Subject: [PATCH 1/3] Add option to preserve Unicode In many cases we may want to preserve the Unicode text in our JSON content. This adds a non-default option to preserve the Unicode content. --- JsonConverter.bas | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/JsonConverter.bas b/JsonConverter.bas index 876b865..793fc90 100644 --- a/JsonConverter.bas +++ b/JsonConverter.bas @@ -154,6 +154,9 @@ Private Type json_Options ' The solidus (/) is not required to be escaped, use this option to escape them as \/ in ConvertToJson EscapeSolidus As Boolean + + ' Allow Unicode characters in JSON text. Set to True to use native Unicode or false for escaped values. + AllowUnicodeChars As Boolean End Type Public JsonOptions As json_Options @@ -725,9 +728,14 @@ Private Function json_Encode(ByVal json_Text As Variant) As String Case 9 ' tab -> 9 -> \t json_Char = "\t" - Case 0 To 31, 127 To 65535 + Case 0 To 31 ' Non-ascii characters -> convert to 4-digit hex json_Char = "\u" & VBA.Right$("0000" & VBA.Hex$(json_AscCode), 4) + Case 127 To 65535 + ' Unicode character range + If Not JsonOptions.AllowUnicodeChars Then + json_Char = "\u" & VBA.Right$("0000" & VBA.Hex$(json_AscCode), 4) + End If End Select json_BufferAppend json_Buffer, json_Char, json_BufferPosition, json_BufferLength From 45966b67f7fe0b3193cbabe6cd98224f863fe41a Mon Sep 17 00:00:00 2001 From: joyfullservice Date: Mon, 24 Jul 2023 11:14:47 -0500 Subject: [PATCH 2/3] Fix Line Feed character parsing in multiline value When converting JSON back to a string, Line Feed characters should be converted back to line feed characters, not Carriage Return & Line Feed. --- JsonConverter.bas | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/JsonConverter.bas b/JsonConverter.bas index 793fc90..11e0d38 100644 --- a/JsonConverter.bas +++ b/JsonConverter.bas @@ -578,7 +578,7 @@ Private Function json_ParseString(json_String As String, ByRef json_Index As Lon json_BufferAppend json_Buffer, vbFormFeed, json_BufferPosition, json_BufferLength json_Index = json_Index + 1 Case "n" - json_BufferAppend json_Buffer, vbCrLf, json_BufferPosition, json_BufferLength + json_BufferAppend json_Buffer, vbLf, json_BufferPosition, json_BufferLength json_Index = json_Index + 1 Case "r" json_BufferAppend json_Buffer, vbCr, json_BufferPosition, json_BufferLength From 4f70c8ef81baebb21b094fd0eeb05715a40e3dab Mon Sep 17 00:00:00 2001 From: joyfullservice Date: Sat, 17 May 2025 16:13:29 -0500 Subject: [PATCH 3/3] Improve accuracy of Unicode range Based on suggestion from @fricyo. This should better handle the euro symbol which is available in commonly used code pages. --- JsonConverter.bas | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/JsonConverter.bas b/JsonConverter.bas index 11e0d38..767c7ec 100644 --- a/JsonConverter.bas +++ b/JsonConverter.bas @@ -728,10 +728,10 @@ Private Function json_Encode(ByVal json_Text As Variant) As String Case 9 ' tab -> 9 -> \t json_Char = "\t" - Case 0 To 31 + Case 0 To 31, 127 To 159 ' Non-ascii characters -> convert to 4-digit hex json_Char = "\u" & VBA.Right$("0000" & VBA.Hex$(json_AscCode), 4) - Case 127 To 65535 + Case 160 To 65535 ' Unicode character range If Not JsonOptions.AllowUnicodeChars Then json_Char = "\u" & VBA.Right$("0000" & VBA.Hex$(json_AscCode), 4)