Core: Jsonize finished

git-svn-id: svn://ultimatepp.org/upp/trunk@4654 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2012-03-03 22:59:48 +00:00
parent 3478676fa7
commit b51db10ed9
7 changed files with 97 additions and 11 deletions

View file

@ -88,6 +88,15 @@ Color::Color(RGBA rgba)
}
}
void Color::Jsonize(JsonIO& jio)
{
int r = GetR();
int g = GetG();
int b = GetB();
jio("red", r)("green", g)("blue", b);
*this = Color(r, g, b);
}
RGBA operator*(int alpha, Color c)
{
RGBA r;

View file

@ -45,6 +45,7 @@ public:
bool operator!=(Color c) const { return color != c.color; }
void Serialize(Stream& s) { s % color; }
void Jsonize(JsonIO& jio);
Color() { SetNull(); }
Color(int r, int g, int b) { color = RGB(r, g, b); }

View file

@ -249,10 +249,16 @@ template<> void Jsonize(JsonIO& io, Date& var)
return;
}
if(IsString(v)) {
Date d = ScanDate("ymd", (String)v);
if(!IsNull(d)) {
var = d;
return;
String text = v;
if(text.GetCount() > 6) {
Date d;
d.year = ScanInt(text.Left(4));
d.month = ScanInt(text.Mid(4, 2));
d.day = ScanInt(text.Mid(6));
if(var.IsValid()) {
var = d;
return;
}
}
}
throw JsonizeError("string expected for Date value");
@ -261,7 +267,7 @@ template<> void Jsonize(JsonIO& io, Date& var)
if(IsNull(var))
io.Set(Null);
else
io.Set(Format("%04d-%02d-%02d", var.year, var.month, var.day));
io.Set(Format("%04d%02d%02d", var.year, var.month, var.day));
}
template<> void Jsonize(JsonIO& io, Time& var)
@ -273,10 +279,19 @@ template<> void Jsonize(JsonIO& io, Time& var)
return;
}
if(IsString(v)) {
Time d = ScanTime("ymd", (String)v);
if(!IsNull(d)) {
var = d;
return;
String text = v;
if(text.GetCount() > 15) {
Time tm;
tm.year = ScanInt(text.Left(4));
tm.month = ScanInt(text.Mid(4, 2));
tm.day = ScanInt(text.Mid(6, 2));
tm.hour = ScanInt(text.Mid(9, 2));
tm.minute = ScanInt(text.Mid(12, 2));
tm.second = ScanInt(text.Mid(15));
if(var.IsValid()) {
var = tm;
return;
}
}
}
throw JsonizeError("string expected for Time value");
@ -285,7 +300,7 @@ template<> void Jsonize(JsonIO& io, Time& var)
if(IsNull(var))
io.Set(Null);
else
io.Set(Format("%04d-%02d-%02d %02d:%02d:%02d",
io.Set(Format("%04d%02d%02d`T%02d:%02d:%02d",
var.year, var.month, var.day, var.hour, var.minute, var.second));
}

View file

@ -19,6 +19,17 @@ void Uuid::Serialize(Stream& s) {
s / version % a % b %c % d;
}
void Uuid::Jsonize(JsonIO& jio)
{
String h;
if(jio.IsStoring()) {
h = Format(*this);
jio.Set(h);
}
else
*this = ScanUuid((String)jio.Get());
}
Uuid Uuid::Create() {
Uuid ud;
ud.a = Random();

View file

@ -3,6 +3,7 @@ struct Uuid : AssignValueTypeNo<Uuid, 50, Moveable<Uuid> > {
void Serialize(Stream& s);
void Xmlize(XmlIO& xio);
void Jsonize(JsonIO& jio);
bool IsNullInstance() const { return a == 0 && b == 0 && c == 0 && d == 0; }
void SetNull() { a = b = c = d = 0; }

View file

@ -36,6 +36,11 @@ void ValueArray::Data::Serialize(Stream& s)
s % data;
}
void ValueArray::Data::Jsonize(JsonIO& jio)
{
Upp::Jsonize(jio, data);
}
void ValueArray::Data::Xmlize(XmlIO& io)
{
Upp::Xmlize(io, data);
@ -156,7 +161,7 @@ void ValueArray::Serialize(Stream& s) {
void ValueArray::Jsonize(JsonIO& jio)
{
if(s.IsLoading()) {
if(jio.IsLoading()) {
data->Release();
Create();
}
@ -245,6 +250,38 @@ void ValueMap::Data::Xmlize(XmlIO& xio)
Upp::Xmlize(xio, value);
}
void ValueMap::Data::Jsonize(JsonIO& jio)
{
if(jio.IsStoring()) {
ValueArray va;
int n = min(value.GetCount(), key.GetCount());
for(int i = 0; i < n; i++) {
ValueMap m;
m.Add("key", StoreAsJsonValue(key[i]));
m.Add("value", StoreAsJsonValue(value[i]));
va.Add(m);
}
jio.Set(va);
}
else {
Value va = jio.Get();
DDUMP(va);
key.Clear();
value.Clear();
for(int i = 0; i < va.GetCount(); i++) {
Value k, v;
DDUMP(va[i]);
DDUMP(va[i]["key"]);
LoadFromJsonValue(k, va[i]["key"]);
LoadFromJsonValue(v, va[i]["value"]);
DDUMP(k);
DDUMP(v);
key.Add(k);
value.Add(v);
}
}
}
unsigned ValueMap::Data::GetHashValue() const {
CombineHash w(key.GetCount());
for(int i = 0; i < key.GetCount(); i++)
@ -365,6 +402,16 @@ void ValueMap::Serialize(Stream& s) {
data->Serialize(s);
}
void ValueMap::Jsonize(JsonIO& jio)
{
if(jio.IsLoading()) {
data->Release();
Create();
}
data->Jsonize(jio);
}
ValueMap::~ValueMap() {
ASSERT(data->GetRefCount() > 0);
data->Release();

View file

@ -239,6 +239,7 @@ class ValueMap : AssignValueTypeNo<ValueMap, VALUEMAP_V, Moveable<ValueMap> >{
virtual bool IsNull() const;
virtual void Serialize(Stream& s);
virtual void Xmlize(XmlIO& xio);
virtual void Jsonize(JsonIO& jio);
virtual unsigned GetHashValue() const;
virtual bool IsEqual(const Value::Void *p);
virtual String AsString() const;
@ -311,6 +312,7 @@ public:
unsigned GetHashValue() const { return data->GetHashValue(); }
void Serialize(Stream& s);
void Jsonize(JsonIO& jio);
String ToString() const { return data->AsString(); }
bool operator==(const ValueMap& v) const;