Am 25.04.2013 11:47, schrieb Jean-Michel Caricand:
> Hello,
>
> I have this json string : { "a": 10, "b": { "c": "AZERTY", "d": "QWERTY" } }
>
> Without struct or C++ class, I want to get b's value as std::string. I
> tried :
>
> #include <iostream>
> #include <fstream>
> #include <cxxtools/jsondeserializer.h>
>
> int main()
> {
>       std::string s("{ \"a\": 10, \"b\": { \"c\": \"AZERTY\", \"d\":
> \"QWERTY\" } }");
>       std::stringstream is(s);
>       cxxtools::JsonDeserializer deserializer(is);
>
>       cxxtools::SerializationInfo si;
>       deserializer.deserialize(si, "b");
>
>       std::string b1;
>       si.getMember("c") >>= b1;  // ----> OK :)
>
>       std::cout << "b1: " << b1 << std::endl;
>
>       std::string b2;
>       deserializer.deserialize(b2, "b"); // ----> NOT OK, b2 is empty :(
>
>       std::cout << "b2: " << b2 << std::endl;
> }
>
> Is it possible to get b's value as string ?
>
> Thanks,
>
> Jean-Michel
>
Hello again,

let me add some hint. I really suggest not to use SerializationInfo 
directly. It is much easier to use the serialization framework. Here is 
a example based on the code above, how to do it:

#include <iostream>
#include <cxxtools/jsondeserializer.h>
#include <cxxtools/jsonserializer.h>

struct B
{
   std::string c;
   std::string d;
};

void operator>>= (const cxxtools::SerializationInfo& si, B& b)
{
   si.getMember("c") >>= b.c;
   si.getMember("d") >>= b.d;
}

void operator<<= (cxxtools::SerializationInfo& si, const B& b)
{
   si.addMember("c") <<= b.c;
   si.addMember("d") <<= b.d;
}

struct A
{
   int a;
   B b;
};

void operator>>= (const cxxtools::SerializationInfo& si, A& a)
{
   si.getMember("a") >>= a.a;
   si.getMember("b") >>= a.b;
}

void operator<<= (cxxtools::SerializationInfo& si, const A& a)
{
   si.addMember("a") <<= a.a;
   si.addMember("b") <<= a.b;
}

int main()
{
      try
      {
          std::string s("{ \"a\": 10, \"b\": { \"c\": \"AZERTY\", \"d\": 
\"QWERTY\" } }");
          std::istringstream is(s);
          cxxtools::JsonDeserializer deserializer(is);

          A a;
          deserializer.deserialize(a);

          std::cout << "a=" << a.a << "\n"
                    << "b.c=" << a.b.c << "\n"
                    << "b.d=" << a.b.d << "\n";

          std::cout << "json again:\n";

          cxxtools::JsonSerializer serializer(std::cout);
          serializer.beautify(true);
          serializer.serialize(a).finish();
      }
      catch (const std::exception& e)  // always good to catch all 
exceptions
      {
          std::cerr << e.what() << std::endl;
      }
}

You just need to write the deserialization and serialization operators.

I started to write a howto for the serialization framework but I'm not 
finished with it. But that is a nice example for it. Thank you for that.

Tommi

------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service 
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_apr
_______________________________________________
Tntnet-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tntnet-general

Reply via email to