0

I'm trying to create make a method, that returns a json-string filled with sample-data. I have created a data-constructor class but when I when I create a data-object and afterwards print it, it for some reason returns an empty json: "[]"? What am I missing here? Why doesn't it return the data-object I just created?

Here is my main class:

public class SimulatedDevice {

    public static void printObject(Object object) {
        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        System.out.println(gson.toJson(object));
        }


    public static class TelemetryDataPoint {

      public String CreateTelemetryDataPoint() {
        ArrayList<Data.TrendData> trendData = new ArrayList<>();
        trendData.add(new Data.TrendData("Building1", "2018-08-28T01:03:02.997301Z", 2, "occupants", "int"));
        Data data = new Data("B1", "0", "0", trendData);
        printObject(data);
        String json = new Gson().toJson(data);
        return json;
      }

     }
    }

This is my data constructor:

package com.microsoft.docs.iothub.samples;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.sql.Time;
import java.util.ArrayList;
import java.util.List;


public class Data extends ArrayList {
    String Building;
    String Floor;
    String Zone;
    ArrayList<TrendData> Trend;

    public Data(String Building, String Floor, String Zone, ArrayList<TrendData> Trend) {
        this.Building = Building;
        this.Floor = Floor;
        this.Zone = Zone;
        this.Trend = Trend;
}

    public static class TrendData {
        String PointId;
        String Timestamp;
        int Value;
        String Type;
        String Unit;

        public TrendData(String PointId, String Timestamp, int Value, String Type, String Unit) {
            this.PointId = PointId;
            this.Timestamp = Timestamp;
            this.Value = Value;
            this.Type = Type;
            this.Unit = Unit;

        }
    }
JonRavn
  • 13
  • 3

1 Answers1

0

If you remove 'extends ArrayList' from Data declaration it will work fine. I have not debugged it enough to figure out why Gson does not like the base class being ArrayList.

Here is a possible explanation: Trouble with Gson serializing an ArrayList of POJO's

AlexC
  • 1,395
  • 14
  • 26