Le 27/04/2013 10:53, Tommi Mäkitalo a écrit :
> 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
Hello Tommi,
I receive some json data from Perl scripts. The content is very dynamic.
>From the previous example, I can have the b variable, but not always. If
b exists, I need to get it as json string to send it to another process.
I did not want to create a C++ class for each json string because I
don't know all variables and if they exists when I receive the json
data. I wrote this code to solve my problem. Can you give me your advice ?
#include <iostream>
#include <fstream>
#include <cxxtools/jsonserializer.h>
#include <cxxtools/jsondeserializer.h>
void foo(const std::string& s)
{
std::stringstream is(s);
cxxtools::JsonDeserializer deserializer(is);
cxxtools::SerializationInfo si;
deserializer.deserialize(si);
// If b exists ...
const cxxtools::SerializationInfo* p;
p = si.findMember("b");
if ((p != 0) && (!p->isNull())) {
cxxtools::SerializationInfo si_b;
si.getMember("b") >>= si_b;
std::stringstream os;
cxxtools::JsonSerializer serializer(os);
serializer.serialize(si_b).finish();
std::string b = os.str();
// Send b (as string) to another process (not C++)
std::cout << "b: " << b << std::endl;
}
else
std::cout << "b do not exist" << std::endl;
// Test if e exists ...
p = si.findMember("e");
if ((p == 0) || (p->isNull()))
std::cout << "e do not exists" << std::endl;
else
std::cout << "e exists" << std::endl;
}
int main()
{
std::string s1("{ \"a\": 10, \"b\": { \"c\": \"AZERTY\", \"d\":
\"QWERTY\" } }");
std::string s2("{ \"a\": 10, \"e\": \"e is here :)\" }");
foo(s1);
std::cout << std::endl;
foo(s2);
}
Thanks,
Jean-Michel
------------------------------------------------------------------------------
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