Ver Fonte

Add JSON method to deserialize from string and expose it to script interfaces.

cosmy há 9 anos atrás
pai
commit
9bb620ad17

+ 1 - 0
Source/Urho3D/AngelScript/ResourceAPI.cpp

@@ -360,6 +360,7 @@ static bool JSONFileSave(File* file, const String& indendation, JSONFile* ptr)
 static void RegisterJSONFile(asIScriptEngine* engine)
 {
     RegisterResource<JSONFile>(engine, "JSONFile");
+    engine->RegisterObjectMethod("JSONFile", "bool FromString(const String&in)", asMETHOD(JSONFile, FromString), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONFile", "JSONValue& GetRoot()", asMETHODPR(JSONFile, GetRoot, () const, const JSONValue&), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONFile", "bool Save(File@+, const String&in) const", asFUNCTION(JSONFileSave), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectMethod("JSONFile", "JSONValue& get_root()", asMETHODPR(JSONFile, GetRoot, () const, const JSONValue&), asCALL_THISCALL);

+ 1 - 0
Source/Urho3D/LuaScript/pkgs/Resource/JSONFile.pkg

@@ -5,6 +5,7 @@ class JSONFile : Resource
     JSONFile();
     ~JSONFile();
 
+    bool FromString(const String source);
     const JSONValue& GetRoot() const;
 
     tolua_outside bool JSONFileSave @ Save(const String fileName, const String indentation = "\t") const;

+ 10 - 0
Source/Urho3D/Resource/JSONFile.cpp

@@ -27,6 +27,7 @@
 #include "../Core/Context.h"
 #include "../IO/Deserializer.h"
 #include "../IO/Log.h"
+#include "../IO/MemoryBuffer.h"
 #include "../Resource/JSONFile.h"
 #include "../Resource/ResourceCache.h"
 
@@ -230,4 +231,13 @@ bool JSONFile::Save(Serializer& dest, const String& indendation) const
     return dest.Write(buffer.GetString(), size) == size;
 }
 
+bool JSONFile::FromString(const String & source)
+{
+    if (source.Empty())
+        return false;
+
+    MemoryBuffer buffer(source.CString(), source.Length());
+    return Load(buffer);
+}
+
 }

+ 3 - 0
Source/Urho3D/Resource/JSONFile.h

@@ -47,6 +47,9 @@ public:
     virtual bool Save(Serializer& dest) const;
     /// Save resource with user-defined indentation, only the first character (if any) of the string is used and the length of the string defines the character count. Return true if successful.
     bool Save(Serializer& dest, const String& indendation) const;
+    
+    /// Deserialize from a string. Return true if successful.
+    bool FromString(const String& source);
 
     /// Return root value.
     JSONValue& GetRoot() { return root_; }