Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> As of today, all json parsers rely dynamic data structures to parse and store the json objects.

I'm not sure that's entirely fair. Callback-based parsers like YAJL leave the application free to store the data in whatever data structure they want, or even to stream-process the input without storing in a data structure at all.

But regardless, the meta-programming approach described here is interesting and novel. Generating structure-specific parsing code is a well-explored area (for example, Protocol Buffers is designed entirely around this idea), but doing it as C++ metaprogramming is a novel approach (Protocol Buffers relies on compile-time code generation).

I don't actually understand how the object inspection and compile-time codegen works with this meta-programming approach; will be interesting to dig in a little deeper and learn more.



I've used Boost Fusion associative maps, in conjunction with a bit of glue code and an appropriate JSON or XML library, to provide pretty seemless serialisation and de-serialisation for both XML and JSON for a while. For XML it's a little tricky because you have to have a means of mapping the simple flat relationship between a struct and its fields, to the various relationships between XML elements, subelements, attributes, and CDATA etc. Here's an example of what my declarations for XML look like in some code I wrote last year.

    BOOST_FUSION_ADAPT_ADT (
        xml_encoded<Description>,
        XML_ATTR        (string, "summary")
        XML_TEXT        (string)
    )

    BOOST_FUSION_ADAPT_ADT (
        xml_encoded<Event>,
        XML_ATTR        (string, "name")
        XML_SUBTREE     (shared_ptr<Description const>, "description")
        XML_ATTR        (optional<unsigned>, "since")
    )
The #defines for each macro are short, and beyond Fusion there's only a small support header

Here's a talk from CppConn describing a similar use case, but for binary data formats https://www.youtube.com/watch?v=wbZdZKpUVeg


The introspection comes from the SIO's of the iod library, they are novel types of object offering introspection at compile time. The iod library makes iterating on the members of an sio object trivial with the foreach primitive (see https://github.com/matt-42/iod/blob/master/iod/json.hh#L417) so generating a serializer / deserializer like this one is not so complicated since you have access to the type and the name of each members. For more info, check the readme of https://github.com/matt-42/iod I did my best to explain the concepts of the library. But I'm still lacking of time to polish the documentation....


Not that it's well-known or anything, but the Gin library that's part of Chromium uses C++ meta-programming to generate structure-specific code to 'parse' JavaScript types into C++ types:

https://code.google.com/p/chromium/codesearch#chromium/src/g...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: