I have a nested JSON object. I am trying to build it in a function and add an internal object to the original, but I cannot extract the result.
void build_object (Poco::JSON::Object * const result) { /* Construct some int/bool/string fields here */ Poco::JSON::Object inner; inner.set("some_number", 5); inner.set("some_string", "xyz"); /* This is where it breaks down */ std::string key = "new_object"; result->set("new_object", inner); /* Then some debugging and testing */ // The new object is printed inside the first -> seems like it working result->stringify(std::cout); printf("result has(key): %i\n", result->has(key)); // true printf("isObject: %i\n", result->isObject(key)); // false - huh? printf("isNull: %i\n", result->isNull(key)); // false printf("isArray: %i\n", result->isArray(key)); // false Poco::JSON::Object::Ptr ptr = result->getObject(key); // unsurpisingly fails since the above indicates it not an object printf("ptr isNull: %i\n", ptr.isNull()); // true // ptr->has("some_number"); // throws NullPointerException // if it not an object/null/array, it must be a value Poco::Dynamic::Var v = result->get(key); // at least one of these things should be true, otherwise what is it? printf("var isString: %i\n", v.isString()); // false printf("var isStuct: %i\n", v.isStruct()); // false printf("var isEmpty: %i\n", v.isEmpty()); // false printf("var isArray: %i\n", v.isArray()); // false printf("var isSigned: %i\n", v.isSigned()); // false printf("var isNumeric: %i\n", v.isNumeric());// false }
So, I have an internal object that is correctly placed in the result, it is printed using stringify with all the correct values, and the result is → has (). But, according to the result, it is not an object, an array, or zero, so you can get it with var. But, once it is obtained from var, it is not a string, structure, array or number, nor is it empty. The inner object seems to exist and does not exist at the same time.
So how do I put this object in my result? And how do I get this?
thanks
note: I saw this topic Proper use of POS-C ++ JSON for data analysis , but it builds a JSON object from a string and then parses it. I suppose I could build everything as a string and convert to a Poco object in the last step, but I'm still wondering why this is happening. Also, using result-> set () and result-> get () is a cleaner, less hack-y solution than going through a string.
Links: Poco JSON Doc , Poco Dynamic Var Doc