0

Here is the json form:

{"simpleChannels":{"item":[{"channelID":4248,"majorChannelNumber":22,"minorChannelNumber":0,"channelType":"SLL","simpleSchedules":[],"channelKey":"4248_1343210400000","shortName":"KWHY","longName":"A3 Los Angeles 22 KWHY IND","networkAffiliation":["Independent"],"logoID":0,"authCode":"FS","engChlFlag":false,"hasMirror":true,"pgSource":"ACTIVE","hdChlFlag":true,"adultChlFlag":false,"vodProviderId":0,"blackOut":false,"liveStreaming":"N"}]}}

Here is the code that attempts to parse and extract values:
json is a std::string containing the above:

    m_guideSlice.Parse<0>(json.c_str());
    rapidjson::Value& simpleChannels = m_guideSlice["simpleChannels"];
    if (simpleChannels.IsObject()) {
        SYSLOG_CRITICAL("simpleChannels.IsObject() == true\n");
        rapidjson::Value& channelArray= simpleChannels["item"];
        if (channelArray.IsArray()) {
            SYSLOG_CRITICAL("channelArray.IsArray() == true\n");
            rapidjson::Value& channel = channelArray[0u];
            if (channel.IsObject()) {
                SYSLOG_CRITICAL("channel.isObject() == true\n");
                rapidjson::Value& channelID = channel["channelID"];
                if (channelID.IsInt()) {
                    SYSLOG_CRITICAL("channelID.IsInt() == true, channelID= %d\n", channelID.GetInt());
                }
                else {
                    SYSLOG_CRITICAL("channelID.IsInt() == false, type= %X\n", channelID.GetType());                     
                }
                rapidjson::Value& majorChannelNumber = channel["majorChannelNumber"];
                if (majorChannelNumber.IsInt()) {
                    SYSLOG_CRITICAL("majorChannelNumber.IsInt() == true, majorChannelNumber= %d\n", majorChannelNumber.GetInt());
                }
                else {
                    SYSLOG_CRITICAL("majorChannelNumber.IsInt() == false, type= %X\n", majorChannelNumber.GetType());                       
                }
                rapidjson::Value& channelType = channel["channelType"];
                if (channelType.IsString()) {
                    SYSLOG_CRITICAL("channelType.IsString() == true\n")
                    SYSLOG_CRITICAL("channelType = %s\n", channelType.GetString());
                }
                else {
                    SYSLOG_CRITICAL("channelType.IsString() == false, type= %X\n", channelType.GetType());
                }
                rapidjson::Value& shortName = channel["shortName"];
                if (channelType.IsString()) {
                    SYSLOG_CRITICAL("shortName.IsString() == true\n")
                    SYSLOG_CRITICAL("shortName = %s\n", shortName.GetString());
                }
                else {
                    SYSLOG_CRITICAL("shortName.IsString() == false, type= %X\n", shortName.GetType());
                }
            }
            else {
                SYSLOG_CRITICAL("channel.IsObject() == false, type= %X\n", channel.GetType());
            }
        }
        else {
            SYSLOG_CRITICAL("channelArray.IsArray() == false, type= %X\n", channelArray.GetType());             
        }
    }
    else {
        SYSLOG_CRITICAL("simpleChannels.IsObject() == false, type= %X\n", simpleChannels.GetType());
    }

Here is the extracted output from syslog:

May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]:   (PgwsIngest.cpp:78) getGuide(): simpleChannels.IsObject() == true 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:81) getGuide(): channelArray.IsArray() == true 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:84) getGuide(): channel.isObject() == true 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:87) getGuide(): channelID.IsInt() == true, channelID= 0 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:94) getGuide(): majorChannelNumber.IsInt() == true, majorChannelNumber= 0 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:101) getGuide(): channelType.IsString() == true 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:102) getGuide(): channelType = SLL 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:109) getGuide(): shortName.IsString() == true 
May 19 01:25:38   [BSP] [30578]: [PGWS-GEN LOG_CRIT tid:30744 pthr:0x7033e4f0]: (PgwsIngest.cpp:110) getGuide(): shortName = KWHY   

The char json values are extracted correctly but the int ones are always 0. I'm a first time rapidjson user so I'm sure there is a simple error I am making but I don't see it right off.

Thanks all,

Btw, the json form passes on http://www.freeformatter.com/json-validator.html so I think it is correct. It is machine generated anyway.

Andre Silva
  • 4,782
  • 9
  • 52
  • 65
kelvin
  • 57
  • 3

2 Answers2

1

I am on a big endian machine and the compiler must not be passing the appropriate flag to indicate endianness.
Explicitly setting the flag:

define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN

in rapidsjon.h should fix this.

Community
  • 1
  • 1
kelvin
  • 57
  • 3
  • I am author of rapidjson. May I know, in your environment, what is better way to detect the endianness? – Milo Yip Jun 29 '14 at 14:04
0

We have improved the endian detection in this pull request.

It will detect more platform defined macros. If it cannot detect successfully, it generates a preprocessor error and then you must define RAPIDJSON_ENDIAN.

You may merge this diff to your version to see if it helps.

Milo Yip
  • 4,902
  • 2
  • 25
  • 27