171 lines
7.4 KiB
C++
171 lines
7.4 KiB
C++
#pragma once
|
|
|
|
#include "dtcore/inc/dtstream.h"
|
|
|
|
namespace dt
|
|
{
|
|
struct _json_value;
|
|
struct _json_object_entry;
|
|
class JsonObjectEntry;
|
|
|
|
enum class JsonType
|
|
{
|
|
None,
|
|
Object,
|
|
Array,
|
|
Integer,
|
|
UnsignedInteger,
|
|
Double,
|
|
String,
|
|
Boolean,
|
|
Null
|
|
};
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// This class serves as a wrapper over the _json_value object from the
|
|
// Json library. It allows users to navigate through any type of Json
|
|
// object. You can also manually build your own JsonValue object and
|
|
// then serialize it as a string.
|
|
class JsonValue : public Serializable
|
|
{
|
|
public:
|
|
// Default constructor. Creates an empty/null value.
|
|
DT_CORE_DLL_ENTRY JsonValue();
|
|
// Create a JsonValue object from a given string.
|
|
DT_CORE_DLL_ENTRY explicit JsonValue(const String& stringValue);
|
|
DT_CORE_DLL_ENTRY explicit JsonValue(const char* stringValue);
|
|
// Creates a JsonValue of Boolean type, with the given bool value.
|
|
DT_CORE_DLL_ENTRY explicit JsonValue(bool boolVal);
|
|
// Creates a JsonValue of Integer type, with the given integer value.
|
|
DT_CORE_DLL_ENTRY explicit JsonValue(U64 intVal);
|
|
DT_CORE_DLL_ENTRY explicit JsonValue(U32 intVal);
|
|
DT_CORE_DLL_ENTRY explicit JsonValue(U16 intVal);
|
|
DT_CORE_DLL_ENTRY explicit JsonValue(U8 intVal);
|
|
DT_CORE_DLL_ENTRY explicit JsonValue(S64 intVal);
|
|
DT_CORE_DLL_ENTRY explicit JsonValue(S32 intVal);
|
|
DT_CORE_DLL_ENTRY explicit JsonValue(S16 intVal);
|
|
DT_CORE_DLL_ENTRY explicit JsonValue(S8 intVal);
|
|
// Creates a JsonValue of Double type, with the given double value.
|
|
DT_CORE_DLL_ENTRY explicit JsonValue(double doubleVal);
|
|
// Creates a JsonValue of Array type, with the content of the given Vector.
|
|
DT_CORE_DLL_ENTRY explicit JsonValue(const Vector<JsonValue>& arrayVal);
|
|
// Creates a JsonValue of Object type, with the content of the given Vector.
|
|
DT_CORE_DLL_ENTRY explicit JsonValue(const Vector<JsonObjectEntry>& objectVal);
|
|
// Copy constructor.
|
|
DT_CORE_DLL_ENTRY JsonValue(const JsonValue& rhs);
|
|
DT_CORE_DLL_ENTRY JsonValue(JsonValue&& rhs);
|
|
|
|
DT_CORE_DLL_ENTRY ~JsonValue();
|
|
|
|
// Copy operator.
|
|
DT_CORE_DLL_ENTRY JsonValue& operator=(const JsonValue& rhs);
|
|
// Comparison operators
|
|
DT_CORE_DLL_ENTRY bool operator==(const JsonValue& rhs) const;
|
|
DT_CORE_DLL_ENTRY bool operator!=(const JsonValue& rhs) const;
|
|
DT_CORE_DLL_ENTRY bool operator<(const JsonValue& rhs) const;
|
|
DT_CORE_DLL_ENTRY bool operator>(const JsonValue& rhs) const;
|
|
DT_CORE_DLL_ENTRY bool operator>=(const JsonValue& rhs) const;
|
|
DT_CORE_DLL_ENTRY bool operator<=(const JsonValue& rhs) const;
|
|
DT_CORE_DLL_ENTRY bool operator^(const JsonValue& rhs) const; // contained, return true if all elements on the left are present on the right
|
|
|
|
// Math operators
|
|
DT_CORE_DLL_ENTRY JsonValue operator+(const JsonValue& rhs);
|
|
DT_CORE_DLL_ENTRY JsonValue operator-(const JsonValue& rhs);
|
|
DT_CORE_DLL_ENTRY JsonValue operator*(const JsonValue& rhs);
|
|
DT_CORE_DLL_ENTRY JsonValue operator/(const JsonValue& rhs);
|
|
DT_CORE_DLL_ENTRY JsonValue operator%(const JsonValue& rhs);
|
|
|
|
// Parses a given Json expression and creates a JsonValue.
|
|
// Returns true if the parsing has succeeded and false
|
|
// otherwise.
|
|
DT_CORE_DLL_ENTRY static bool Parse(const String& jsonString, JsonValue* out);
|
|
|
|
// Checks if the current object is an empty/null object (no content).
|
|
DT_CORE_DLL_ENTRY bool IsEmpty() const;
|
|
// Returns the type of the current Json object.
|
|
DT_CORE_DLL_ENTRY JsonType GetType() const;
|
|
|
|
// Returns the boolean content of a Boolean type JsonValue.
|
|
// Logs an error if the object type is not Boolean.
|
|
DT_CORE_DLL_ENTRY bool GetBool() const;
|
|
// Returns the integer content of an Integer type JsonValue.
|
|
// Logs an error if the object type is not Integer.
|
|
DT_CORE_DLL_ENTRY U64 GetU64() const;
|
|
DT_CORE_DLL_ENTRY U32 GetU32() const;
|
|
DT_CORE_DLL_ENTRY U16 GetU16() const;
|
|
DT_CORE_DLL_ENTRY U8 GetU8() const;
|
|
DT_CORE_DLL_ENTRY S64 GetS64() const;
|
|
DT_CORE_DLL_ENTRY S32 GetS32() const;
|
|
DT_CORE_DLL_ENTRY S16 GetS16() const;
|
|
DT_CORE_DLL_ENTRY S8 GetS8() const;
|
|
// Returns the double content of a Double type JsonValue.
|
|
// Logs an error if the object type is not Double.
|
|
DT_CORE_DLL_ENTRY double GetDouble() const;
|
|
// Returns the string content of a String type JsonValue.
|
|
// Logs an error if the object type is not String.
|
|
DT_CORE_DLL_ENTRY String GetString() const;
|
|
// Returns the number of members or length of an Object or Array type JsonValue.
|
|
// Logs an error if the object type is not Object or Array.
|
|
DT_CORE_DLL_ENTRY int GetLength() const;
|
|
// Returns a value at a given index of an Array type JsonValue.
|
|
// Logs an error if the object type is not Array.
|
|
DT_CORE_DLL_ENTRY JsonValue GetArrayValue(unsigned int index) const;
|
|
// Returns a member name and value tuple of an Object type JsonValue.
|
|
// Logs an error if the object type is not Object.
|
|
DT_CORE_DLL_ENTRY JsonObjectEntry GetObjectEntry(unsigned int index) const;
|
|
// Returns a member name and value tuple of an Object type JsonValue.
|
|
// Logs an error if the object type is not Object.
|
|
DT_CORE_DLL_ENTRY JsonObjectEntry GetObjectEntry(const String& name) const;
|
|
|
|
// Serializes the current object in a valid Json string.
|
|
DT_CORE_DLL_ENTRY String ToString() const;
|
|
|
|
// Serialize implementation override for the Serializable interface.
|
|
DT_CORE_DLL_ENTRY virtual void Serialize(U8 version, Stream& stream) override;
|
|
DT_CORE_DLL_ENTRY virtual U8 GetSerializeVersion() const override { return 2; }
|
|
|
|
private:
|
|
friend class JsonObjectEntry;
|
|
friend class StreamJsonInput;
|
|
|
|
JsonValue(_json_value* externalJsonValue);
|
|
|
|
_json_value* GetExternalJsonValue() const { return m_ExternalJsonValue; }
|
|
void FreeExternalJsonValue();
|
|
|
|
private:
|
|
_json_value* m_ExternalJsonValue;
|
|
bool m_IsParsedValue;
|
|
};
|
|
|
|
// Stringify the Json value
|
|
DT_CORE_DLL_ENTRY String& operator<<(String& out, const JsonValue& obj);
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// A class that represents a wrapper on the Json library object _json_object_entry.
|
|
// It's basically a <name, value> tuple representing a member of an Object type
|
|
// JsonValue. This can be used for retrieving or creating an Object type JsonValue.
|
|
class JsonObjectEntry
|
|
{
|
|
public:
|
|
DT_CORE_DLL_ENTRY JsonObjectEntry(const String& name, const JsonValue& value);
|
|
|
|
// Fetches the name of the Json Object member.
|
|
DT_CORE_DLL_ENTRY const String& GetName() const { return m_Name; }
|
|
// Fetches the Json value of the Json Object member.
|
|
DT_CORE_DLL_ENTRY const JsonValue& GetValue() const { return m_Value; }
|
|
|
|
private:
|
|
friend JsonValue;
|
|
|
|
JsonObjectEntry(const _json_object_entry& objEntry);
|
|
|
|
private:
|
|
String m_Name;
|
|
JsonValue m_Value;
|
|
};
|
|
|
|
}
|
|
|