Unicode in TinyJSON Thomas 31st March, 2008 08:23 (UTC)
Parametrization seems to be the right way to do it! Thanks for the great post - and the valuable tips..
Unicode in TinyJSON Kirit Sælensminde 31st March, 2008 08:44 (UTC)
Thomas said

Parametrization seems to be the right way to do it! Thanks for the great post - and the valuable tips..

Glad that it was useful to you.

I'm not sure if this will sway you on switching to Boost.Variant, but here's what the traversals might look like:

namespace {
    json_string string_to_json( const wstring &s ) {
        return L"\"" + replaceAll( s, L"\"", L"\\\"" ) + L"\"";
    }
    struct atom_to_json : public boost::static_visitor< json_string > {
        json_string operator()( t_null ) const {
            return L"null";
        }
        json_string operator()( const wstring &s ) const {
            return string_to_json( s );
        }
        template< typename T >
        json_string operator()( T i ) const {
            return FSLib::coerce< wstring >( i );
        }
    };
    struct to_json : public boost::static_visitor< json_string > {
        json_string operator()( const Json::atom_t &t ) const {
            return boost::apply_visitor( ::atom_to_json(), t );
        }
        json_string operator()( const Json::array_t &t ) const {
            std::wostringstream s;
            for ( Json::array_t::const_iterator i( t.begin() ); i != t.end(); ++i )
                s << ( i == t.begin() ? wstring() : L"," ) << toJson( **i );
            return L"[" + s.str() + L"]";
        }
        json_string operator()( const Json::object_t &t ) const {
            std::wostringstream s;
            for ( Json::object_t::const_iterator i( t.begin() ); i != t.end(); ++i )
                s << ( i == t.begin() ? wstring() : L"," ) << string_to_json( i->first ) << L":" << toJson( *i->second );
            return L"{" + s.str() + L"}";
        }
    };
}
json_string FSLib::toJson( const Json &json ) {
    return boost::apply_visitor( ::to_json(), json );
}

The string mangling isn't quite up to production quality — it probably ought to do something explicit about control characters and throw for those that aren't supported. I think the call to coerce can be replaced with Boost's lexical cast.

I think the big advantage here is that there are no type holes. You are forced to address each type within the variants or it won't compile and you also know you won't get any surprises at run time.

It does however imply a widening of scope of TinyJSON to a full JSON library rather than just parsing. Maybe that's Ok for version 2 though? :)


To join in the discussion you should register or log in
Unicode in TinyJSON Thomas 31st March, 2008 08:54 (UTC)
Kirit Sælensminde said

I think the big advantage here is that there are no type holes. You are forced to address each type within the variants or it won't compile and you also know you won't get any surprises at run time.

Definitely! As I wrote in my blog, the reason to use boost::any was lazyness ;) .. But I will change it to boost::variant in the next version.

Kirit Sælensminde said

It does however imply a widening of scope of TinyJSON to a full JSON library rather than just parsing. Maybe that's Ok for version 2 though? :)

I have to think about it ;)

Unicode in TinyJSON Kirit Sælensminde 31st March, 2008 09:03 (UTC)

I should have also mentioned that I can give you some Unicode encoding/decoding code to use or to look at if it'll help.

I can't make any g'tees about its portability, at least not until I've been through and ported it at least one other platform anyway.


To join in the discussion you should register or log in