Overview
Features
Download
Documentation
Community
Add-Ons & Services

JSON and DynamicStruct

Please post support and help requests here.

JSON and DynamicStruct

Postby slimshader » 10 Jan 2013, 14:07

I am having trouble reconstructing DynamicStruct serialized to JSON. My test code is:

Code: Select all
  std::vector<int> numbers;
  numbers.push_back(1);
  numbers.push_back(2);
  numbers.push_back(3);

  DynamicStruct ds;
  ds["tab"] = numbers;
  Dynamic::Var root;
  root = ds;
  std::string str = root.convert<std::string>();


this yields nice
Code: Select all
{ "tab" : [ 1, 2, 3 ] }
JSON string. Now I wish to reconstruct DynamicStruct back. Is there a way to do it? Nice thing about DynamicStruct / Var is that it holds 'numbers' vector as std::vector<int> as opposed to std::vector<Dynamic::Var> (aka JSON::Array) can it be preserved while reconstructing? I am fairly sure the answer is no but in any case any tips would be helpful.
slimshader
 
Posts: 4
Joined: 10 Jan 2013, 13:40

Re: JSON and DynamicStruct

Postby alex » 10 Jan 2013, 18:34

slimshader wrote:Now I wish to reconstruct DynamicStruct back. Is there a way to do it?

No, but we will gladly look into a proposal for it and accept a contribution if it's sound.
alex
 
Posts: 1044
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: JSON and DynamicStruct

Postby fbraem » 10 Jan 2013, 18:48

You can always write your own handler and use that with JSON::Parser instead of the default handler. It's not possible to do this in a generic way, because the parser never knows which types are used in a JSON array. But if you know where these arrays with all integers are, you can handle it differently in your handler.
Zumuta!, that's the way to do IT!
fbraem
 
Posts: 95
Joined: 11 Aug 2008, 22:47
Location: Belgium

Re: JSON and DynamicStruct

Postby slimshader » 10 Jan 2013, 21:07

fbraem wrote:You can always write your own handler and use that with JSON::Parser instead of the default handler. It's not possible to do this in a generic way, because the parser never knows which types are used in a JSON array. But if you know where these arrays with all integers are, you can handle it differently in your handler.


That is actually what I am trying to do. I guess the best way to serialize data this way would be to also include type marker so that proper type can be instantiated during parsing. But then again that is not how DynamicStruct gets serialized to JSON. My general problem is this:

I have a hierarchy of classes that have a pair of methods:

Code: Select all
void toJson(JSONNode&);
void fromJson(const JSONNode&);


JSONNode is an JSON value representation from libjson library, semantically closest to Poco::JSON::Object. It is being used for both serialization and Memento pattern for example for undo actions (state of object it stored in JSONNode in memory then later state is restored from it when needed). Thing is I never really liked this solution, it works but I never wanted it to be this closely tight to JSON. DynamicStruct is perfect replacement imho as it is generic enough and might as well be "visited" by XML serializer. My code already has Any (more or less Dynamic::Any) and DynamicStruct, but finding it in Poco with JSON support already in just raised my hopes for possibility of finally getting rid of libjson (which is crap).
slimshader
 
Posts: 4
Joined: 10 Jan 2013, 13:40


Return to Support

Who is online

Users browsing this forum: No registered users and 1 guest