I need to build a json array such as:
{
"data":{
[
{
"input":"I1",
"state":false,
"channel":0,
"module_name":"OE_IOT_1016A"
},
{
"input":"I2",
"state":false,
"channel":1,
"module_name":"OE_IOT_1016A"
},
{
"input":"I3",
"state":false,
"channel":2,
"module_name":"OE_IOT_1"
}
],
"ttl":60,
"published_at":"2018-12-02T15:48:29.699Z",
"coreid":"2c003f000c47363339343638"
}
}
The form of the json array builder is as follows:
The block_cnt is to batch output Pub’s before the payload reached 256 (Hand calculated for now)
JsonWriterStatic<256> root;
root.startArray();
int block_cnt = 0;
for (int i = 0; i<15;i++){
// Crearte record
root.startObject();
// Crearte record
//root.insertKeyObject("rec");
root.insertKeyValue("input",inputs_alias[i]);
root.insertKeyValue("state", input_status[i]);
root.insertKeyValue("channel", i);
root.insertKeyValue("module_name", module_name);
root.finishObjectOrArray();
block_cnt++;
if (block_cnt == 3) {
root.finishObjectOrArray();
Particle.publish("inputs", root.getBuffer(),60,PRIVATE);
root.startArray();
block_cnt = 0;
}
}
root.finishObjectOrArray();
Particle.publish("inputs", root.getBuffer(),60,PRIVATE);
This outputs:
event: inputs
data: {“data”:"[{“input”:“I1”,“state”:false,“channel”:0,“module_name”:“OE_IOT_1016A”}{“input”:“I2”,“state”:false,“channel”:1,“module_name”:“OE_IOT_1016A”}{“input”:“I3”,“state”:false,“channel”:2,“module_name”:“OE_IOT_1016A”}][{“input”:“I4”,“state”:false,“channel”:3,“modu”,“ttl”:60,“published_at”:“2018-12-02T16:37:55.969Z”,“coreid”:“2c003f000c47363339343638”}
NO COMMAS’s BETWEEN OBJECTS
SOME RANDOM DATA AFTER THE ARRAY [{“input”:“I4”,“state”:false,“channel”:3,"modu"
ONLY THE FIRST 3 RECORDS AROU EVER OUTPUT AND REPEATS AGAIN
Can anyone set me on the right path, using the JsonParserGeneratorRK library ?