JSON_Spirit: mapping value.type() back to the type?

1.1k Views Asked by At

You can display a Value's type like this:

cout << val.type() << end;

and it print a number.

How can I map this number back to the actual type?

besides peeking in the header file, of course, which reveals all...

enum Value_type {
    obj_type,array_type,str_type,bool_type,int_type,real_type,null_type
};
2

There are 2 best solutions below

0
On BEST ANSWER

Nope, that seems to be the canonical way:

    switch(v.type()) {
        case obj_type:    pp_obj(v, lev+1);   break;
        case array_type:  pp_array(v, lev+1); break;
        case str_type:    pp<string>(v, lev+1);   break;
        case bool_type:   pp<bool>(v, lev+1);  break;
        case int_type:    pp<int>(v, lev+1);   break;
        case real_type:   pp<double>(v, lev+1);  break;
        case null_type:   pp_null(v, lev+1);  break;
    }
2
On

Value val; read(is, val); Object o = val.get_obj();

Then create a Pair, assuming it's type 0. Pair pair = o[1];

where 1 is the iterated value. This took me forever to figure out, so I'm trying to save the time of anyone else who needs to look this up later. use sizeof(o)/sizeof(int) to iterate, along with the ++i instead of i++.