Reputation: 167
using rapidjson, how can i encode a number to json format? i have 123.321, i want to convert it to "123.321", then save it in a file. later using json convert it back to 123.321. i don't want to use Document which saves as like "{"tag":"value"}", i want it to be saved as just a "value", then be converted back.
i have the following code to convert number to "number":
Value v(123);
StringBuffer mybuffer;
Writer<StringBuffer> mywriter(mybuffer);
v.Accept(mywriter);
const char* myjson = mybuffer.GetString();
how to convert it back? is the following a solution? i don't want to use handler!
Reader reader;
StringStream ss(myjson);
reader.Parse(ss, handler);
thanks for the upcoming helps.
Upvotes: 1
Views: 2279
Reputation: 167
I just found the answer after digging into google:
to encode a number to json using rapidjson:
Value v(123.321);
StringBuffer mybuffer;
Writer<StringBuffer> mywriter(mybuffer);
v.Accept(mywriter);
const char* myjson = mybuffer.GetString();
now myjson has "123.321" as its value. then to decode myjson to a number:
Document d;
d.Parse(myjson); // myjson is "123.321"
assert(d.IsNumber());
value = d.GetDouble(); // now the value is 123.321
as simple as it gets.
Upvotes: 3