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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix line feed character parsing in multiline value #253

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

joyfullservice
Copy link

I know this has been discussed in the following issues:

As a simple matter of principle, JSON conversion should be converting, not changing the original string. A proper conversion should support a round-trip conversion back to the original value. This can be demonstrated with the following code snippet:

'---------------------------------------------------------------------------------------
' Procedure : TestJsonNewLineIssue
' Author    : Adam Waller
' Date      : 7/24/2023
' Purpose   : Encountered an issue where vbCrLf strings are not parsed correctly when
'           : converting to JSON and back to string values.
'---------------------------------------------------------------------------------------
'
Public Sub TestJsonNewLineIssue()
    
    Const cstrTest As String = "Line1" & vbCrLf & "Line2" & vbCr & "Line3" & vbLf & "Line4" & vbCrLf
    
    Dim dTest As Dictionary
    Dim strResult As String
    
    Set dTest = New Dictionary
    
    dTest("Multiline") = cstrTest
    Debug.Assert dTest("Multiline") = cstrTest
    
    ' Test round trip conversion
    strResult = ParseJson(ConvertToJson(dTest, 2))("Multiline")
    Debug.Assert (strResult = cstrTest)
    
End Sub

Replacing a line feed (vbLf) single character, with two characters (vbCrLf) may be necessary in some applications, but this should be performed in the calling code, not embedded within the JSON conversion. In my opinion, the JSON conversion should only be performing the conversion. What goes in is what should come out. 馃槃

On the VBA side, vbCrLf = (vbCr & vbLf) so I don't know that we need to do anything more complicated than simply converting \n to vbLf.

When converting JSON back to a string, line feed characters should be converted back to line feed characters, not Carriage Return & Line Feed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant