summaryrefslogtreecommitdiff
path: root/src/app
diff options
context:
space:
mode:
authorDaniel Friesel <derf@finalrewind.org>2018-11-12 13:47:44 +0100
committerDaniel Friesel <derf@finalrewind.org>2018-11-12 13:47:44 +0100
commitee720ed3bcc18fad43127d27e9d57781ecc91393 (patch)
tree1d392d5d5613bd6fa2d6b7572dc4c1744139605a /src/app
parent96462e6b5be917a5146eaae62234432f1030898f (diff)
prototest: json serialization
Diffstat (limited to 'src/app')
-rw-r--r--src/app/prototest/main.cc46
1 files changed, 43 insertions, 3 deletions
diff --git a/src/app/prototest/main.cc b/src/app/prototest/main.cc
index 05c3469..43c24f9 100644
--- a/src/app/prototest/main.cc
+++ b/src/app/prototest/main.cc
@@ -9,15 +9,48 @@
char buf[256];
+// TODOs
+//
+// Code -> JSON
+// Code -> XDR
+// Code -> MsgPack
+// Code -> ProtoBuf
+// Code -> CBOR
+//
+// JSON -> Code/Data
+// XDR -> Code/Data
+// MsgPack -> Code/Data
+// ProtoBuf -> Code/Data
+// CBOR -> Code/Data
+
void loop(void)
{
+ static unsigned int ts = 0;
char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
ArduinoJson::StaticJsonBuffer<200> jsonBuffer;
ArduinoJson::JsonObject& root = jsonBuffer.parseObject(json);
const char *sensor = root["sensor"];
kout << "sensor: " << sensor << endl;
+ /*
+ * nlohmann modernjson serialization
+ */
+
+ nlohmann::json js1;
+ js1["sensor"] = "gps";
+ js1["time"] = ts;
+ js1["data"] = {48.756080, 2.302038};
+ kout << js1.dump() << endl;
+
+ nlohmann::json js2 = {
+ {"sensor", "gps"},
+ {"time", ts},
+ {"data", {48.756080, 2.302038} }
+ };
+ kout << js2.dump() << endl;
+
nlohmann::json j = R"({"compact": true, "schema": 0})"_json;
+ j["ts"] = ts;
std::vector<std::uint8_t> v_cbor = nlohmann::json::to_cbor(j);
kout << "CBOR vector is " << hex;
@@ -26,15 +59,21 @@ void loop(void)
}
kout << endl;
- kout << "manual JSON: {\"sensor\":\"gps\",\"time\":" << dec << 1351824120;
+ kout << "manual JSON: {\"sensor\":\"gps\",\"time\":" << dec << ts;
kout << ",\"data\":[" << 48.756080 << "," << 2.302038 << "]}" << endl;
+ /*
+ * nlohmann modernjson deserialization
+ */
+
+ auto jd1 = R"({"compact": true, "schema": 0})"_json;
+
BufferOutput<XDRStream> foostream(buf);
XDRInput input(buf);
char test[] = "Obai World!";
- foostream << 123 << -2 << 123456 << 0 << 4294967296 << 0;
+ foostream << 123 << -2 << ts << 0 << 4294967296 << 0;
foostream.setNextArrayLen(3);
foostream << fixed << "Hai";
foostream.setNextArrayLen(sizeof(test));
@@ -50,7 +89,7 @@ void loop(void)
kout << dec;
kout << "foostream = " << input.get_uint32() << " = " << 123;
kout << ", " << input.get_int32() << " = " << -2;
- kout << ", " << input.get_uint32() << " = " << 123456;
+ kout << ", " << input.get_uint32() << " = " << ts;
kout << ", " << input.get_uint32();
kout << ", " << input.get_uint64();
kout << ", " << input.get_uint32();
@@ -63,6 +102,7 @@ void loop(void)
#ifdef TIMER_S
kout << dec << uptime.get_s() << endl;
#endif
+ ts++;
}
int main(void)