khoarus / rapidjson

Automatically exported from code.google.com/p/rapidjson
MIT License
0 stars 0 forks source link

Serializing to a string #34

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
Hello,

I am trying to serialize a JSON document to a string and I am not able to do 
it. I have been reading the examples but I don´t find nothing similar. In all 
the examples the serializing text is redirected to the standard output throught 
the FileStream, but I need to redirect it to a string variable. How can I do 
it? There is another class like String Stream or I have to implement it?

Thank you for your help.

Original issue reported on code.google.com by unai.d...@gmail.com on 27 Aug 2012 at 12:32

GoogleCodeExporter commented 8 years ago
Hi,

I just got into this library. You can use the GenericStringBuffer class to do 
this:

GenericStringBuffer<UTF8<>> buffer;
Writer<GenericStringBuffer<UTF8<>>> writer(buffer);

// use writer
writer.StartObject();
writer.EndObject();

std::string str = buffer.GetString();

I personally would like to see rapidjson::Writer support std::stringstream. It 
currently does not as Writer expects an uppercase Put() instead of the 
lower-case put().

hope that helps!

Original comment by p.aum...@gmail.com on 9 Sep 2012 at 9:41

GoogleCodeExporter commented 8 years ago

Original comment by milo...@gmail.com on 12 Nov 2012 at 2:16

GoogleCodeExporter commented 8 years ago
Sorry i don`t understand how to use your code snipped:

I have a function something like that:

Document statusContent;
statusContent.SetObject();

Value cell1(voltages.cell1);
Value cell2(voltages.cell2);
Value cell3(voltages.cell3);

statusContent.AddMember("cell1", cell1, statusContent.GetAllocator());
statusContent.AddMember("cell2", cell2, statusContent.GetAllocator());
statusContent.AddMember("cell3", cell3, statusContent.GetAllocator());

// Convert JSON document to string
GenericStringBuffer< UTF8<> > buffer;
Writer<GenricStringBuffer, UTF8<> > writer(buffer);

// use writer
writer.StartObject();
writer.EndObject();

string str = buffer.GetString();

But your example wont compile i get the error:
holobotcontrol.cpp:193:60: error:   expected a type, got 
‘GenericStringBuffer’

I also dont unterstand how to say: Use my document "statusContent" as source 
for conversion!?
Could you please tell me how i stringify my object in this case?

Original comment by lukas.du...@googlemail.com on 8 Dec 2012 at 1:43

GoogleCodeExporter commented 8 years ago
1) you should include stringbuffer.h in your project

2) You should call document.Accept(writer) to associate your document with 
writer

Original comment by kazt...@gmail.com on 25 Feb 2013 at 8:30

GoogleCodeExporter commented 8 years ago
Using StringBuffer worked for me

--code--
#include "rapidjson/document.h"     // rapidjson's DOM-style API
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
#include <cstdio>

using namespace rapidjson;

int main(int, char*[]) {
    const char json[] = "{\"category\":\"\", \"milestone\":\"\", \"data_name\":\"\", \"data_field\":\"\", \"data_value\":\"\", \"data_type\":\"\"}";

    Document document;

    printf("Original json:\n%s\n", json);

    // In-situ parsing, decode strings directly in the source string. Source must be string.
    char buffer[sizeof(json)];
    memcpy(buffer, json, sizeof(json));
    if (document.ParseInsitu<0>(buffer).HasParseError())
        return 1;

    document["category"] = "INIT";
    document["data_value"] = "foobar";

    printf("Updated json:\n");

    // Convert JSON document to string
    StringBuffer strbuf;
    Writer<StringBuffer> writer(strbuf);
    document.Accept(writer);
    // string str = buffer.GetString();
    printf("--\n%s\n--\n", strbuf.GetString());

    return 0;
}

Original comment by blpwe...@gmail.com on 6 Mar 2013 at 7:33

GoogleCodeExporter commented 8 years ago

Original comment by milo...@gmail.com on 24 Jun 2014 at 2:06

GoogleCodeExporter commented 8 years ago
about the function GetString()
why is the return type is :: const char*
when i use it whith ch generic param as wchar_t it makes troubles 
so if the return type is const ch * it works for me

Original comment by abo.anas...@gmail.com on 24 Jan 2015 at 9:35

GoogleCodeExporter commented 8 years ago
Please use the latest version at https://github.com/miloyip/rapidjson

Original comment by milo...@gmail.com on 24 Jan 2015 at 2:47