s11n for JSON
November 25, 2010 on 3:18 pm | In libs11n, software-dev | 1 CommentHello, fans of C++ and JSON! (Everyone else can stop reading here.)
Recently i started another pet project, the nosjob library, which is a C++ library for creating and consuming JSON data. It provides a set of JSON-specified value types, like String, Boolean, Object, and Array, which can be combined to create data structures of near-arbitrary complexity. The library can output such structures to JSON form and parse such structures from JSON input.
One particularly interesting aspect of it is that it contains a bare-bones variant of libs11n with which it can de/serialize near-arbitrary client types from/to JSON.
For example, in its simplest form:
std::map<int,double> map;
for( int i = 0; i < 5; ++i ) {
map[i] = i * 1.1;
}
nosjob::s11n::save( map, std::cout );
(Strangely enough, a std::map actually serializes to a JSON Array, instead of a JSON Object, in order to be able to support map keys of arbitrary type (JSON Object keys are always strings).)
More detailed examples can be found here.
Like libs11n, it uses a two-step serialization process: first serialize the “client data” to a JSON-compatible data structure, then format that JSON data structure to a given output stream (or output iterator). For deserialization, it it parses JSON input (from a stream or input iterator), builds up a tree of JSON-like value objects, and then feeds that data to the client-defined deserialization routine for final processing. The client provides a pair of conversion functors for converting their type to/from JSON-compatible form, and nosjob does the rest.
The serialization support does not rival libs11n’s in terms of flexibility and features, but it’s not supposed to.
If you’re (still) looking for easy-to-use JSON support for your C++ application, then nosjob might be interesting for you.
Happy Hacking!
Bob Ross is The Man!
—– stephan
PS: one might ask why not simply build this support into libs11n? By the time the nosjob code got to a point where i could add an s11n API for it, the effort and architectural overhead of including it into libs11n seemed too high. There are also disparities in the libs11n and JSON data models which would be awkward to consolidate (e.g. in s11n key/value pairs are only used for basic data types, where JSON allows keys to reference arbitrary other data types). Adding the s11n-like API to the library was a trivial, straightforward task, however: less than 50 lines of code, not including the built-in JSON-to/from-Native conversions which the nosjob::s11n bits use as the default de/serialization implementations (i had already written them for other reasons, which is why they don’t count as “effort” for this purpose).
Evangelism: utfcpp
November 9, 2010 on 8:30 pm | In General | 1 CommentHello, fellow hackers!
i just wanted to take a moment to point out an extremely useful little library for handling UTF8 and UTF16 in C++: Nemanja Trifunovic’s utfcpp (http://utfcpp.sourceforge.net/)
utfcpp follows the STL’s iterator model, and this model allows one to decode and encode UTF8 and UTF16 from and to damned near anywhere using a really simple API.
If you have to deal with UTF encoding/decoding in your C++ applications, i can heartily recommend utfcpp. It’s trivial to use, it has a header-only implementation with no external dependencies (except the STL), and it has very liberal licensing conditions (basically “do what you want, but keep this copyright notice intact”).
Happy hacking!
—– stephan beal
Powered by WordPress with Pool theme design by Borja Fernandez.
Entries and comments feeds.
Valid XHTML and CSS. ^Top^