From 22fc6c54c2360f35aafe48f875456948a8ac20dd Mon Sep 17 00:00:00 2001 From: Sandu Liviu Catalin Date: Mon, 20 Jun 2016 18:53:09 +0300 Subject: [PATCH] Minor updates to the JSON module. --- modules/json/Common.cpp | 43 +++++++++++++++++++++ modules/json/Common.hpp | 29 ++++++++++++-- modules/json/JValue.cpp | 18 ++++++++- modules/json/JValue.hpp | 83 +++++++++++++++++++++++++++++++++++++++++ modules/json/Module.cpp | 10 +++++ 5 files changed, 178 insertions(+), 5 deletions(-) diff --git a/modules/json/Common.cpp b/modules/json/Common.cpp index 709a9e56..03c2b451 100644 --- a/modules/json/Common.cpp +++ b/modules/json/Common.cpp @@ -41,6 +41,49 @@ void SqThrowLast(HSQUIRRELVM vm, CSStr msg) // Throw the resulting error message STHROWF("%s [%s]", msg, val.mPtr); } + +// ------------------------------------------------------------------------------------------------ +Object SqFromJSON(HSQUIRRELVM vm, json_t * jval) +{ + switch (json_typeof(jval)) + { + case JSON_OBJECT: + { + + } break; + case JSON_ARRAY: + { + + } break; + case JSON_STRING: + { + + } break; + case JSON_INTEGER: + { + + } break; + case JSON_REAL: + { + + } break; + case JSON_TRUE: + { + + } break; + case JSON_FALSE: + { + + } break; + case JSON_NULL: + { + + } break; + default: STHROWF("Unknown JSON value type"); + } + + return Object(); +} // ------------------------------------------------------------------------------------------------ json_t * SqToJSON(HSQUIRRELVM vm, SQInteger idx) diff --git a/modules/json/Common.hpp b/modules/json/Common.hpp index ec1efaaa..0a5f4e12 100644 --- a/modules/json/Common.hpp +++ b/modules/json/Common.hpp @@ -26,6 +26,13 @@ namespace SqMod { #define SQJSON_VERSION_MINOR 0 #define SQJSON_VERSION_PATCH 1 +/* ------------------------------------------------------------------------------------------------ + * Forward declarations. +*/ +class JValue; +class JArray; +class JObject; + /* ------------------------------------------------------------------------------------------------ * Implements RAII to free the strings obtained from dumps even after exceptions. */ @@ -56,11 +63,20 @@ struct CStrGuard }; /* ------------------------------------------------------------------------------------------------ - * Forward declarations. + * Class that represents an error occurred while parsing JSON data. */ -class JValue; -class JArray; -class JObject; +struct JError +{ +public: + + // -------------------------------------------------------------------------------------------- + json_error_t mErr; // The managed error instance. + + /* -------------------------------------------------------------------------------------------- + * Default constructor. + */ + JError(); +}; /* ------------------------------------------------------------------------------------------------ * Retrieve the string representation of JSON value type. @@ -75,6 +91,11 @@ inline CSStr JSONTypeStr(json_t * ptr) return (ptr == nullptr) ? _SC("unknown") : JSONTypeToStr(json_typeof(ptr)); } +/* ------------------------------------------------------------------------------------------------ + * Convert a script value from the stack to a JSON object. +*/ +Object SqFromJSON(HSQUIRRELVM vm, json_t * jval); + /* ------------------------------------------------------------------------------------------------ * Convert a script value from the stack to a JSON object. */ diff --git a/modules/json/JValue.cpp b/modules/json/JValue.cpp index 859a1660..15a340ab 100644 --- a/modules/json/JValue.cpp +++ b/modules/json/JValue.cpp @@ -28,6 +28,12 @@ Object JValue::ToString() const return Var< Object >(_SqVM, -1).value; } +// ------------------------------------------------------------------------------------------------ +Object JValue::GetValue() const +{ + return Object(); +} + // ================================================================================================ void Register_JValue(Table & jns) { @@ -40,7 +46,17 @@ void Register_JValue(Table & jns) .SquirrelFunc(_SC("_typename"), &JValue::Typename) .Func(_SC("_tostring"), &JValue::ToString) // Properties - //.Prop(_SC("Prop"), &JValue::Prop) + .Prop(_SC("IsObject"), &JValue::IsObject) + .Prop(_SC("IsArray"), &JValue::IsArray) + .Prop(_SC("IsString"), &JValue::IsString) + .Prop(_SC("IsInteger"), &JValue::IsInteger) + .Prop(_SC("IsReal"), &JValue::IsReal) + .Prop(_SC("IsNumber"), &JValue::IsNumber) + .Prop(_SC("IsTrue"), &JValue::IsTrue) + .Prop(_SC("IsFalse"), &JValue::IsFalse) + .Prop(_SC("IsBoolean"), &JValue::IsBoolean) + .Prop(_SC("IsNull"), &JValue::IsNull) + .Prop(_SC("Value"), &JValue::GetValue) // Member Methods //.Func(_SC("Func"), &JValue::Func) ); diff --git a/modules/json/JValue.hpp b/modules/json/JValue.hpp index b285b8de..d868e9a3 100644 --- a/modules/json/JValue.hpp +++ b/modules/json/JValue.hpp @@ -249,7 +249,90 @@ public: return (m_Ptr != nullptr); } + /* -------------------------------------------------------------------------------------------- + * See whether the managed value is object. + */ + bool IsObject() const + { + return json_is_object(m_Ptr); + } + /* -------------------------------------------------------------------------------------------- + * See whether the managed value is array. + */ + bool IsArray() const + { + return json_is_array(m_Ptr); + } + + /* -------------------------------------------------------------------------------------------- + * See whether the managed value is string. + */ + bool IsString() const + { + return json_is_string(m_Ptr); + } + + /* -------------------------------------------------------------------------------------------- + * See whether the managed value is integer. + */ + bool IsInteger() const + { + return json_is_integer(m_Ptr); + } + + /* -------------------------------------------------------------------------------------------- + * See whether the managed value is real number. + */ + bool IsReal() const + { + return json_is_real(m_Ptr); + } + + /* -------------------------------------------------------------------------------------------- + * See whether the managed value is number. + */ + bool IsNumber() const + { + return json_is_number(m_Ptr); + } + + /* -------------------------------------------------------------------------------------------- + * See whether the managed value is boolean true. + */ + bool IsTrue() const + { + return json_is_true(m_Ptr); + } + + /* -------------------------------------------------------------------------------------------- + * See whether the managed value is boolean false. + */ + bool IsFalse() const + { + return json_is_false(m_Ptr); + } + + /* -------------------------------------------------------------------------------------------- + * See whether the managed value is boolean. + */ + bool IsBoolean() const + { + return json_is_boolean(m_Ptr); + } + + /* -------------------------------------------------------------------------------------------- + * See whether the managed value is null. + */ + bool IsNull() const + { + return json_is_null(m_Ptr); + } + + /* -------------------------------------------------------------------------------------------- + * Retrieve the script equivalent of the managed value. + */ + Object GetValue() const; }; } // Namespace:: SqMod diff --git a/modules/json/Module.cpp b/modules/json/Module.cpp index a4b247c3..43692823 100644 --- a/modules/json/Module.cpp +++ b/modules/json/Module.cpp @@ -174,6 +174,16 @@ void RegisterAPI(HSQUIRRELVM vm) Register_JValue(jns); RootTable(vm).Bind(_SC("SqJSON"), jns); + + Sqrat::ConstTable(vm) + .Const(_SC("JSON_OBJECT"), JSON_OBJECT) + .Const(_SC("JSON_ARRAY"), JSON_ARRAY) + .Const(_SC("JSON_STRING"), JSON_STRING) + .Const(_SC("JSON_INTEGER"), JSON_INTEGER) + .Const(_SC("JSON_REAL"), JSON_REAL) + .Const(_SC("JSON_TRUE"), JSON_TRUE) + .Const(_SC("JSON_FALSE"), JSON_FALSE) + .Const(_SC("JSON_NULL"), JSON_NULL); } } // Namespace:: SqMod