Page 1 of 1

LoadJSON allow comments

Posted: Wed Feb 21, 2024 5:14 am
by Rinzwind
Ever saw an Sublime or VS Code config file? Chock full of comments... even if not officially allowed by the json standard. Would be nice if PB could read those too (flag or not) and ignore the comments (or even made it possible to add them programmatically, but that's more work).

Re: LoadJSON allow comments

Posted: Wed Feb 21, 2024 9:31 am
by jacdelad
Can you post an example?

Also, afaik PureBasic uses a lib not created by Fred himself, so you'd have to ask the creator to support it.

Re: LoadJSON allow comments

Posted: Wed Feb 21, 2024 9:39 am
by Fred
I didn't wrote the lib, but Freak did :D. So yes it's a nice hand tailored json lib specifically for PB. It's for XML where we use xerces.

Re: LoadJSON allow comments

Posted: Wed Feb 21, 2024 10:33 am
by Little John

Re: LoadJSON allow comments

Posted: Wed Feb 21, 2024 12:27 pm
by Rinzwind
Would be nice if LoadJSON just ignores comments. As current state, it just returns false and fails loading.

Re: LoadJSON allow comments

Posted: Wed Feb 21, 2024 1:16 pm
by jacdelad
Fred wrote: Wed Feb 21, 2024 9:39 am I didn't wrote the lib, but Freak did :D. So yes it's a nice hand tailored json lib specifically for PB. It's for XML where we use xerces.
Oh, I didn't know that. Good work freak! :D

Re: LoadJSON allow comments

Posted: Wed Feb 21, 2024 6:01 pm
by DarkDragon

Re: LoadJSON allow comments

Posted: Wed Feb 21, 2024 8:13 pm
by jacdelad
DarkDragon wrote: Wed Feb 21, 2024 6:01 pm https://www.json.org/json-en.html
Nice, PureBasic is even mentioned on the site.

Re: LoadJSON allow comments

Posted: Thu Feb 22, 2024 4:25 am
by Rinzwind
DarkDragon wrote: Wed Feb 21, 2024 6:01 pm JSON doesn't support comments.

See also
https://stackoverflow.com/questions/244 ... ed-in-json
https://www.json.org/json-en.html
Yes, I said that already in the first post; and? In practice it does and many disagree with that design (hence jsonc is a thing which is nothing more than giving a name to the practice of adding comments). As can also be seen in any project (VS Code being a big one) using it for editable configuration files because to use it for manual editing, you'll need comments for the end-user. So please let the parser ignore them at the very least so I can read those json files without issues with LoadJSON.

Re: LoadJSON allow comments

Posted: Thu Feb 22, 2024 5:59 am
by DarkDragon
Rinzwind wrote: Thu Feb 22, 2024 4:25 am
DarkDragon wrote: Wed Feb 21, 2024 6:01 pm JSON doesn't support comments.

See also
https://stackoverflow.com/questions/244 ... ed-in-json
https://www.json.org/json-en.html
Yes, I said that already in the first post; and? In practice it does and many disagree with that design (hence jsonc is a thing which is nothing more than giving a name to the practice of adding comments). As can also be seen in any project (VS Code being a big one) using it for editable configuration files because to use it for manual editing, you'll need comments for the end-user. So please let the parser ignore them at the very least so I can read those json files without issues with LoadJSON.
While I agree that comments in JSON should be allowed,
someone will eventually come and say "I got this JSON which should be invalid". Even in other languages JSON parsers are strict regarding comments and if you support loading comments and not writing people will complain about inconsistency. However as you said there is JSONC and we could have LoadJSONC next to LoadJSON.

Re: LoadJSON allow comments

Posted: Thu Feb 22, 2024 1:58 pm
by tored
Untested, zero error handling and unoptimized.

jsonc.txt

Code: Select all

{
    /*
     * Sweet section
     */
    "fruit": "Watermelon", // Yes, watermelons is sweet!
    "dessert": /* Yummy! */ "Cheesecake",
    // And finally
    "drink": "Milkshake - /* strawberry */ or // chocolate!" // Mmm...
}

Code: Select all

EnableExplicit

Procedure.s StripJsonc(jsonc.s)
  #CHAR_ASTERISK = 42
  #CHAR_DQOUTES = 34
  #CHAR_SLASH = 47
  #CHAR_BACKSLASH = 92
  
  #SINGLE_COMMENT = 1
  #MULTI_COMMENT = 2
  
  Protected *buffer.Character, *begin, *current.Character, *next.Character, *tmp.Character, inComment, inString, json.s, count  

  *buffer.Character = AllocateMemory(StringByteLength(jsonc), #PB_Memory_NoClear)
  *begin = *buffer
  *current.Character = @jsonc
  *next.Character = *current + SizeOf(Character)
  inComment = #False
  inString = #False
  
  While *current\c
    If Not inComment And *current\c = #CHAR_DQOUTES
      count = 0
      *tmp = *current - SizeOf(Character)
      
      While *tmp\c = #CHAR_BACKSLASH
        count + 1
        *tmp - SizeOf(Character)
      Wend  
      
      If Not Bool(count % 2)
        inString = Bool(Not inString)  
      EndIf
    EndIf  
    
    If inString
      Goto copy
    EndIf
    
    If Not inComment And *current\c = #CHAR_SLASH And *next\c = #CHAR_SLASH
      inComment = #SINGLE_COMMENT
      *current + SizeOf(Character)
      Goto loop
    ElseIf inComment = #SINGLE_COMMENT And *current\c = #CR And *next\c = #LF
      inComment = #False
    ElseIf inComment = #SINGLE_COMMENT And *current\c = #LF
      inComment = #False
    ElseIf Not inComment And *current\c = #CHAR_SLASH And *next\c = #CHAR_ASTERISK
      inComment = #MULTI_COMMENT
      *current + SizeOf(Character)
      Goto loop
    ElseIf inComment = #MULTI_COMMENT And *current\c = #CHAR_ASTERISK And *next\c = #CHAR_SLASH
      inComment = #False
      *current + SizeOf(Character)
      Goto loop
    EndIf  
    
    If Not inComment  
      copy:
      *buffer\c = *current\c
      *buffer + SizeOf(Character)  
    EndIf
    
    loop:
    *current + SizeOf(Character)
    *next = *current + SizeOf(Character)
  Wend
  json = PeekS(*begin)
  FreeMemory(*begin)
  ProcedureReturn json
EndProcedure


DataSection
  DataStart:
  IncludeBinary "jsonc.txt"
  DataEnd:
EndDataSection


Define jsonc.s = PeekS(?DataStart,?DataEnd-?DataStart,#PB_UTF8)
Define json.s = StripJsonc(jsonc)
If Not ParseJSON(0, json)    
  Debug JSONErrorMessage()
  End 1
EndIf  

Debug ComposeJSON(0, #PB_JSON_PrettyPrint)
FreeJSON(0)
Edit: This was a port of this MIT library https://github.com/sindresorhus/strip-json-comments

It was the most straight forward implantation I could find without spending too much time on it.

Re: LoadJSON allow comments

Posted: Fri Feb 23, 2024 10:24 pm
by infratec
My version of LoadJSONC():

No Peek, no Poke, no Goto, no extra memory

https://www.purebasic.fr/english/viewtopic.php?t=83647

Re: LoadJSON allow comments

Posted: Mon Feb 26, 2024 1:47 pm
by tored
infratec wrote: Fri Feb 23, 2024 10:24 pm My version of LoadJSONC():

No Peek, no Poke, no Goto, no extra memory

https://www.purebasic.fr/english/viewtopic.php?t=83647
Great without the unnecessary memory allocation.