VError VUUID::FromJSONValue( const VJSONValue& inJSONValue) { if (inJSONValue.IsNull()) SetNull( true); else { VString s; inJSONValue.GetString( s); FromString( s); } return VE_OK; }
void VJSONValue::test() { const char *sTestJson = "{\"good\":true,\"name\":\"smith\",\"paul\":{\"good\":false,\"name\":\"paul\",\"Nul\":null,\"age\":10.321,\"text\":\"\"\\/\b\f\n\r\t\"},\"age\":42}"; VJSONValue object( JSON_object); object.SetProperty( CVSTR( "name"), VJSONValue( CVSTR( "smith"))); object.SetProperty( CVSTR( "age"), VJSONValue( 42)); object.SetProperty( CVSTR( "good"), VJSONValue::sTrue); for( VJSONPropertyIterator i( object.GetObject()) ; i.IsValid() ; ++i) { VString propertyName = i.GetName(); VJSONValue propertyValue = i.GetValue(); } VJSONValue object_child( JSON_object); object_child.SetProperty( CVSTR( "name"), VJSONValue( CVSTR( "paul"))); object_child.SetProperty( CVSTR( "age"), VJSONValue( 10.321)); object_child.SetProperty( CVSTR( "good"), VJSONValue::sFalse); object_child.SetProperty( CVSTR( "text"), VJSONValue( CVSTR( "\"\\/\b\f\n\r\t"))); object_child.SetProperty( CVSTR( "Nul"), VJSONValue::sNull); object_child.SetProperty( CVSTR( "undef"), VJSONValue::sUndefined); VJSONArray *object_children = new VJSONArray; object_children->Push( object_child); object.SetProperty( CVSTR( "children"), VJSONValue( object_children)); ReleaseRefCountable( &object_children); VString s; VJSONWriter writer( JSON_WithQuotesIfNecessary); VError err = writer.StringifyValue( object, s); DebugMsg( s); DebugMsg( "\n"); VJSONWriter writer2( JSON_WithQuotesIfNecessary | JSON_PrettyFormatting); err = writer2.StringifyValue( object, s); DebugMsg( s); DebugMsg( "\n"); VJSONValue value; VJSONImporter importer(s); err = importer.Parse( value); VString s2; writer2.StringifyValue( value, s2); DebugMsg( s2); DebugMsg( "\n"); xbox_assert( s == s2); { VJSONImporter importer2( CVSTR( "[1,2,3,[4,5],6]")); err = importer2.Parse( value); value.GetString( s2); DebugMsg( s2); DebugMsg( "\n"); } }
VError VJSONWriter::StringifyValue( const VJSONValue& inValue, VString& outString) { VError err = VE_OK; switch( inValue.GetType()) { case JSON_undefined: { outString = VJSONValue::sUndefinedString; break; } case JSON_null: { outString = VJSONValue::sNullString; break; } case JSON_true: { outString = VJSONValue::sTrueString; break; } case JSON_false: { outString = VJSONValue::sFalseString; break; } case JSON_string: { VString s; err = inValue.GetString( s); s.GetJSONString( outString, GetOptions()); break; } case JSON_number: { VReal r( inValue.GetNumber()); err = r.GetJSONString( outString, GetOptions()); break; } case JSON_array: { err = StringifyArray( inValue.GetArray(), outString); break; } case JSON_object: { err = StringifyObject( inValue.GetObject(), outString); break; } default: xbox_assert( false); } return err; }
VError VJSONWriter::StringifyValue( const VJSONValue& inValue, VString& outString) { VError err = VE_OK; switch( inValue.GetType()) { case JSON_undefined: { outString = VJSONValue::sUndefinedString; break; } case JSON_null: { outString = VJSONValue::sNullString; break; } case JSON_true: { outString = VJSONValue::sTrueString; break; } case JSON_false: { outString = VJSONValue::sFalseString; break; } case JSON_string: { VString s; err = inValue.GetString( s); s.GetJSONString( outString, GetOptions()); break; } case JSON_date: { VTime dd; VString s; inValue.GetTime(dd); dd.GetJSONString(s); if ((GetOptions() & JSON_AllowDates) != 0) { outString = "\"!!" + s + "!!\""; } else outString = "\"" + s + "\""; break; } case JSON_number: { VReal r( inValue.GetNumber()); err = r.GetJSONString( outString, GetOptions()); break; } case JSON_array: { err = StringifyArray( inValue.GetArray(), outString); break; } case JSON_object: { err = StringifyObject( inValue.GetObject(), outString); break; } default: xbox_assert( false); } return err; }