mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-08-24 06:51:10 +02:00
Updated Examples (markdown)
34
Examples.md
34
Examples.md
@@ -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);
|
||||
}
|
||||
```
|
Reference in New Issue
Block a user