Arduino json library problem (or my CPP indolence)

Hello all,

i have very heavy problem with ArduinoJson library.
I am trying to send request with json body to server. This body contains json with some informations and with an array of measures collected from sensors.

here is the code:

    void writeMeasureArray(const char* keyName, JsonObject& root)
    {
      JsonArray& array = root.createNestedArray(keyName);
      for (int i = 0; i < this->sensorCount; i++) {
        JsonObject& _obj = (this->sensorArray[i])->toMeasureJson();
        _obj.printTo(Debug);
        array.add(_obj);
      }
    };

this function should produce output like this:

{"deviceIp":{"value":"192.168.1.8"},"sensorMeasures":[{"sensorType":"GAS","digitalResult":1,"analogResult":-99999.00},
{"sensorType":"PIR","digitalResult":0,"analogResult":-99999.00},
{"sensorType":"TEMP","digitalResult":0,"analogResult":-99999.00}]}

but produces this:

{"deviceIp":{"value":"192.168.1.8"},"sensorMeasures":[{"sensorType":"TEMP","digitalResult":0,"analogResult":-99999.00},
{"sensorType":"TEMP","digitalResult":0,"analogResult":-99999.00},
{"sensorType":"TEMP","digitalResult":0,"analogResult":-99999.00}]}

sensorArray is a fixed size array of pointers to Sensor class which have toMeasureJson() method.
toMeasureJson() returns current measures of current sensor.
printing every single _obj to Serial (Debug is just a define to easily switch between Serial1 or Serial or whatever) gives
expected results. Adding _obj reference gives three same references in array.
Can some tell what am I doing wrong?

quick reply: when you are adding objects to an array you probably want to create a new instance for every entry

every _obj is a new instance of JsonObject refrence.
as i said: printing single _obj before adding to array gives expected output. When _obj is in array, printing whole root object gives wrong output.

below is toMeasureJson function.

    JsonObject& toMeasureJson()
    {
      DynamicJsonBuffer buffer;
      JsonObject &root = buffer.createObject();
      root["sensorType"] = this->sensorType;
      root["digitalResult"] = readDigitalValue();
      root["analogResult"] = readAnalogValue();
      return root;
    }

Could it be a memory issue? Not sure what happens if you try to create an instance but it fails due to insufficient memory.

andrzeju:
Can some tell what am I doing wrong?

Well, is seems that sensorArray[1] points to your TEMP sensor rather than pointing to your PIR sensor like it should. As to how that happened - beats me. I have no idea how this->sensorArray gets populated.

Well, I was probably very tired. Bug was very simple:

    JsonObject& toMeasureJson()
    {
      DynamicJsonBuffer buffer;
      JsonObject &root = buffer.createObject();
      root["sensorType"] = this->sensorType;
      root["digitalResult"] = readDigitalValue();
      root["analogResult"] = readAnalogValue();
      return root;
    }

I tried to use locally allocated buffer. After function returned root reference, buffer was discarded, this is why this weird error occured.