Issue #5 new

Memory Consumption

Martin Grund
created an issue

The JSON::Value uses protected member variables to store each possible value. This means, that for each instantiation of a JSON::Value, memory for all types must be reserved. I would recommend using a union type for this to limit memory consumption.

Comments (3)

  1. rodr

    I would recommend to use a union and a type in Value. Then in the cast operators, check the type, and if types match, return the value of the corresponding member of the union, else return a default-constructed object for that type, or try to convert to the requested type from the actual type using a switch for all available types. This way, maybe if, e.g., the type is int, and the user requests a string, maybe it could be converted with a stringstream... Of course they could check the actual type stored. This way, the memory usage is reduced, and useful values are returned in most cases. This would resolve also issue #20.

  2. rodr

    example implementation: before anything, define uniou u with all the member types:

    /** Cast operator for float */
    long double as_float() const {
        case INT: return u.int_v; break;
        case FLOAT: return u.float_v; break;
        case BOOL: return u.bool_v?1.0:0.0; break;
            // or parse string to see if it contains exactly a number
        case STRING: return 0.0; break;
        case OBJECT: return 0.0; break;
        case ARRAY: return 0.0; break;
        case NIL: return 0.0; break;
        return 0.0;

    Maybe, members of the union other than inmediate types could be pointers and if the type is one of them, delete it in the destructor of Value.

  3. Log in to comment