How serialize/deserialize a JSON vector of data structure record?

Apr 2, 2014 at 2:56 PM
Hi,

I am trying to use Json to serialize/de-serialize vector of json record. I am new at this JSON.

How do you serialize/de-serialize entire json vector into stream?

Thank You

Mike

Example:
utility::stringstream_t streamOutput;
std::vector<json::value> jsonVector;

for (int i = 0; i < updatelist.GetCount(); i++)
{
FILETIME *ftFileModify;
SYSTEMTIME stUTC;
json::value jsonObj;

__int64 temp = updatelist[i].GetLastWriteTime();
 ftFileModify = (FILETIME *)&temp;
 FileTimeToSystemTime(ftFileModify, &stUTC);

  jsonObj[L"wYear"] = json::value::number(stUTC.wYear);
  jsonObj[L"wMonth"] = json::value::number(stUTC.wMonth);
  jsonObj[L"wDayOfWeek"] = json::value::number(stUTC.wDayOfWeek);
  jsonObj[L"wDay"] = json::value::number(stUTC.wDay);
  jsonObj[L"wHour"] = json::value::number(stUTC.wHour);
  jsonObj[L"wMinute"] = json::value::number(stUTC.wMinute);
  jsonObj[L"wSecond"] = json::value::number(stUTC.wSecond);
  jsonObj[L"wMilliseconds"] = json::value::number(stUTC.wMilliseconds);
  jsonVector.push_back(jsonObj);
}

//TODO: How to Serialize jsonVector into text stream?
Coordinator
Apr 4, 2014 at 4:36 PM
Hi Mike,

Looking at the code you posted it looks like you are trying to create a json array of json objects. You can create a json array with our library by using one of the factory function overloads json::value::array(…). In particular there is one that takes a std::vector<json::value>. Another option is to let our library directly handle the storage for the json array elements in our object model. This could be done with something like the following:
utility::stringstream_t streamOutput; 
json::value jsonArray;

for (int i = 0; i < updatelist.GetCount(); i++) 
{
  FILETIME *ftFileModify;
  SYSTEMTIME stUTC;
  json::value jsonObj;

  __int64 temp = updatelist[i].GetLastWriteTime();
  ftFileModify = (FILETIME *)&temp;
  FileTimeToSystemTime(ftFileModify, &stUTC);

  jsonObj[L"wYear"] = json::value::number(stUTC.wYear);
  jsonObj[L"wMonth"] = json::value::number(stUTC.wMonth);
  jsonObj[L"wDayOfWeek"] = json::value::number(stUTC.wDayOfWeek);
  jsonObj[L"wDay"] = json::value::number(stUTC.wDay);
  jsonObj[L"wHour"] = json::value::number(stUTC.wHour);
  jsonObj[L"wMinute"] = json::value::number(stUTC.wMinute);
  jsonObj[L"wSecond"] = json::value::number(stUTC.wSecond);
  jsonObj[L"wMilliseconds"] = json::value::number(stUTC.wMilliseconds);
  jsonArray[i] = std::move(jsonObj);
}
To serialize any json value with our library simply call one of the json::value::serialize() overloads. For example to serialize to the stringstream you have just call the following:
jsonArray.serialize(streamOutput); 
For more small code snippets using our Json library you can also take a look here in our documentation. Or at the test cases. Let me know if you have any questions.

Steve