If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Automatically converting JSON response to C++ objects

[ Edited ]

Hi,

 

Imagine I have some JSON response. And I also have some custom classes MyCustomClass1 and MyCustomClass2.

I know we have a JSON parser on BB 10 SDK (JsonDataAccess). I am curious if BlackBerry 10 has a way (maybe a class? or a library?) that also allows to automatically convert this JSON response to objects of my custom classes? (e.g., MyCustomClass1 and MyCustomClass2).

 

I found some library which is supposed to do this: http://fossil.wanderinghorse.net/repos/nosjob/index.cgi/index

(it's description says: "Easy parsing of JSON string data (from arbitrary input iterators) into C++ objects.") -- but I was curious if there is a BlackBerry way to do this? Plus I am not sure yet if I can use this library on BB 10.

 

Thanks for your input.

Please use plain text.
New Contributor
adampigg
Posts: 7
Registered: ‎02-06-2013
My Device: Dev Alpha A
My Carrier: Three UK

Re: Automatically converting JSON response to C++ objects

Have you considered using QJSon?
Please use plain text.
Developer
gdev001
Posts: 163
Registered: ‎01-30-2013
My Device: Blackberry 10 Simulator

Re: Automatically converting JSON response to C++ objects

Please use plain text.
Developer
Enaud
Posts: 61
Registered: ‎02-08-2013
My Device: z10
My Carrier: rogers

Re: Automatically converting JSON response to C++ objects

[ Edited ]

Is QJon available for native? I'm porting a library that uses it but I can't figure out how to resolve this dependancy with what's in the sdk?

 

How do you include it?

 

E.

Please use plain text.
BlackBerry Development Advisor
RSperanza
Posts: 141
Registered: ‎03-08-2012
My Device: Z10
My Carrier: Bell

Re: Automatically converting JSON response to C++ objects

No, there is no automatic way to do that in C / C++.  What you could do is use JsonDataAccess to load the data and traverse the QVariant data returned and as you encounter certain types of data, instantiate the custom classes they map to and ppopulate the classes with data from the QVariant objects.  It will require some work on your part to ensure that there are things like a className attribute to indicate which class this maps to and perhaps you can pass in the object directly to that class so that it can read what it needs directly from the QVariant object, which will make the loading function more lightweight (it then only needs to know what to instantiate from the top level of embedded objects).

 

Hope that helps.

Please use plain text.