Bijju
Bijju

Reputation: 43

assert is failing for rapidJson

I am trying to parse json data in c++ using RapidJson. I dont know where am I doing wrong but my assert is failing. When I try to debug its showing sigabrt when it runs the line assert. Community I appreciate your insights. Thanks for answering this naive question.

#include <iostream>
#include <assert.h>
#include "hpdf.h"
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include <rapidjson/istreamwrapper.h>
#include <fstream>

int main() {

    std::ifstream ifs("/home/is/..../test.json");
    rapidjson::IStreamWrapper isw(ifs);

    rapidjson::Document document;
    document.ParseStream(isw);


    assert(document.IsObject());

    rapidjson::Value::MemberIterator hello = document.FindMember("timeStamp");
    std::string vali = document["timestamp"].GetString();
    std::cout << vali << std::endl;

    return 0;

}

I tried using rapidJson filestream also but again it failed in the same line.

[
  {
    "timeStamp": "...",
    "alertType": "...",
    "instanceId": 8

   }
  ]

Upvotes: 0

Views: 6822

Answers (3)

Bijju
Bijju

Reputation: 43

There is a problem with my data.json. This library works when there is no [] in the .json file. I removed the square brackets and everything seems to work fine.

Upvotes: 0

Read more about JSON.

Your input document (starting with a [) is a JSON array containing a single JSON object.

I recommend using document.GetType() then have different processing on its result (perhaps with a switch).

So it is normal that document.IsObject() is false. For your input document.IsArray() should be true and your JSON object inside that array is accessible thru document[0]

Upvotes: 1

c-smile
c-smile

Reputation: 27470

99% on that your JSON is wrong.

Yet, "timeStamp" is not the same as "timestamp"

Upvotes: 1

Related Questions