This overload is invoked when a JSONVar is set equal to another JSONVar by reference like so:
JSONVar a const
char input[] = "{'foo':{'bar': '42'}}";
JSONVar a = JSON.parse(input);
JSONVar b = a["foo"];
Serial.print("b['foo']: ");
Serial.println(b["foo"]);
result:
b['foo']: null
I see a few things wrong with this:
First, swapping the c_json pointers in the JSONVar container does nothing as the linked list is all stored in the c_json structs. The JSONVar object only is created on the fly when requested via the [] operator, and destroyed when out of scope. See below:
Second, the intent of the code in the C11+ rvalue = overloaded operator is to swap the two objects instead of copy. Why? This is not the same behavior as in older compilers and is not the same behavior if the = operator is used on a JSONVar object directly without referencing. This is is incredibly confusing to have an object be copied in one situation, but then be swapped in a different situation. In my opinion the = operator should always copy, regardless if an lvalue or rvalue is passed.
on C11+ and newer compilers, the
=
operator does not behave as expected. The code is listed belowhttps://github.com/arduino-libraries/Arduino_JSON/blob/ea843a9a0b56e3baaa1d26795072f81b9b8787ca/src/JSONVar.cpp#L244-L261
This overload is invoked when a
JSONVar
is set equal to anotherJSONVar
by reference like so:result:
I see a few things wrong with this: First, swapping the
c_json
pointers in theJSONVar
container does nothing as the linked list is all stored in thec_json
structs. TheJSONVar
object only is created on the fly when requested via the[]
operator, and destroyed when out of scope. See below:Second, the intent of the code in the C11+ rvalue
=
overloaded operator is to swap the two objects instead of copy. Why? This is not the same behavior as in older compilers and is not the same behavior if the=
operator is used on aJSONVar
object directly without referencing. This is is incredibly confusing to have an object be copied in one situation, but then be swapped in a different situation. In my opinion the=
operator should always copy, regardless if an lvalue or rvalue is passed.