JSON library bug?
Added by alex merge over 8 years ago
This code
#include <iostream>
#include <Wt/Json/Object>
#include <Wt/Json/Array>
#include <Wt/Json/Value>
#include <Wt/Json/Serializer>
using namespace std;
using namespace Wt::Json;
int main() {
Object result;
try {
Value tmp(ArrayType);
result["datafields"] = tmp;
const Array& tmp_array1 = result.get("datafields"); // seems to be OK but cannot modify returned object
result["data"] = Value(ArrayType);
Array& tmp_array2 = (Array&) result.get("data"); // need to const_cast to work with returned object
tmp_array2.push_back(Value(ObjectType));
Object& tmp_object = tmp_array2.back(); // <---- here is SIGSEGV
tmp_object["Role"] = Value(StringType);
tmp_object["Role"] = "text1";
} catch (const std::exception& e) {
std::cout << e.what();
}
}
Cause segmentation fault.
The call stack is :
Thread #1 [test] 1835 [core: 0] (Suspended : Signal : SIGSEGV:Segmentation fault)
boost::any::empty() at any.hpp:139 0x7ffff6e043f6
Wt::Json::Value::type() at Value.C:157 0x7ffff72fa66c
Wt::Json::Value::getR<Wt::Json::Object>() at Value.C:72 0x7ffff72fc2f0
Wt::Json::Value::operator Wt::Json::Object&() at Value.C:260 0x7ffff72facd5
main() at main.cpp:21 0x411d00
the system is : Centos 7 , Linux 3.10.0-229.11.1.el7.x86_64 x86_64, gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), boost v.1_56
What Im doing wrong ?
Replies (3)
RE: JSON library bug? - Added by Koen Deforche over 8 years ago
Hey,
Modified as follows it works (without casts):
int main() {
Object result;
try {
Value tmp(ArrayType);
result["datafields"] = tmp;
const Array& tmp_array1 = result.get("datafields"); // seems to be OK but cannot modify returned object
result["data"] = Value(ArrayType);
Array& tmp_array2 = result["data"]; // Wt::Json::Object is a std::map, so you can use it as such
tmp_array2.push_back(Value(ObjectType));
Object& tmp_object = tmp_array2.back();
tmp_object["Role"] = Wt::WString("text1"); // <- you can directly set data in an object (but needs to be a WString for string data)
} catch (const std::exception& e) {
std::cout << e.what();
}
std::cerr << Wt::Json::serialize(result) << std::endl;
}
I'm not entirely sure why your code doesn't work though (despite the const cast it should still work, it seems). I'll need to investigate this some time later.
Koen
RE: JSON library bug? - Added by yvan vander sanden almost 8 years ago
I was facing the same problem. The problem lies within the reference manual.
It gives this example:
Json::Object person;
person["children"] = Json::Value(Json::ArrayType);
Json::Array& children = person.get("children");
// add children ...
The get function retrieves a reference to a Json::Value&, not the array. That is why the original poster tried casting it like:
Json::Array& children = (Json::Array&)person.get("children");
Which is incorrect. The reference manual's example should be:
Json::Array& children = person["children"];