Home » U++ Library support » U++ Core » Custom array Jsonize
Custom array Jsonize [message #36226] |
Mon, 14 May 2012 18:38 |
|
koldo
Messages: 3394 Registered: August 2008
|
Senior Veteran |
|
|
Hello all
I wanted to Jsonize a custom array but I do not know how to do it. Could you help me?
This very simple and rude code saves correctly but it does not load the values:
#include <Core/Core.h>
using namespace Upp;
struct MyVector {
MyVector() {sz = 0;}
void resize(int _sz) {data.Alloc(_sz); sz = _sz;}
int size() {return sz;}
double &operator()(int i) {return data[i];}
Buffer<double> data;
int sz;
};
void Jsonize(JsonIO &xml, MyVector &vec) {
int sz;
if(xml.IsStoring())
sz = vec.size();
xml("size", sz);
if(xml.IsLoading())
vec.resize(sz);
for(int r = 0; r < sz; r++)
xml("", vec(r));
}
CONSOLE_APP_MAIN
{
StdLogSetup(LOG_COUT);
MyVector v, w;
v.resize(3);
for (int i = 0; i < 3; ++i)
v(i) = i;
String jso = StoreAsJson(v);
LoadFromJson(w, jso);
LOG(w(2));
getchar();
}
I have also tried code based in JsonizeArray() implementation but in my case it raises exceptions.
void JsonizeArray(JsonIO& io, MyVector & array)
{
if(io.IsLoading()) {
const Value& va = io.Get();
array.resize(va.GetCount());
for(int i = 0; i < va.GetCount(); i++) {
JsonIO jio(va[i]);
Jsonize(jio, array(i));
}
}
else {
Vector<Value> va;
va.SetCount(array.size());
for(int i = 0; i < array.size(); i++) {
JsonIO jio;
Jsonize(jio, array(i));
jio.Put(va[i]);
}
io.Set(ValueArray(va)); // It raises an "IsStoring() && !map" exception here
}
}
Best regards
Iñaki
[Updated on: Mon, 14 May 2012 18:39] Report message to a moderator
|
|
|
|
Goto Forum:
Current Time: Sat Sep 21 05:21:26 CEST 2024
Total time taken to generate the page: 0.09721 seconds
|