Closed qicosmos closed 9 years ago
All JsonValue's valid until JsonAllocator alive. In this situation allocating buffer from allocator and copy string to them can solve the problem
hi, thanks for your reply, could you give some sample about the solution?
std::string source = "{\"foo\" : \"bar\"}";
JsonAllocator allocator;
memcpy(allocator.allocate(source.size() + 1), source.c_str(), source.size() + 1);
char *endptr;
JsonValue value;
int status = jsonParse(source, &endptr, &value, allocator);
// ... and use value as usual
thanks for your reply, however the solution is a little complicated, indeed i want to make a dynamic type by nan-boxing, actually lua, js, use nan-boxing to create dynamic type, so i think c++ also can do this. just like this: dynamic a = 1; dynamic b = 2.5; dynamic c = "hello world"; //more natural and easy to use it is a problem for string, cause the address maybe invalid, maybe need reference count to solve the problem.
JsonValue save the address of pointer, but if the value is std::string, and maybe it is a temporary variables, in this situation, the address is held by JsonValue is valid.
how to reslove the problem?