@@ -140,6 +140,7 @@ void datum_t::init_json(cJSON *json) {
140140 case cJSON_String: {
141141 init_str ();
142142 *r_str = json->valuestring ;
143+ check_str_validity (*r_str);
143144 } break ;
144145 case cJSON_Array: {
145146 init_array ();
@@ -935,6 +936,10 @@ void datum_t::init_from_pb(const Datum *d) {
935936 *r_str = d->r_str ();
936937 check_str_validity (*r_str);
937938 } break ;
939+ case Datum::R_JSON : {
940+ scoped_cJSON_t cjson (cJSON_Parse (d->r_str ().c_str ()));
941+ init_json (cjson.get ());
942+ } break ;
938943 case Datum::R_ARRAY : {
939944 init_array ();
940945 for (int i = 0 ; i < d->r_array_size (); ++i) {
@@ -979,43 +984,51 @@ bool datum_t::key_is_truncated(const store_key_t &key) {
979984 }
980985}
981986
982- void datum_t::write_to_protobuf (Datum *d) const {
983- switch (get_type ()) {
984- case R_NULL : {
985- d->set_type (Datum::R_NULL );
986- } break ;
987- case R_BOOL : {
988- d->set_type (Datum::R_BOOL );
989- d->set_r_bool (r_bool);
990- } break ;
991- case R_NUM : {
992- d->set_type (Datum::R_NUM );
993- // so we can use `isfinite` in a GCC 4.4.3-compatible way
994- using namespace std ; // NOLINT(build/namespaces)
995- r_sanity_check (isfinite (r_num));
996- d->set_r_num (r_num);
997- } break ;
998- case R_STR : {
999- d->set_type (Datum::R_STR );
1000- d->set_r_str (*r_str);
1001- } break ;
1002- case R_ARRAY : {
1003- d->set_type (Datum::R_ARRAY );
1004- for (size_t i = 0 ; i < r_array->size (); ++i) {
1005- (*r_array)[i]->write_to_protobuf (d->add_r_array ());
987+ void datum_t::write_to_protobuf (Datum *d, use_json_t use_json) const {
988+ switch (use_json) {
989+ case use_json_t ::NO : {
990+ switch (get_type ()) {
991+ case R_NULL : {
992+ d->set_type (Datum::R_NULL );
993+ } break ;
994+ case R_BOOL : {
995+ d->set_type (Datum::R_BOOL );
996+ d->set_r_bool (r_bool);
997+ } break ;
998+ case R_NUM : {
999+ d->set_type (Datum::R_NUM );
1000+ // so we can use `isfinite` in a GCC 4.4.3-compatible way
1001+ using namespace std ; // NOLINT(build/namespaces)
1002+ r_sanity_check (isfinite (r_num));
1003+ d->set_r_num (r_num);
1004+ } break ;
1005+ case R_STR : {
1006+ d->set_type (Datum::R_STR );
1007+ d->set_r_str (*r_str);
1008+ } break ;
1009+ case R_ARRAY : {
1010+ d->set_type (Datum::R_ARRAY );
1011+ for (size_t i = 0 ; i < r_array->size (); ++i) {
1012+ (*r_array)[i]->write_to_protobuf (d->add_r_array (), use_json);
1013+ }
1014+ } break ;
1015+ case R_OBJECT : {
1016+ d->set_type (Datum::R_OBJECT );
1017+ // We use rbegin and rend so that things print the way we expect.
1018+ for (auto it = r_object->rbegin (); it != r_object->rend (); ++it) {
1019+ Datum_AssocPair *ap = d->add_r_object ();
1020+ ap->set_key (it->first );
1021+ it->second ->write_to_protobuf (ap->mutable_val (), use_json);
1022+ }
1023+ } break ;
1024+ case UNINITIALIZED : // fallthru
1025+ default : unreachable ();
10061026 }
10071027 } break ;
1008- case R_OBJECT : {
1009- d->set_type (Datum::R_OBJECT );
1010- // We use rbegin and rend so that things print the way we expect.
1011- for (std::map<std::string, counted_t <const datum_t > >::const_reverse_iterator
1012- it = r_object->rbegin (); it != r_object->rend (); ++it) {
1013- Datum_AssocPair *ap = d->add_r_object ();
1014- ap->set_key (it->first );
1015- it->second ->write_to_protobuf (ap->mutable_val ());
1016- }
1028+ case use_json_t ::YES : {
1029+ d->set_type (Datum::R_JSON );
1030+ d->set_r_str (as_json ().PrintUnformatted ());
10171031 } break ;
1018- case UNINITIALIZED : // fallthru
10191032 default : unreachable ();
10201033 }
10211034}
@@ -1283,8 +1296,8 @@ void wire_datum_map_t::finalize() {
12831296 r_sanity_check (state == COMPILED );
12841297 while (!map.empty ()) {
12851298 map_pb.push_back (std::make_pair (Datum (), Datum ()));
1286- map.begin ()->first ->write_to_protobuf (&map_pb.back ().first );
1287- map.begin ()->second ->write_to_protobuf (&map_pb.back ().second );
1299+ map.begin ()->first ->write_to_protobuf (&map_pb.back ().first , use_json_t :: NO );
1300+ map.begin ()->second ->write_to_protobuf (&map_pb.back ().second , use_json_t :: NO );
12881301 map.erase (map.begin ());
12891302 }
12901303 state = SERIALIZABLE ;
0 commit comments