SmartOS/Message/Json.h

231 lines
4.2 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef __Json_H__
#define __Json_H__
#include "Core\Type.h"
#include "Core\SString.h"
#include "Core\List.h"
#include "Core\Dictionary.h"
/*
一个Json对象内部包含有一个字符串读取成员就是截取子字符串构建新的Json对象。
*/
enum class JsonType : byte
{
null,
object,
array,
string,
boolean,
integer,
Float
};
// Json对象
class Json
{
public:
// 构造只读实例
Json(cstring str);
Json(cstring str, int len);
Json(const String& value);
// 值类型
JsonType Type() const;
bool IsNull() const;
// 获取值
String AsString() const;
bool AsBoolean() const;
int AsInt() const;
float AsFloat() const;
double AsDouble() const;
// 读取成员。找到指定成员,并用它的值构造一个新的对象
const Json operator[](cstring key) const;
// 特殊支持数组
int Length() const;
const Json operator[](int index) const;
//Json& operator[](int index);
Json();
// 设置输出缓冲区
//Json(String& value);
Json(char* buf, int len);
// 添加成员
Json& Add(cstring key, cstring value);
Json& Add(cstring key, bool value);
Json& Add(cstring key, int value);
Json& Add(cstring key, float value);
Json& Add(cstring key, double value);
Json& Add(cstring key, const String& value);
// 添加数组成员
//Json& Add(const Json& value);
Json& Add(cstring value);
Json& Add(bool value);
Json& Add(int value);
Json& Add(float value);
Json& Add(double value);
Json& Add(const String& value);
// 添加对象成员
Json& Add(cstring key, const Json& value);
// 特殊处理字符串,避免隐式转换
//Json& Add(cstring key, cstring value);
//Json AddObject(cstring key);
String ToString() const;
void Show(bool newline = false) const;
#if DEBUG
static void Test();
#endif
private:
String _str;
Json Find(cstring key) const;
void AddKey(cstring key);
};
/*
// Json值类型
enum ValueType
{
INT,
FLOAT,
BOOL,
STRING,
OBJECT,
ARRAY,
NIL
};
class JValue;
// Json对象
class JObject
{
public:
JObject();
JObject(const JObject& o);
JObject(JObject&& o);
JObject& operator=(const JObject& o);
JObject& operator=(JObject&& o);
~JObject();
JValue& operator[] (cstring key);
const JValue& operator[] (cstring key) const;
// 插入
void Add(cstring key, JValue& value);
// 大小
int size() const;
String ToString() const;
protected:
// 内部容器
Dictionary<cstring, JValue*> _items;
};
// Json数组
class JArray
{
public:
JArray();
~JArray();
JArray(const JArray& a);
JArray(JArray&& a);
JArray& operator=(const JArray& a);
JArray& operator=(JArray&& a);
JValue& operator[] (uint i);
const JValue& operator[] (uint i) const;
// 添加
void Add(const JValue& n);
// 大小
int size() const;
String ToString() const;
protected:
// 内部容器
List<JValue*> _array;
};
// Json值
class JValue
{
public:
JValue();
JValue(const JValue& v);
JValue(Int64 i);
JValue(int i);
JValue(double f);
JValue(bool b);
JValue(cstring s);
JValue(const String& s);
JValue(const JObject& o);
JValue(const JArray& a);
JValue(JValue&& v);
JValue(String&& s);
JValue(JObject&& o);
JValue(JArray&& a);
ValueType type() const
{
return type_t;
}
JValue& operator[] (cstring key);
const JValue& operator[] (cstring key) const;
JValue& operator[] (uint i);
const JValue& operator[] (uint i) const;
JValue& operator=(const JValue& v);
JValue& operator=(JValue&& v);
explicit operator double() const { return float_v; }
explicit operator int() const { return (int)int_v; }
explicit operator bool() const { return bool_v; }
explicit operator String() const { return string_v; }
operator JObject () const { return object_v; }
operator JArray () const { return array_v; }
double as_float() const { return float_v; }
int as_int() const { return (int)int_v; }
bool as_bool() const { return bool_v; }
String as_string() const { return string_v; }
String ToString() const;
protected:
double float_v;
Int64 int_v;
bool bool_v;
String string_v;
JObject object_v;
JArray array_v;
ValueType type_t;
};
*/
#endif