Page 2 of 4

Re: JSON encoder and decoder

Posted: Tue Oct 25, 2011 6:27 am
by Kukulkan
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

Re: JSON encoder and decoder

Posted: Tue Oct 25, 2011 12:17 pm
by PMV
This are normal maps, so you just need to look at the Map-Library :wink:

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
(code directly written down in here :) )

MFG PMV

Re: JSON encoder and decoder

Posted: Wed Apr 04, 2012 9:33 pm
by kosjachok
Hi PMV,
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)
But getting error: "[ERROR] Invalid memory access. (read error at adress 8)". (PB 4,60)
I checked json format on json.parser.online.fr, and it`s valid.

Re: JSON encoder and decoder

Posted: Thu Apr 05, 2012 8:41 pm
by PMV
update 05.04.2012
+ bugfix: JSON_encode() returned #False, if a number was followed by square bracket "]"


what a stupid bug ... :oops:
Now your JSON works :wink:

MFG PMV

Re: JSON encoder and decoder

Posted: Thu Apr 05, 2012 8:52 pm
by kosjachok
thank you PMV :D
you have made excellent parser

Re: JSON encoder and decoder

Posted: Fri Apr 06, 2012 1:58 pm
by Kukulkan
Thanks PMV. Great include! :D

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

Kukulkan

Re: JSON encoder and decoder

Posted: Tue May 22, 2012 1:34 pm
by mariosk8s
Is there a way to reuse the same JSON object for multiple encoding purposes?

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)
This one crashes (map not initialized) on the 2nd call to myJSON\o("foo")\s = "bar"
Or am i just doing something wrong?

Re: JSON encoder and decoder

Posted: Wed May 23, 2012 12:14 am
by PMV
Because JSON_clear() will call ClearStructure(), you jave to call
InitializeStructure() bevor using it again.

MFG PMV

Re: JSON encoder and decoder

Posted: Wed May 23, 2012 7:41 am
by mariosk8s
Ah, thanks.
Good to know.

Re: JSON encoder and decoder

Posted: Tue Nov 13, 2012 11:48 pm
by PMV
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

Re: JSON encoder and decoder

Posted: Wed Nov 14, 2012 7:26 am
by Kukulkan
Thanks PMV. Very good work!

Re: JSON encoder and decoder

Posted: Fri Nov 16, 2012 2:32 am
by MicroByte
This is just fantastic and exactly what i needed to get my php arrays into PB, Thank You :)

Re: JSON encoder and decoder

Posted: Tue Feb 19, 2013 5:20 pm
by mariosk8s
PMV, if i'm not mistaken, then this should fix your unicode decoding.
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                                                                                                        

Re: JSON encoder and decoder

Posted: Wed Feb 20, 2013 3:42 pm
by mariosk8s
I was slightly mistaken. I misread the PeekS doc to understand length to be bytes not characters. :oops:

I updated the patch to address this.

Re: JSON encoder and decoder

Posted: Wed Feb 20, 2013 5:07 pm
by PMV
Oh, thats true ... peeking the u-symbol isn't really clever here. :lol:
... after so long time, there are still bugs. :x

Thanks for finding it.