Browse Source

Add to/from string conversions for JSONValueType & JSONNumberType. Minor code cleanup and missing JSONValue script bindings. Closes #1546.

Lasse Öörni 9 years ago
parent
commit
b7c7568c24

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

@@ -299,6 +299,12 @@ static void RegisterJSONValue(asIScriptEngine* engine)
     engine->RegisterEnumValue("JSONValueType", "JSON_ARRAY", JSON_ARRAY);
     engine->RegisterEnumValue("JSONValueType", "JSON_OBJECT", JSON_OBJECT);
 
+    engine->RegisterEnum("JSONNumberType");
+    engine->RegisterEnumValue("JSONNumberType", "JSONNT_NAN", JSONNT_NAN);
+    engine->RegisterEnumValue("JSONNumberType", "JSONNT_INT", JSONNT_INT);
+    engine->RegisterEnumValue("JSONNumberType", "JSONNT_UINT", JSONNT_UINT);
+    engine->RegisterEnumValue("JSONNumberType", "JSONNT_FLOAT_DOUBLE", JSONNT_FLOAT_DOUBLE);
+
     engine->RegisterObjectType("JSONValue", sizeof(JSONValue), asOBJ_VALUE | asOBJ_APP_CLASS_CDAK);
     engine->RegisterObjectBehaviour("JSONValue", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructJSONValue), asCALL_CDECL_OBJLAST);
     engine->RegisterObjectBehaviour("JSONValue", asBEHAVE_CONSTRUCT, "void f(bool)", asFUNCTION(ConstructJSONValueBool), asCALL_CDECL_OBJLAST);
@@ -319,6 +325,9 @@ static void RegisterJSONValue(asIScriptEngine* engine)
     engine->RegisterObjectMethod("JSONValue", "JSONValue& opAssign(const JSONValue&in)", asMETHODPR(JSONValue, operator =, (const JSONValue&), JSONValue&), asCALL_THISCALL);
 
     engine->RegisterObjectMethod("JSONValue", "JSONValueType get_valueType() const", asMETHOD(JSONValue, GetValueType), asCALL_THISCALL);
+    engine->RegisterObjectMethod("JSONValue", "JSONNumberType get_numberType() const", asMETHOD(JSONValue, GetNumberType), asCALL_THISCALL);
+    engine->RegisterObjectMethod("JSONValue", "String get_valueTypeName() const", asMETHODPR(JSONValue, GetValueTypeName, () const, String), asCALL_THISCALL);
+    engine->RegisterObjectMethod("JSONValue", "String get_numberTypeName() const", asMETHODPR(JSONValue, GetNumberTypeName, () const, String), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "bool get_isNull() const", asMETHOD(JSONValue, IsNull), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "bool get_isBool() const", asMETHOD(JSONValue, IsBool), asCALL_THISCALL);
     engine->RegisterObjectMethod("JSONValue", "bool get_isNumber() const", asMETHOD(JSONValue, IsNumber), asCALL_THISCALL);

+ 17 - 2
Source/Urho3D/LuaScript/pkgs/Resource/JSONValue.pkg

@@ -2,12 +2,20 @@ $#include "Resource/JSONValue.h"
 
 enum JSONValueType
 {
-    JSON_NULL,
+    JSON_NULL = 0,
     JSON_BOOL,
     JSON_NUMBER,
     JSON_STRING,
     JSON_ARRAY,
-    JSON_OBJECT,
+    JSON_OBJECT
+};
+
+enum JSONNumberType
+{
+    JSONNT_NAN = 0,
+    JSONNT_INT,
+    JSONNT_UINT,
+    JSONNT_FLOAT_DOUBLE
 };
 
 class JSONValue
@@ -31,6 +39,9 @@ class JSONValue
     tolua_outside void JSONValueSetObject @ SetObject(const JSONObject& value);
 
     JSONValueType GetValueType() const;
+    JSONNumberType GetNumberType() const;
+    String GetValueTypeName() const;
+    String GetNumberTypeName() const;
     bool IsNull() const;
     bool IsBool() const;
     bool IsNumber() const;
@@ -70,6 +81,10 @@ class JSONValue
     static const JSONObject emptyObject;
 
     tolua_readonly tolua_property__is_set bool null;
+    tolua_readonly tolua_property__get_set JSONValueType valueType;
+    tolua_readonly tolua_property__get_set JSONNumberType numberType;
+    tolua_readonly tolua_property__get_set String valueTypeName;
+    tolua_readonly tolua_property__get_set String numberTypeName;
 };
 
 ${

+ 61 - 0
Source/Urho3D/Resource/JSONValue.cpp

@@ -23,6 +23,7 @@
 #include "../Precompiled.h"
 
 #include "../Core/Context.h"
+#include "../Core/StringUtils.h"
 #include "../IO/Log.h"
 #include "../Resource/JSONValue.h"
 
@@ -31,6 +32,26 @@
 namespace Urho3D
 {
 
+static const char* valueTypeNames[] =
+{
+    "Null",
+    "Bool",
+    "Number",
+    "String",
+    "Array",
+    "Object",
+    0
+};
+
+static const char* numberTypeNames[] =
+{
+    "NaN",
+    "Int",
+    "Unsigned",
+    "Real",
+    0
+};
+
 const JSONValue JSONValue::EMPTY;
 const JSONArray JSONValue::emptyArray;
 const JSONObject JSONValue::emptyObject;
@@ -152,6 +173,16 @@ JSONNumberType JSONValue::GetNumberType() const
     return (JSONNumberType)(type_ & 0xffff);
 }
 
+String JSONValue::GetValueTypeName() const
+{
+    return GetValueTypeName(GetValueType());
+}
+
+String JSONValue::GetNumberTypeName() const
+{
+    return GetNumberTypeName(GetNumberType());
+}
+
 JSONValue& JSONValue::operator [](unsigned index)
 {
     // Convert to array type
@@ -586,4 +617,34 @@ VariantVector JSONValue::GetVariantVector() const
     return variantVector;
 }
 
+String JSONValue::GetValueTypeName(JSONValueType type)
+{
+    return valueTypeNames[type];
+}
+
+String JSONValue::GetNumberTypeName(JSONNumberType type)
+{
+    return numberTypeNames[type];
+}
+
+JSONValueType JSONValue::GetValueTypeFromName(const String& typeName)
+{
+    return GetValueTypeFromName(typeName.CString());
+}
+
+JSONValueType JSONValue::GetValueTypeFromName(const char* typeName)
+{
+    return (JSONValueType)GetStringListIndex(typeName, valueTypeNames, JSON_NULL);
+}
+
+JSONNumberType JSONValue::GetNumberTypeFromName(const String& typeName)
+{
+    return GetNumberTypeFromName(typeName.CString());
+}
+
+JSONNumberType JSONValue::GetNumberTypeFromName(const char* typeName)
+{
+    return (JSONNumberType)GetStringListIndex(typeName, numberTypeNames, JSONNT_NAN);
+}
+
 }

+ 23 - 3
Source/Urho3D/Resource/JSONValue.h

@@ -22,6 +22,8 @@
 
 #pragma once
 
+#include "../Core/Variant.h"
+
 namespace Urho3D
 {
 
@@ -39,7 +41,7 @@ enum JSONValueType
     /// JSON array type.
     JSON_ARRAY,
     /// JSON object type.
-    JSON_OBJECT,
+    JSON_OBJECT
 };
 
 /// JSON number type.
@@ -52,7 +54,7 @@ enum JSONNumberType
     /// Unsigned integer.
     JSONNT_UINT,
     /// Float or double.
-    JSONNT_FLOAT_DOUBLE,
+    JSONNT_FLOAT_DOUBLE
 };
 
 class JSONValue;
@@ -166,6 +168,11 @@ public:
     JSONValueType GetValueType() const;
     /// Return number type.
     JSONNumberType GetNumberType() const;
+    /// Return value type's name.
+    String GetValueTypeName() const;
+    /// Return number type's name.
+    String GetNumberTypeName() const;
+
     /// Check is null.
     bool IsNull() const { return GetValueType() == JSON_NULL; }
     /// Check is boolean.
@@ -268,6 +275,19 @@ public:
     /// Empty JSON object.
     static const JSONObject emptyObject;
 
+    /// Return name corresponding to a value type.
+    static String GetValueTypeName(JSONValueType type);
+    /// Return name corresponding to a number type.
+    static String GetNumberTypeName(JSONNumberType type);
+    /// Return a value type from name; null if unrecognized.
+    static JSONValueType GetValueTypeFromName(const String& typeName);
+    /// Return a value type from name; null if unrecognized.
+    static JSONValueType GetValueTypeFromName(const char* typeName);
+    /// Return a number type from name; NaN if unrecognized.
+    static JSONNumberType GetNumberTypeFromName(const String& typeName);
+    /// Return a value type from name; NaN if unrecognized.
+    static JSONNumberType GetNumberTypeFromName(const char* typeName);
+
 private:
     /// type.
     unsigned type_;
@@ -286,4 +306,4 @@ private:
     };
 };
 
-}
+}