mirror of
				https://github.com/VCMP-SqMod/SqMod.git
				synced 2025-11-04 00:07:19 +01:00 
			
		
		
		
	Minor updates to the JSON module.
This commit is contained in:
		@@ -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)
 | 
			
		||||
 
 | 
			
		||||
@@ -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.
 | 
			
		||||
*/
 | 
			
		||||
 
 | 
			
		||||
@@ -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)
 | 
			
		||||
    );
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user