Updated Examples (markdown)

domonetic
2015-11-05 17:32:22 -03:00
parent 6f516a4eac
commit 8f350ddbce

@@ -102,4 +102,38 @@ for(JsonObject::iterator it=object.begin(); it!=object.end(); ++it)
value = it->value.as<const char*>();
}
```
## Example 4: Serialize and Deserialize
Here is the canonical example for serializing and deserializing with ArduinoJson.
By following this example, you are making the best usage of your memory and you maintain a good software design.
```c++
struct SensorData {
const char* name;
int time;
float value;
};
#define SENSORDATA_JSON_SIZE (JSON_OBJECT_SIZE(3))
bool deserialize(SensorData& data, char* json)
{
StaticJsonBuffer<SENSORDATA_JSON_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
data.name = root["name"];
data.time = root["time"];
data.value = root["value"];
return root.success();
}
void serialize(const SensorData& data, char* json, size_t maxSize)
{
StaticJsonBuffer<SENSORDATA_JSON_SIZE> jsonBuffer;
JsonObject& root = jsonBuffer.createObject();
root["name"] = data.name;
root["time"] = data.time;
root["value"] = data.value;
root.printTo(json, maxSize);
}
```