json

BlitzMax Forums/BlitzMax Programming/json

Jaydubeww(Posted 2015) [#1]
I'm working with the twrc.json module:
https://github.com/Trylobot/bmx-rjson
as it seems to be the most complete for max. The other modules don't like negative numbers or scientific notation. I would link to them but I don't have time at the moment. My issue is probably a simple fix but I cant figure it out.

With the twrc module I can generate a json string from a class, but I can't seem to generate a class back from it. Here's the code:

<code>
SuperStrict

Import twrc.rjson

Global lev:level = New level
lev.x = 10
lev.y = 20
lev.name = "my fist level"

Global levelstr:String = json.stringify(lev)

Print levelstr

Global back:level = New level
back = json.parse(levelstr, "level")

Global back:Object = json.parse(levelstr, "level")

'Print back.x.ToString()
'Print back.y
'Print back.name

Type level
Field x:Int
Field y:Int
Field name:String
End Type
</code>

Of course the error is "cannot convert from 'object' to 'level'. How am I supposed to go about this and get the json back into a class?

Thanks.


Henri(Posted 2015) [#2]
Hi,

at first glance it seems you are declaring variable 'back' two times, but I think the ultimate problem is type casting.
Parse method is probably returning a object type 'level' so variable must be declared as 'level' and then use type casting like this:
Global back:level = level(json.parse(levelstr, "level"))

-Henri


Jaydubeww(Posted 2015) [#3]
Ah yes thanks. I could not remember how type casting worked in bmax (silly me!). Also, you are right, the second declaration was supposed to be commented out before posting the code!

Thanks again


Hardcoal(Posted 2015) [#4]
Im i the only one that this module name reminds him Friday the 13