Check dynamic_t type in dynamic_t::to_json().

It's not good that a user faces some assert violation in RapidJson, so now to_json() checks the value before trying to serialize it.
More tests of JSON serialization/parsing.