| 1 | // ArduinoJson - https://arduinojson.org |
| 2 | // Copyright © 2014-2026, Benoit BLANCHON |
| 3 | // MIT License |
| 4 | // |
| 5 | // This example shows how to deserialize a MessagePack document with |
| 6 | // ArduinoJson. |
| 7 | // |
| 8 | // https://arduinojson.org/v7/example/msgpack-parser/ |
| 9 | |
| 10 | #include <ArduinoJson.h> |
| 11 | |
| 12 | void setup() { |
| 13 | // Initialize serial port |
| 14 | Serial.begin(9600); |
| 15 | while (!Serial) |
| 16 | continue; |
| 17 | |
| 18 | // Allocate the JSON document |
| 19 | JsonDocument doc; |
| 20 | |
| 21 | // The MessagePack input string |
| 22 | uint8_t input[] = {131, 166, 115, 101, 110, 115, 111, 114, 163, 103, 112, 115, |
| 23 | 164, 116, 105, 109, 101, 206, 80, 147, 50, 248, 164, 100, |
| 24 | 97, 116, 97, 146, 203, 64, 72, 96, 199, 58, 188, 148, |
| 25 | 112, 203, 64, 2, 106, 146, 230, 33, 49, 169}; |
| 26 | // This MessagePack document contains: |
| 27 | // { |
| 28 | // "sensor": "gps", |
| 29 | // "time": 1351824120, |
| 30 | // "data": [48.75608, 2.302038] |
| 31 | // } |
| 32 | |
| 33 | // Parse the input |
| 34 | DeserializationError error = deserializeMsgPack(doc, input); |
| 35 | |
| 36 | // Test if parsing succeeded |
| 37 | if (error) { |
| 38 | Serial.print("deserializeMsgPack() failed: "); |
| 39 | Serial.println(error.f_str()); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | // Fetch the values |
| 44 | // |
| 45 | // Most of the time, you can rely on the implicit casts. |
| 46 | // In other case, you can do doc["time"].as<long>(); |
| 47 | const char* sensor = doc["sensor"]; |
| 48 | long time = doc["time"]; |
| 49 | double latitude = doc["data"][0]; |
| 50 | double longitude = doc["data"][1]; |
| 51 | |
| 52 | // Print the values |
| 53 | Serial.println(sensor); |
| 54 | Serial.println(time); |
| 55 | Serial.println(latitude, 6); |
| 56 | Serial.println(longitude, 6); |
| 57 | } |
| 58 | |
| 59 | void loop() { |
| 60 | // not used in this example |
| 61 | } |