bool GdaJson::findValue(const json_spirit::Value& input, json_spirit::Value& output, const wxString& name) { if (input.type() != json_spirit::obj_type) return false; return GdaJson::findValue(input.get_obj(), output, name); }
void ConvertTo(json_spirit::Value& value, bool fAllowNull=false) { if (fAllowNull && value.type() == json_spirit::null_type) return; if (value.type() == json_spirit::str_type) { // reinterpret string as unquoted json value json_spirit::Value value2; std::string strJSON = value.get_str(); if (!read_string(strJSON, value2)) throw std::runtime_error(std::string("Error parsing JSON:")+strJSON); ConvertTo<T>(value2, fAllowNull); value = value2; } else { value = value.get_value<T>(); } }
json_spirit::Object JSONRPCReplyObj(const json_spirit::Value& result, const json_spirit::Value& error, const json_spirit::Value& id) { json_spirit::Object reply; if (error.type() != json_spirit::null_type) reply.push_back(json_spirit::Pair("result", json_spirit::Value::null)); else reply.push_back(json_spirit::Pair("result", result)); reply.push_back(json_spirit::Pair("error", error)); reply.push_back(json_spirit::Pair("id", id)); return reply; }
bool jsonGetMember(const json_spirit::Value& value, const char* name, T& out) { using namespace json_spirit; if (value.type() == obj_type) { // yeah, only objects have values. BOOST_FOREACH(const Pair& pair, value.get_obj()) { if (pair.name_ == name) { out = pair.value_.get_value<T>(); return true; } } }
// Convert from a JSON to an AtNode static AtSmartPtr<AtNode> ConvertNode(json_spirit::Value node) { AtSmartPtr<AtNode> obj (new AtNode()); if (node.type() == json_spirit::str_type) { obj->value = std::wstring(node.get_str().begin(),node.get_str().end()); } else if (node.type() == json_spirit::int_type || node.type() == json_spirit::real_type) { std::wstringstream stream; if (node.type() == json_spirit::int_type) stream << node.get_int(); if (node.type() == json_spirit::real_type) stream << node.get_real(); obj->value = stream.str().c_str(); obj->children.insert(AtNode::child_pairtype( "@number", AtSmartPtr<AtNode>(new AtNode()) )); } else if (node.type() == json_spirit::bool_type) { if (node.get_bool()) obj->value = L"true"; else obj->value = L"false"; obj->children.insert(AtNode::child_pairtype( "@boolean", AtSmartPtr<AtNode>(new AtNode()) )); } else if (node.type() == json_spirit::array_type) { obj->children.insert(AtNode::child_pairtype( "@array", AtSmartPtr<AtNode>(new AtNode()) )); json_spirit::Array nodeChildren = node.get_array(); json_spirit::Array::iterator itr = nodeChildren.begin(); for (; itr != nodeChildren.end(); itr++) { obj->children.insert(AtNode::child_pairtype( "item", ConvertNode(*itr) )); } } else if (node.type() == json_spirit::obj_type) { json_spirit::Object objectProperties = node.get_obj(); json_spirit::Object::iterator itr = objectProperties.begin(); for (; itr != objectProperties.end(); itr++) { obj->children.insert(AtNode::child_pairtype( itr->name_, ConvertNode(itr->value_) )); } } else if (node.type() == json_spirit::null_type) { return obj; } else { assert(! "Unimplemented type found when parsing JSON!"); } return obj; }
bool GdaJson::hasName(const json_spirit::Value& val, const wxString& name) { if (val.type() != json_spirit::obj_type) return false; return GdaJson::hasName(val.get_obj(), name); }
wxString GdaJson::getStrValFromObj(const json_spirit::Value& val, const wxString& name) { if (val.type() != json_spirit::obj_type) return ""; return getStrValFromObj(val.get_obj(), name); }