LoadJSON allow comments

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

LoadJSON allow comments

Post 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).
User avatar
jacdelad
Addict
Addict
Posts: 1472
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: LoadJSON allow comments

Post 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.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Fred
Administrator
Administrator
Posts: 16662
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: LoadJSON allow comments

Post 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.
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: LoadJSON allow comments

Post by Little John »

Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: LoadJSON allow comments

Post by Rinzwind »

Would be nice if LoadJSON just ignores comments. As current state, it just returns false and fails loading.
User avatar
jacdelad
Addict
Addict
Posts: 1472
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: LoadJSON allow comments

Post 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
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
DarkDragon
Addict
Addict
Posts: 2228
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: LoadJSON allow comments

Post by DarkDragon »

bye,
Daniel
User avatar
jacdelad
Addict
Addict
Posts: 1472
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: LoadJSON allow comments

Post 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.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Rinzwind
Enthusiast
Enthusiast
Posts: 638
Joined: Wed Mar 11, 2009 4:06 pm
Location: NL

Re: LoadJSON allow comments

Post 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.
DarkDragon
Addict
Addict
Posts: 2228
Joined: Mon Jun 02, 2003 9:16 am
Location: Germany
Contact:

Re: LoadJSON allow comments

Post 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.
bye,
Daniel
tored
User
User
Posts: 61
Joined: Wed Feb 16, 2022 12:47 pm
Location: Sweden

Re: LoadJSON allow comments

Post 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.
infratec
Always Here
Always Here
Posts: 6864
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: LoadJSON allow comments

Post by infratec »

My version of LoadJSONC():

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

https://www.purebasic.fr/english/viewtopic.php?t=83647
tored
User
User
Posts: 61
Joined: Wed Feb 16, 2022 12:47 pm
Location: Sweden

Re: LoadJSON allow comments

Post 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.
Post Reply