LoadJSON allow comments
LoadJSON allow comments
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
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.
Also, afaik PureBasic uses a lib not created by Fred himself, so you'd have to ask the creator to support it.
Good morning, that's a nice tnetennba!
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: LoadJSON allow comments
I didn't wrote the lib, but Freak did
. So yes it's a nice hand tailored json lib specifically for PB. It's for XML where we use xerces.

-
- Addict
- Posts: 4779
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: LoadJSON allow comments
Would be nice if LoadJSON just ignores comments. As current state, it just returns false and fails loading.
Re: LoadJSON allow comments
Oh, I didn't know that. Good work freak!Fred wrote: Wed Feb 21, 2024 9:39 am I didn't wrote the lib, but Freak did. So yes it's a nice hand tailored json lib specifically for PB. It's for XML where we use xerces.

Good morning, that's a nice tnetennba!
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
-
- Addict
- Posts: 2344
- Joined: Mon Jun 02, 2003 9:16 am
- Location: Germany
- Contact:
Re: LoadJSON allow comments
JSON doesn't support comments.
See also
https://stackoverflow.com/questions/244 ... ed-in-json
https://www.json.org/json-en.html
See also
https://stackoverflow.com/questions/244 ... ed-in-json
https://www.json.org/json-en.html
bye,
Daniel
Daniel
Re: LoadJSON allow comments
Nice, PureBasic is even mentioned on the site.
Good morning, that's a nice tnetennba!
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: LoadJSON allow comments
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 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
-
- Addict
- Posts: 2344
- Joined: Mon Jun 02, 2003 9:16 am
- Location: Germany
- Contact:
Re: LoadJSON allow comments
While I agree that comments in JSON should be allowed,Rinzwind wrote: Thu Feb 22, 2024 4:25 amYes, 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 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
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
Daniel
Re: LoadJSON allow comments
Untested, zero error handling and unoptimized.
jsonc.txt
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.
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)
It was the most straight forward implantation I could find without spending too much time on it.
Re: LoadJSON allow comments
My version of LoadJSONC():
No Peek, no Poke, no Goto, no extra memory
https://www.purebasic.fr/english/viewtopic.php?t=83647
No Peek, no Poke, no Goto, no extra memory
https://www.purebasic.fr/english/viewtopic.php?t=83647
Re: LoadJSON allow comments
Great without the unnecessary memory allocation.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