/** @brief Utilities for handling JSON data. */ #ifndef JsonUtil_H #define JsonUtil_H class RageFileBasic; #include "json/value.h" namespace JsonUtil { bool LoadFromString( Json::Value &root, RString sData, RString &sErrorOut ); bool LoadFromStringShowErrors(Json::Value &root, const RString sData); bool LoadFromFileShowErrors(Json::Value &root, const RString &sFile); bool LoadFromFileShowErrors(Json::Value &root, RageFileBasic &f); bool WriteFile(const Json::Value &root, const RString &sFile, bool bMinified); template static void SerializeVectorObjects(const vector &v, void fn(const T &, Json::Value &), Json::Value &root) { root = Json::Value(Json::arrayValue); root.resize(v.size()); for(unsigned i=0; i static void SerializeVectorPointers(const vector &v, void fn(const T &, Json::Value &), Json::Value &root) { root = Json::Value(Json::arrayValue); root.resize(v.size()); for(unsigned i=0; i static void SerializeArray(const V &v, void fn(const T &, Json::Value &), Json::Value &root) { root = Json::Value(Json::arrayValue); root.resize( v.size() ); int i=0; for( typename V::const_iterator iter=v.begin(); iter!=v.end(); iter++ ) fn( *iter, root[i++] ); } template static void SerializeArrayValues(const V &v, Json::Value &root) { root = Json::Value(Json::arrayValue); root.resize( v.size() ); int i=0; for( typename V::const_iterator iter=v.begin(); iter!=v.end(); iter++ ) root[i++] = *iter; } template static void SerializeArrayObjects(const V &v, Json::Value &root) { root = Json::Value(Json::arrayValue); root.resize( v.size() ); int i=0; for( typename V::const_iterator iter=v.begin(); iter!=v.end(); iter++ ) iter->Serialize( root[i++] ); } template static void SerializeStringToObjectMap(const M &m, F fnEnumToString(E e), Json::Value &root) { for( typename M::const_iterator iter=m.begin(); iter!=m.end(); iter++ ) iter->second.Serialize( root[ fnEnumToString(iter->first) ] ); } template static void SerializeStringToValueMap(const M &m, F fnToString(E e), Json::Value &root) { for( typename M::const_iterator iter=m.begin(); iter!=m.end(); iter++ ) root[ fnToString(iter->first) ] = iter->second; } template static void SerializeValueToValueMap(const M &m, Json::Value &root) { for( typename M::const_iterator iter=m.begin(); iter!=m.end(); iter++ ) root[ (iter->first) ] = iter->second; } // Serialize a map that has a non-string key type template static void SerializeObjectToObjectMapAsArray(const V &v, const RString &sKeyName, const RString &sValueName, Json::Value &root) { root = Json::Value(Json::arrayValue); root.resize( v.size() ); int i=0; for( typename V::const_iterator iter=v.begin(); iter!=v.end(); iter++ ) { Json::Value &vv = root[i++]; iter->first.Serialize( vv[sKeyName] ); iter->second.Serialize( vv[sValueName] ); } } template static void SerializeObjectToValueMapAsArray(const V &v, const RString &sKeyName, const RString &sValueName, Json::Value &root) { root = Json::Value(Json::arrayValue); root.resize( v.size() ); int i=0; for( typename V::const_iterator iter=v.begin(); iter!=v.end(); iter++ ) { Json::Value &vv = root[i++]; iter->first.Serialize( vv[sKeyName] ); vv[sValueName] = iter->second; } } template static void SerializeVectorValues(const vector &v, Json::Value &root) { root = Json::Value(Json::arrayValue); root.resize(v.size()); for(unsigned i=0; i static void DeserializeVectorObjects(vector &v, void fn(T &, const Json::Value &), const Json::Value &root) { v.resize(root.size()); for(unsigned i=0; i static void DeserializeArrayObjects( V &v, const Json::Value &root) { v.resize( root.size() ); for( unsigned i=0; i static void DeserializeVectorPointers(vector &v, void fn(T &, const Json::Value &), const Json::Value &root) { for(unsigned i=0; i static void DeserializeArrayValues(vector &v, const Json::Value &root) { v.clear(); for( unsigned i=0; i static void DeserializeArrayValuesIntoSet(S &s, const Json::Value &root) { s.clear(); for( unsigned i=0; i static void DeserializeArrayValuesIntoVector(vector &v, const Json::Value &root) { v.clear(); for( unsigned i=0; i static void DeserializeValueToValueMap(M &m, const Json::Value &root) { for( Json::Value::const_iterator iter = root.begin(); iter != root.end(); iter++ ) (*iter).TryGet( m[ iter.memberName() ] ); } template static void DeserializeStringToValueMap(M &m, F fnToValue(E e), const Json::Value &root) { for( Json::Value::const_iterator iter = root.begin(); iter != root.end(); iter++ ) (*iter).TryGet( m[ fnToValue(iter.memberName()) ] ); } template static void DeserializeStringToObjectMap(M &m, F fnToValue(E e), const Json::Value &root) { for( Json::Value::const_iterator iter = root.begin(); iter != root.end(); iter++ ) m[ fnToValue(iter.memberName()) ].Deserialize( *iter ); } // Serialize a map that has a non-string key type template static void DeserializeObjectToObjectMapAsArray(map &m, const RString &sKeyName, const RString &sValueName, const Json::Value &root) { m.clear(); ASSERT( root.type() == Json::arrayValue ); for( Json::Value::const_iterator iter = root.begin(); iter != root.end(); iter++ ) { ASSERT( (*iter).type() == Json::objectValue ); K k; if( !k.Deserialize( (*iter)[sKeyName] ) ) continue; V v; if( !v.Deserialize( (*iter)[sValueName] ) ) continue; m[k] = v; } } template static void DeserializeObjectToValueMapAsArray(map &m, const RString &sKeyName, const RString &sValueName, const Json::Value &root) { for( unsigned i=0; i static void DeserializeVectorValues(vector &v, const Json::Value &root) { v.resize(root.size()); for(unsigned i=0; i