wangxiaowei0303 / rapidjson

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

Add tutorial information on how to add objects and arrays of objects #59

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The rapidjson code worked great. I just had excessive headaches trying to get 
the right usage and call flow to add objects within a JSON document or array. I 
did figure it out and it worked very well, so I thought that it would be best 
to share.

The code below hasn't been directly compiled, but the flow and general syntax 
is what worked for my application.

    // Create your new document
    Document *newDoc = new Document;

    // Parse an empty JSON objext to initialize the document.
    char json[IBUFSIZE] = "{  }";
    if (newDoc->Parse<0>(json).HasParseError())
    {
        // Oops, something went wrong
    }
    else
    {
        Value myVal;

        // This is just a string that is added to the document.
        myVal.SetString("My JSON Document", newDoc->GetAllocator());
        newDoc->AddMember("Doc Name", myVal, newDoc->GetAllocator());

        // Now we add a new object to the document that will be used to store its own info
        // First we add the member. Then we define th new member as an object.
        // Then we get a reference to the object so that we can more easily add to
        // or read from it.
        newDoc->AddMember("Parameters", newValue, newDoc->GetAllocator());
        (*newDoc)["Parameters"].SetObject();
        Value& outParams = (*newDoc)["Parameters"];

        // Now we add a name/value pair to the object outParams instead of the
        // document newDoc.
        myVal.SetInt(17);
        outParams->AddMember("Param Int", myVal, newDoc->GetAllocator());

        // Now add an array of objects
        // First, create an array
        outParams->AddMember("Param Array", arrayValue, newDoc->GetAllocator());
        (*outParams)["Param Array"].SetArray();
        Value& aVal = (*outParams)["Param Array"];

        // Now, add 5 objects to the array
        for (int i=0; i<5; i++)
        {
            Value objValue;
            objValue.SetObject();
            //call_func_to_add_data_to_object(newDoc, &objValue, <reference to data to add>);

            // Now push the object onto the end of the array
            aVal.PushBack(objValue, newDoc->GetAllocator());
        }
    }

Hopefully the code above saves someone else some hours of trial and error and 
maybe even becomes a snippet in a future version of the tutorial.

Original issue reported on code.google.com by ryan.den...@discgogo.com on 28 Jan 2013 at 10:53

GoogleCodeExporter commented 9 years ago
This isn't a defect - it should be marked as a task. If there was a way to set 
this properly, I missed it. Sorry about that.

Original comment by ryan.den...@discgogo.com on 28 Jan 2013 at 10:55

GoogleCodeExporter commented 9 years ago

Original comment by milo...@gmail.com on 5 Feb 2013 at 10:59

GoogleCodeExporter commented 9 years ago
Hi,

Thank you for the code snippet. This really helps. I do have a question, 
regarding 

newDoc->AddMember("Parameters", newValue, newDoc->GetAllocator());

What is newValue? what is it being set to?

Thank you

Original comment by nkan...@gmail.com on 23 Apr 2014 at 12:58

GoogleCodeExporter commented 9 years ago
My error in forgetting that. It is used inside of rapidjson but not used in our 
code when adding an object. Add the definition when defining the new document 
and it should be good to go.

// Create your new document
Document *newDoc = new Document;
Value newValue;                       // Add this line

Original comment by ryan.den...@discgogo.com on 26 Apr 2014 at 6:40

GoogleCodeExporter commented 9 years ago
I hope the new documentation and examples (in progress) can help users to learn 
how to use RapidJSON.

http://miloyip.github.io/rapidjson/md_doc_tutorial.html
http://miloyip.github.io/rapidjson/md_doc_dom.html
https://github.com/miloyip/rapidjson/tree/master/example/simpledom
https://github.com/miloyip/rapidjson/tree/master/example/tutorial

If you have some ideas on the documentation and/or examples, please tell us.
You may create an issue in https://github.com/miloyip/rapidjson/issues

Thank you.

Original comment by milo...@gmail.com on 13 Jul 2014 at 4:34