static void printValueTree( FILE *fout, Json::Value &value, const std::string &path = "." ) { switch ( value.type() ) { case Json::nullValue: fprintf( fout, "%s=null\n", path.c_str() ); break; case Json::intValue: fprintf( fout, "%s=%s\n", path.c_str(), Json::valueToString( value.asLargestInt() ).c_str() ); break; case Json::uintValue: fprintf( fout, "%s=%s\n", path.c_str(), Json::valueToString( value.asLargestUInt() ).c_str() ); break; case Json::realValue: fprintf( fout, "%s=%s\n", path.c_str(), normalizeFloatingPointStr(value.asDouble()).c_str() ); break; case Json::stringValue: fprintf( fout, "%s=\"%s\"\n", path.c_str(), value.asString().c_str() ); break; case Json::booleanValue: fprintf( fout, "%s=%s\n", path.c_str(), value.asBool() ? "true" : "false" ); break; case Json::arrayValue: { fprintf( fout, "%s=[]\n", path.c_str() ); int size = value.size(); for ( int index =0; index < size; ++index ) { static char buffer[16]; #if defined(_MSC_VER) && defined(__STDC_SECURE_LIB__) sprintf_s( buffer, sizeof(buffer), "[%d]", index ); #else snprintf( buffer, sizeof(buffer), "[%d]", index ); #endif printValueTree( fout, value[index], path + buffer ); } } break; case Json::objectValue: { fprintf( fout, "%s={}\n", path.c_str() ); Json::Value::Members members( value.getMemberNames() ); std::sort( members.begin(), members.end() ); std::string suffix = *(path.end()-1) == '.' ? "" : "."; for ( Json::Value::Members::iterator it = members.begin(); it != members.end(); ++it ) { const std::string &name = *it; printValueTree( fout, value[name], path + suffix + name ); } } break; default: break; } }
void JsonTree::init( const string &key, const Json::Value &value, bool setType, NodeType nodeType, ValueType valueType ) { mKey = key; mNodeType = nodeType; mParent = 0; mValue = ""; mValueType = valueType; if( ! value.isNull() && ( value.isArray() || value.isObject() ) ) { if( value.isArray() ) { mNodeType = NODE_ARRAY; for ( uint32_t i = 0; i < value.size(); i++ ) { pushBack( JsonTree( "", value[ i ] ) ); } } else if( value.isObject() ) { mNodeType = NODE_OBJECT; Json::Value::Members members = value.getMemberNames(); for( Json::Value::Members::const_iterator memberIt = members.begin(); memberIt != members.end(); ++memberIt ) { string key = *memberIt; pushBack( JsonTree( key, value[ key ] ) ); } } } else { if( value.isBool() ) { mValue = toString( value.asBool() ); if( setType ) { mValueType = VALUE_BOOL; } } else if ( value.isDouble() ) { mValue = toString( value.asDouble() ); if ( setType ) { mValueType = VALUE_DOUBLE; } } else if ( value.isInt() ) { mValue = toString( value.asLargestInt() ); if ( setType ) { mValueType = VALUE_INT; } } else if ( value.isString() ) { mValue = toString( value.asString() ); if ( setType ) { mValueType = VALUE_STRING; } } else if ( value.isUInt() ) { mValue = toString( value.asLargestUInt() ); if ( setType ) { mValueType = VALUE_UINT; } } } }
static inline void pack(msgpack::packer<Stream>& packer, const Json::Value& source) { switch(source.type()) { case Json::objectValue: { packer.pack_map(source.size()); const Json::Value::Members keys(source.getMemberNames()); for(auto it = keys.begin(); it != keys.end(); ++it) { packer << *it; pack(packer, source[*it]); } } break; case Json::arrayValue: { packer.pack_array(source.size()); for(auto it = source.begin(); it != source.end(); ++it) { pack(packer, *it); } } break; case Json::booleanValue: { packer << source.asBool(); } break; case Json::stringValue: { packer << source.asString(); } break; case Json::realValue: { packer << source.asDouble(); } break; case Json::intValue: { packer << source.asLargestInt(); } break; case Json::uintValue: { packer << source.asLargestUInt(); } break; case Json::nullValue: { packer << msgpack::type::nil(); }} }
py::object pj_convert(Json::Value const & node) { switch (node.type()) { case Json::ValueType::nullValue: { //return py::object(); return py::object(Py_None, true); } case Json::ValueType::intValue: { //node.asInt(); //node.asInt64(); return py::int_(node.asLargestInt()); } case Json::ValueType::uintValue: { //node.asUInt(); //node.asUInt64(); return py::int_(node.asLargestUInt()); } case Json::ValueType::realValue: { //node.asFloat(); return py::float_(node.asDouble()); } case Json::ValueType::stringValue: { return py::str(node.asString()); } case Json::ValueType::booleanValue: { return py::bool_(node.asBool()); } case Json::ValueType::arrayValue: { py::list result; for (auto it = node.begin(); it != node.end(); ++it) { result.append(pj_convert(*it)); } return result; } case Json::ValueType::objectValue: { py::dict result; for (auto it = node.begin(); it != node.end(); ++it) { result[pj_convert(it.key())] = pj_convert(*it); } return result; } default: { throw std::runtime_error("undefined json value"); } } }