1

I started to play around with the ArduinoJson library to parse data I got from one of my APIs (the format of the JSON data I get from my API can be improved for sure, but the software I had to implement the API with is rather limited in formatting JSON strings). The data is formatted as an array of arrays like this:

[["P101","1.2345","bar(a)"],["P102","1234.567","mbar"],["P103","2345.678","mbar"],["P104","2.3456","bar(a)"]]

In order to get a first impression about how much memory is needed I used ArduinoJSON's Assistant to analyse my dataset, calculate memory requirements and get a very basic code snippet for parsing the data. Code snippet generated by the assistant is as follows:

const size_t bufferSize = 4*JSON_ARRAY_SIZE(3) + JSON_ARRAY_SIZE(4) + 90;
DynamicJsonBuffer jsonBuffer(bufferSize);

const char* json = "[[\"P101\",\"1.2345\",\"bar(a)\"],[\"P102\",\"1234.567\",\"mbar\"],[\"P103\",\"2345.678\",\"mbar\"],[\"P104\",\"2.3456\",\"bar(a)\"]]";

JsonArray& root = jsonBuffer.parseArray(json);

JsonArray& root_ = root;

JsonArray& root_0 = root_[0];
const char* root_00 = root_0[0]; // "P101"
const char* root_01 = root_0[1]; // "1.2345"
const char* root_02 = root_0[2]; // "bar(a)"

JsonArray& root_1 = root_[1];
const char* root_10 = root_1[0]; // "P102"
const char* root_11 = root_1[1]; // "1234.567"
const char* root_12 = root_1[2]; // "mbar"

JsonArray& root_2 = root_[2];
const char* root_20 = root_2[0]; // "P103"
const char* root_21 = root_2[1]; // "2345.678"
const char* root_22 = root_2[2]; // "mbar"

JsonArray& root_3 = root_[3];
const char* root_30 = root_3[0]; // "P104"
const char* root_31 = root_3[1]; // "2.3456"
const char* root_32 = root_3[2]; // "bar(a)"

I did not test this, yet but had a look at the produced snippet and stumbled upon these lines:

JsonArray& root = jsonBuffer.parseArray(json);

JsonArray& root_ = root;

What is the exact advantage of using address-references root, root_ and e.g. root_1 here? Those should, as far as I understood the code, reference to the same data at the end. Is it for better readability only?

1

The advantage of reference above pointers:

  • References cannot be NULL, so it's much safer to use them
  • Address of a reference cannot be acquired, this means a calling function cannot mess up anything outside the type (value) being passed.

For passing a reference to an address the same advantages applies (cannot be NULL, address cannot be changed).

Also, address arithmetic (address + xxx or address - xxx) cannot be used, which prevents tricky programming errors where values outside a variable's memory space can be changed.

0

It is not a reference to reference. It is a copy of the reference.

The generator must handle more difficult JSON inputs then yours and to make output readable it has a strategy, which in simple case can look as unnecessary. Yes, it could use root[n][m].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.