JSON encoder and decoder
Re: JSON encoder and decoder
Hi PMV,
thank you for the reply. But you missunderstood. I need to get all "Test n" entries. How to get the number of entries? In your example, you directly access them using the known name:
*out\o("Test 1")\o("url")\s
But I need to parse this with different (unknown) names. I try'd to find out, how to get the entries in the list:
Test 1
Test 2
Test 3
If I know them, I can parse their individual values (url, logo etc.).
As I don't know anything abou the names, I need some sort of loop through the entries. And I can not find a way to get them...
Kukulkan
thank you for the reply. But you missunderstood. I need to get all "Test n" entries. How to get the number of entries? In your example, you directly access them using the known name:
*out\o("Test 1")\o("url")\s
But I need to parse this with different (unknown) names. I try'd to find out, how to get the entries in the list:
Test 1
Test 2
Test 3
If I know them, I can parse their individual values (url, logo etc.).
As I don't know anything abou the names, I need some sort of loop through the entries. And I can not find a way to get them...
Kukulkan
Re: JSON encoder and decoder
This are normal maps, so you just need to look at the Map-Library
(code directly written down in here
)
MFG PMV

Code: Select all
Debug MapSize(*out\o())
ResetMap(*out\o())
While NextMapElement(*out\o())
Debug MapKey(*out\o())
Debug *out\o()\o("url")\s
Wend

MFG PMV
Re: JSON encoder and decoder
Hi PMV,
can you help me parse this json.txt
I try:
But getting error: "[ERROR] Invalid memory access. (read error at adress
". (PB 4,60)
I checked json format on json.parser.online.fr, and it`s valid.
can you help me parse this json.txt
I try:
Code: Select all
IncludeFile "JSON_Parser.pbi"
Define File.i = OpenFile(#PB_Any, "JSON.txt")
Define String.s = ""
Define Format.i = ReadStringFormat(File)
While Eof(File) = #False
String + ReadString(File, Format) + Chr(13) + Chr(10)
Wend
CloseFile(File)
;Debug String
Define *out.jsonObj = JSON_decode(String)
Debug "repair = " + Str(*out\a(0)\o("repair")\i)

I checked json format on json.parser.online.fr, and it`s valid.
Re: JSON encoder and decoder
update 05.04.2012
+ bugfix: JSON_encode() returned #False, if a number was followed by square bracket "]"
what a stupid bug ...
Now your JSON works
MFG PMV
+ bugfix: JSON_encode() returned #False, if a number was followed by square bracket "]"
what a stupid bug ...

Now your JSON works

MFG PMV
Re: JSON encoder and decoder
thank you PMV
you have made excellent parser

you have made excellent parser
Re: JSON encoder and decoder
Thanks PMV. Great include!
It's a shame that PB is not supporting JSON by default...
Kukulkan

It's a shame that PB is not supporting JSON by default...

Kukulkan
- mariosk8s
- Enthusiast
- Posts: 103
- Joined: Wed Apr 06, 2011 11:37 am
- Location: Hüfingen, Germany
- Contact:
Re: JSON encoder and decoder
Is there a way to reuse the same JSON object for multiple encoding purposes?
This one crashes (map not initialized) on the 2nd call to myJSON\o("foo")\s = "bar"
Or am i just doing something wrong?
Code: Select all
XIncludeFile "Json.pbi"
Define myJSON.jsonObj
myJSON\o("foo")\s = "bar"
Debug JSON_encode(@myJSON)
JSON_clear(@myJSON)
myJSON\o("foo")\s = "bar"
Debug JSON_encode(@myJSON)
Or am i just doing something wrong?
Re: JSON encoder and decoder
Because JSON_clear() will call ClearStructure(), you jave to call
InitializeStructure() bevor using it again.
MFG PMV
InitializeStructure() bevor using it again.
MFG PMV
- mariosk8s
- Enthusiast
- Posts: 103
- Joined: Wed Apr 06, 2011 11:37 am
- Location: Hüfingen, Germany
- Contact:
Re: JSON encoder and decoder
Ah, thanks.
Good to know.
Good to know.
Re: JSON encoder and decoder
update 17.07.2012
+ add: JSON_freeStringBuffer() to be used if JSON_encode() has created very big strings and after that you dosn't need this function any longer.
+ add: additional parameter "initialize" for JSON_clear(), can set to #False to disable automatic initialization of the given object (behavior as before).
+ improved: memory-managmend for JSON_encode(), results in a much more faster then before
+ improved: JSON_clear() is calling InitializeStructure() automatically, to make the given object usable again after call
Sorry, long time since then, i have totally forgotten about that.
I don't know how fast it is, but it is a few times faster i think.
The additional parameter is because of the problem above. Old
code needs to be updated only a little bit. But for beginners
it should be much easier to handle this.
MFG PMV
+ add: JSON_freeStringBuffer() to be used if JSON_encode() has created very big strings and after that you dosn't need this function any longer.
+ add: additional parameter "initialize" for JSON_clear(), can set to #False to disable automatic initialization of the given object (behavior as before).
+ improved: memory-managmend for JSON_encode(), results in a much more faster then before
+ improved: JSON_clear() is calling InitializeStructure() automatically, to make the given object usable again after call
Sorry, long time since then, i have totally forgotten about that.
I don't know how fast it is, but it is a few times faster i think.
The additional parameter is because of the problem above. Old
code needs to be updated only a little bit. But for beginners
it should be much easier to handle this.

MFG PMV
Re: JSON encoder and decoder
Thanks PMV. Very good work!
Re: JSON encoder and decoder
This is just fantastic and exactly what i needed to get my php arrays into PB, Thank You 

- mariosk8s
- Enthusiast
- Posts: 103
- Joined: Wed Apr 06, 2011 11:37 am
- Location: Hüfingen, Germany
- Contact:
Re: JSON encoder and decoder
PMV, if i'm not mistaken, then this should fix your unicode decoding.
It suffered from an off by 1.
It suffered from an off by 1.
Code: Select all
--- a/Json.pb 2013-02-19 17:08:17.716628088 +0100
+++ b/Json.pb 2013-02-19 17:14:05.309259709 +0100
@@ -253,11 +253,12 @@
Case 't' ; tabulator
string + Chr(9)
Case 'u' ; 4 hex digits
+ *c + SizeOf(CHARACTER) ; eat the 'u'
If nullByte - *c > 4 * SizeOf(CHARACTER)
hexDigit = "$"
;For i = 1 To 4
hexDigit + PeekS(*c, 4)
- *c + SizeOf(CHARACTER) * 4
+ *c + SizeOf(CHARACTER) * 3 ; only 3 the 4th eaten below
;Next
string + Chr(Val(hexDigit))
Else
Last edited by mariosk8s on Wed Feb 20, 2013 3:43 pm, edited 1 time in total.
- mariosk8s
- Enthusiast
- Posts: 103
- Joined: Wed Apr 06, 2011 11:37 am
- Location: Hüfingen, Germany
- Contact:
Re: JSON encoder and decoder
I was slightly mistaken. I misread the PeekS doc to understand length to be bytes not characters.
I updated the patch to address this.

I updated the patch to address this.
Re: JSON encoder and decoder
Oh, thats true ... peeking the u-symbol isn't really clever here.
... after so long time, there are still bugs.
Thanks for finding it.

... after so long time, there are still bugs.

Thanks for finding it.