JSON encoder and decoder

Share your advanced PureBasic knowledge/code with the community.
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: JSON encoder and decoder

Post 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
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: JSON encoder and decoder

Post 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
kosjachok
New User
New User
Posts: 5
Joined: Thu Feb 17, 2011 11:08 pm

Re: JSON encoder and decoder

Post 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.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: JSON encoder and decoder

Post 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
kosjachok
New User
New User
Posts: 5
Joined: Thu Feb 17, 2011 11:08 pm

Re: JSON encoder and decoder

Post by kosjachok »

thank you PMV :D
you have made excellent parser
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: JSON encoder and decoder

Post by Kukulkan »

Thanks PMV. Great include! :D

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

Kukulkan
User avatar
mariosk8s
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Apr 06, 2011 11:37 am
Location: Hüfingen, Germany
Contact:

Re: JSON encoder and decoder

Post 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?
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: JSON encoder and decoder

Post by PMV »

Because JSON_clear() will call ClearStructure(), you jave to call
InitializeStructure() bevor using it again.

MFG PMV
User avatar
mariosk8s
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Apr 06, 2011 11:37 am
Location: Hüfingen, Germany
Contact:

Re: JSON encoder and decoder

Post by mariosk8s »

Ah, thanks.
Good to know.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: JSON encoder and decoder

Post 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
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: JSON encoder and decoder

Post by Kukulkan »

Thanks PMV. Very good work!
MicroByte
New User
New User
Posts: 8
Joined: Tue May 08, 2012 11:06 pm

Re: JSON encoder and decoder

Post by MicroByte »

This is just fantastic and exactly what i needed to get my php arrays into PB, Thank You :)
User avatar
mariosk8s
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Apr 06, 2011 11:37 am
Location: Hüfingen, Germany
Contact:

Re: JSON encoder and decoder

Post 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                                                                                                        
Last edited by mariosk8s on Wed Feb 20, 2013 3:43 pm, edited 1 time in total.
User avatar
mariosk8s
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Apr 06, 2011 11:37 am
Location: Hüfingen, Germany
Contact:

Re: JSON encoder and decoder

Post 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.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: JSON encoder and decoder

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