byzhang / rapidjson

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

Adding elements to an array #105

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hello

Maybe somebody can help me see the problem. I think I'm following the proper 
steps (I read Issue #59).

I'm trying to add elements to an array, but it stays empty.

Here's my code:

        // Generate output file structure
        rapidjson::Document jsonDoc;
        rapidjson::Value newValue;

        jsonDoc.SetObject();
        jsonDoc.AddMember("ArchwaveAES67", newValue, jsonDoc.GetAllocator());
        jsonDoc["ArchwaveAES67"].SetObject();
        rapidjson::Value& aes67 = jsonDoc["ArchwaveAES67"];

        newValue.SetInt(1);
        aes67.AddMember("version", newValue, jsonDoc.GetAllocator());

        aes67.AddMember("devices", newValue, jsonDoc.GetAllocator());
        jsonDoc["devices"].SetArray();
        rapidjson::Value& devices = jsonDoc["devices"];

/*        const RavennaHTTPServers& ravennaHTTPServers = 
m_bonjourBrowser.getRavennaHTTPServers();

        for( RavennaHTTPServers::const_iterator it = ravennaHTTPServers.begin();
             it != ravennaHTTPServers.end();
             it++ )
        {
            rapidjson::Value newDevice;
            newDevice.SetObject();

            newValue.SetString( it->second.m_serviceName.c_str() );
            newDevice.AddMember("hostname", newValue, jsonDoc.GetAllocator());

            sprintf( buff, "%s:%d", it->second.m_address.c_str(), it->second.m_port );
            newValue.SetString( buff );
            newDevice.AddMember("IPaddress", newValue, jsonDoc.GetAllocator());

            devices.PushBack( newDevice, jsonDoc.GetAllocator() );
        }
*/

            rapidjson::Value newDevice;
            newDevice.SetObject();

            newValue.SetString( "SomeHost" );
            newDevice.AddMember("hostname", newValue, jsonDoc.GetAllocator());

            newValue.SetString( "12.123.123.12:1562" );
            newDevice.AddMember("IPaddress", newValue, jsonDoc.GetAllocator());

            devices.PushBack( newDevice, jsonDoc.GetAllocator() );

        // Write output
        rapidjson::StringBuffer jsonOutput;
        rapidjson::PrettyWriter< rapidjson::StringBuffer > jsonWriter( jsonOutput );

        jsonDoc.Accept( jsonWriter );
        printf( "JSON string: %s\n", jsonOutput.GetString() );

Note that I commented out the loop, just to make sure that something gets 
added. The code after the loop tries to add a dummy element to the array.

What I get at the output is:

{
    "ArchwaveAES67": {
        "version": 1,
        "devices": null
    }
}

Instead of:

{
    "ArchwaveAES67": {
        "version": 1,
        "devices": [
            "hostname": "SomeHost",
            "IPaddress": "12.123.123.12:1562"
        ]
    }
}

I get no compiler errors nor warnings.

Can anybody help me find the problem? Is this a bug or am I using the library 
wrong?

Thanks in advance and kind regards,
Roberto Barbieri Carrera

Original issue reported on code.google.com by rbarbier...@gmail.com on 21 Mar 2014 at 9:24

GoogleCodeExporter commented 9 years ago
This looks wrong:
   aes67.AddMember("devices", newValue, jsonDoc.GetAllocator());
   jsonDoc["devices"].SetArray(); // (*)
   rapidjson::Value& devices = jsonDoc["devices"]; 

You probably meant to use aes67 here instead of jsonDoc:
   newValue.SetArray();
   aes67.AddMember("devices", newValue, jsonDoc.GetAllocator());
   rapidjson::Value& devices = aes67["devices"];

IIRC, requesting the non-existent member "devices" in jsonDoc (*) should have 
raised an assertion.  Do you compile with assertions disabled?  This would be a 
bad idea for debugging...

hth,
  Philipp

Original comment by philipp....@gmail.com on 21 Mar 2014 at 12:51

GoogleCodeExporter commented 9 years ago
Oh, thank you very much. 

I'm sorry to have posted this. 

I can't unfortunately enable exceptions on my embedded platform, they are not 
supported.

Thanks for your help.

Kind regards,
Roberto

Original comment by rbarbier...@gmail.com on 21 Mar 2014 at 1:09

GoogleCodeExporter commented 9 years ago
An "assertion" is not an "exception", it's just a check followed by a function 
call (e.g. printing a message and calling abort()).

#ifndef RAPIDJSON_ASSERT
#include <cassert>
#define RAPIDJSON_ASSERT(x) assert(x)
#endif // RAPIDJSON_ASSERT

If your platform doesn't support "assert()", you can provide your own 
RAPIDJSON_ASSERT macro which lets you at least put a breakpoint in the debugger:

void assertion_failed( const char* expr, const char* file, int line );
#define RAPIDJSON_ASSERT(Expr)
  ((void)((Expr) ? 0 : (assertion_failed( #Expr, __FILE__, __LINE__ ), 0)))

/Philipp

Original comment by philipp....@gmail.com on 21 Mar 2014 at 1:50

GoogleCodeExporter commented 9 years ago
Sorry, I read wrong. That I can certainly do.

Thanks a lot for your help.

R.,
R.

Original comment by rbarbier...@gmail.com on 21 Mar 2014 at 2:00

GoogleCodeExporter commented 9 years ago

Original comment by milo...@gmail.com on 20 Jun 2014 at 11:20