Am 11.11.2013 20:27, schrieb Michael Gius:
Hi List,

below code allows me to deserialize json like this:
    {
        "d_a": 24.25,
        "d_b": 25.5,
        "d_c": 23.2
    }
but can I also use cxxtools to deserialize the following json array into a vector of Mystruct (size unknown):

    [
        {
            "d_a": 24.25,
            "d_b": 25.5,
            "d_c": 23.2
        },
        {
            "d_a": 24.5,
            "d_b": 25.5,
            "d_c": 23.2
        }
        //more objects of the same type can follow
    ]


Thanks in advance for any pointers.
Best regards,
Michael




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

struct Mystruct {
    double d_a;
    double d_b;
    double d_c;
};


void operator<<= (cxxtools::SerializationInfo& si, const Mystruct& mystruct)
{
    si.addMember("d_a") <<= mystruct.d_a;
    si.addMember("d_b") <<= mystruct.d_b;
    si.addMember("d_c") <<= mystruct.d_c;
}

void operator>>= (const cxxtools::SerializationInfo& si, Mystruct& mystruct)
{
    si.getMember("d_a") >>= mystruct.d_a;
    si.getMember("d_b") >>= mystruct.d_b;
    si.getMember("d_c") >>= mystruct.d_c;
}

int main(int argc, char* argv[])
{
    try
    {
        // define an empty mystruct object
        Mystruct vmystruct;

        // read json mystruct struct from stdin:
        cxxtools::JsonDeserializer deserializer(std::cin);
        deserializer.deserialize(vmystruct);

        //for (unsigned n = 0; n < vmystruct.size(); ++n)
            std::cout << vmystruct.d_a << ' ';
    }
    catch (const std::exception& e)
    {
        std::cerr << e.what() << std::endl;
    }
}

just deserialize a std::vector of the struct:

   int main(int argc, char* argv[])
   {
        try
        {
            std::vector<Mystruct> vmystruct;

            // read json mystruct struct from stdin:
            cxxtools::JsonDeserializer deserializer(std::cin);
            deserializer.deserialize(vmystruct);

            for (unsigned n = 0; n < vmystruct.size(); ++n)
                std::cout << vmystruct[n].d_a << ' ';
        }
        catch (const std::exception& e)
        {
            std::cerr << e.what() << std::endl;
        }
   }

There is a predefined deserialization operator for std::vector<T> for each type T, which is deserializable. You can also deserialize e.g. a std::vector<std::pair<std::string, std::map<int, Mystruct> > > or some other complex structure if you want.

Tommi
------------------------------------------------------------------------------
November Webinars for C, C++, Fortran Developers
Accelerate application performance with scalable programming models. Explore
techniques for threading, error checking, porting, and tuning. Get the most 
from the latest Intel processors and coprocessors. See abstracts and register
http://pubads.g.doubleclick.net/gampad/clk?id=60136231&iu=/4140/ostg.clktrk
_______________________________________________
Tntnet-general mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tntnet-general

Reply via email to