增加Json类结果,未完全编译通过

This commit is contained in:
Stone 2016-09-13 18:18:25 +00:00
parent fba440c0ec
commit 6df6b17225
2 changed files with 524 additions and 0 deletions

374
Message/Json.cpp Normal file
View File

@ -0,0 +1,374 @@
#include "Json.h"
JValue::JValue() : type_t(NIL) { }
JValue::JValue(Int64 i) : int_v(i), type_t(INT) { }
JValue::JValue(int i) : int_v(i), type_t(INT) { }
JValue::JValue(double f) : float_v(f), type_t(FLOAT) { }
JValue::JValue(bool b) : bool_v(b), type_t(BOOL) { }
JValue::JValue(cstring s) : string_v(s), type_t(STRING) { }
JValue::JValue(const String& s) : string_v(s.GetBuffer()), type_t(STRING) { }
JValue::JValue(const JObject& o) : object_v(o), type_t(OBJECT) { }
JValue::JValue(const JArray& o) : array_v(o), type_t(ARRAY) { }
JValue::JValue(String&& s) : string_v(s), type_t(STRING) { }
JValue::JValue(JObject&& o) : object_v(o), type_t(OBJECT) { }
JValue::JValue(JArray&& o) : array_v(o), type_t(ARRAY) { }
JValue::JValue(const JValue& v)
{
switch(v.type())
{
case INT:
int_v = v.int_v;
type_t = INT;
break;
case FLOAT:
float_v = v.float_v;
type_t = FLOAT;
break;
case BOOL:
bool_v = v.bool_v;
type_t = BOOL;
break;
case NIL:
type_t = NIL;
break;
case STRING:
string_v = v.string_v;
type_t = STRING;
break;
case ARRAY:
array_v = v.array_v;
type_t = ARRAY;
break;
case OBJECT:
object_v = v.object_v;
type_t = OBJECT;
break;
}
}
JValue::JValue(JValue&& v)
{
switch(v.type())
{
case INT:
int_v = (v.int_v);
type_t = INT;
break;
case FLOAT:
float_v = (v.float_v);
type_t = FLOAT;
break;
case BOOL:
bool_v = (v.bool_v);
type_t = BOOL;
break;
case NIL:
type_t = NIL;
break;
case STRING:
string_v= (v.string_v);
type_t = STRING;
break;
case ARRAY:
array_v = (v.array_v);
type_t = ARRAY;
break;
case OBJECT:
object_v= (v.object_v);
type_t = OBJECT;
break;
}
}
JValue& JValue::operator=(const JValue& v)
{
switch(v.type())
{
case INT:
int_v = v.int_v;
type_t = INT;
break;
case FLOAT:
float_v = v.float_v;
type_t = FLOAT;
break;
case BOOL:
bool_v = v.bool_v;
type_t = BOOL;
break;
case NIL:
type_t = NIL;
break;
case STRING:
string_v = v.string_v;
type_t = STRING;
break;
case ARRAY:
array_v = v.array_v;
type_t = ARRAY;
break;
case OBJECT:
object_v = v.object_v;
type_t = OBJECT;
break;
}
return *this;
}
JValue& JValue::operator=(JValue&& v)
{
switch(v.type())
{
case INT:
int_v = (v.int_v);
type_t = INT;
break;
case FLOAT:
float_v = (v.float_v);
type_t = FLOAT;
break;
case BOOL:
bool_v = (v.bool_v);
type_t = BOOL;
break;
case NIL:
type_t = NIL;
break;
case STRING:
string_v = (v.string_v);
type_t = STRING;
break;
case ARRAY:
array_v = (v.array_v);
type_t = ARRAY;
break;
case OBJECT:
object_v = (v.object_v);
type_t = OBJECT;
break;
}
return *this;
}
JValue& JValue::operator[] (cstring key)
{
if (type() != OBJECT)
throw ("JValue not an object");
return object_v[key];
}
const JValue& JValue::operator[] (cstring key) const
{
if (type() != OBJECT)
throw ("JValue not an object");
return object_v[key];
}
JValue& JValue::operator[] (uint i)
{
if (type() != ARRAY)
throw ("JValue not an array");
return array_v[i];
}
const JValue& JValue::operator[] (uint i) const
{
if (type() != ARRAY)
throw ("JValue not an array");
return array_v[i];
}
JObject::JObject() { }
JObject::~JObject() { }
JObject::JObject(const JObject& o) : _items(o._items) { }
JObject::JObject(JObject&& o) : _items((o._items)) { }
JObject& JObject::operator=(const JObject& o)
{
_items = o._items;
return *this;
}
JObject& JObject::operator=(JObject&& o)
{
_items = (o._items);
return *this;
}
JValue& JObject::operator[] (cstring key)
{
return *_items[key];
}
const JValue& JObject::operator[] (cstring key) const
{
return *_items[key];
}
void JObject::Add(cstring key, JValue& value)
{
_items.Add(key, &value);
}
uint JObject::size() const
{
return _items.Count();
}
String& JObject::ToStr(String& str) const
{
auto& keys = _items.Keys();
auto& vals = _items.Values();
str += '{';
for(int i=0; i<size(); i++)
{
if(i > 0) str += ',';
auto key = (cstring)keys[i];
str = str + '"' + key + '"' + ':';
auto& val = *(JValue*)vals[i];
val.ToStr(str);
}
str += '}';
return str;
}
JArray::JArray() { }
JArray::~JArray() { }
JArray::JArray(const JArray& a) : _array(a._array) { }
JArray::JArray(JArray&& a) : _array((a._array)) { }
JArray& JArray::operator=(const JArray& a)
{
_array = a._array;
return *this;
}
JArray& JArray::operator=(JArray&& a)
{
_array = (a._array);
return *this;
}
JValue& JArray::operator[] (uint i)
{
return *_array[i];
}
const JValue& JArray::operator[] (uint i) const
{
return *_array[i];
}
uint JArray::size() const
{
return _array.Count();
}
void JArray::Add(const JValue& v)
{
_array.Add(&(JValue&)v);
}
String& JArray::ToStr(String& str) const
{
str += '[';
for(int i=0; i<size(); i++)
{
if(i > 0) str += ',';
auto& v = (*this)[i];
v.ToStr(str);
}
str += ']';
return str;
}
String& JValue::ToStr(String& str) const
{
auto& v = *this;
switch(v.type())
{
case INT:
str += (int)v;
break;
case FLOAT:
str += (double)v;
break;
case BOOL:
str += ((bool)v ? "true" : "false");
break;
case NIL:
str += "null";
break;
case STRING:
str = str + '"' + (String)v + '"';
break;
case ARRAY:
((JArray)v).ToStr(str);
break;
case OBJECT:
((JObject)v).ToStr(str);
break;
}
return str;
}

150
Message/Json.h Normal file
View File

@ -0,0 +1,150 @@
#ifndef __Json_H__
#define __Json_H__
#include "Type.h"
#include "SString.h"
#include "List.h"
#include "Dictionary.h"
/** Json值类型 */
enum ValueType
{
INT, // JSON's int
FLOAT, // JSON's float 3.14 12e-10
BOOL, // JSON's boolean (true, false)
STRING, // JSON's string " ... " or (not really JSON) ' ... '
OBJECT, // JSON's object { ... }
ARRAY, // JSON's array [ ... ]
NIL // JSON's null
};
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);
// 大小
uint size() const;
String& ToStr(String& str) 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);
// 大小
uint size() const;
String& ToStr(String& str) 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_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_v; }
bool as_bool() const { return bool_v; }
String as_string() const { return string_v; }
String& ToStr(String& str) const;
protected:
double float_v;
Int64 int_v;
bool bool_v;
String string_v;
JObject object_v;
JArray array_v;
ValueType type_t;
};
#endif