1

I want to read topojson objects into a Java class. Topojson is a special class of JSON, defined where the objects have arcs in a lookup table as follows:

{
  "type":"Topology",
  "objects":{
    "collection":{
      "type":"GeometryCollection",
      "geometries":[
        {"type":"LineString","properties":{"id":842296681,"start":892624561,"end":892624564,"class":5,"lanes":null,"direction":"B","length":0.000485},"arcs":[0]},
        {"type":"LineString","properties":{"id":842296682,"start":892624563,"end":892624564,"class":5,"lanes":null,"direction":"B","length":0.000351},"arcs":[1]},
      ]
    }
  },
  "arcs":[
    [[4816,1007],[262,2281],[183,738]],
    [[4397,3892],[341,-268],[235,0],[288,402]]
  ],
  "transform":{
    "scale":[3.8203658765624953e-7,1.4901510251040002e-7],
    "translate":[-87.63437999816,41.86725999012999]},
    "bbox":[-87.63437999816,41.86725999012999,-87.63056001432003,41.86874999213999]
}

I am trying to do as suggested in this answer and read the objects directly into a class, which I've sort of awkwardly defined as

import java.util.ArrayList;
import java.util.Map;

/**
 * Created by gregmacfarlane on 7/11/17.
 */
public class TopoJsonNetwork {
  String type;
  Map<String, GeometryCollection> objects;
  ArrayList<Double[][]> arcs;
  Transform transform;

}

class GeometryCollection{
  String type;
  Geometry[] geometries;

}

class Geometry{

  String type;
  Map<String, String> properties;
  Integer[] arcs; // lines will have only a single arc


}


class Transform{
  Double[] scale;
  Double[] translate;
  Double[] bbox;

}

Everything works great when I call the gson reader

reader = new JsonReader(new FileReader(topoFile));

Gson gson = new Gson();
TopoJsonNetwork topoNet = gson.fromJson(reader, TopoJsonNetwork.class);

meaning that all the objects of my class are populated --- except for the arcs array. The element is there, but with a null value. What should I change about how this element is defined so that it populates correctly?

gregmacfarlane
  • 2,121
  • 3
  • 24
  • 53

1 Answers1

2

Using your code I am able to get the expected results. I modified your main program and looped through the ArrayList as a 2D array using nested for loops. The result was a list of all the numbers from the arcs list in your class.

JsonReader reader = new JsonReader(new FileReader("topojson.json"));
Gson gson = new Gson();
TopoJsonNetwork topoNet = gson.fromJson(reader, TopoJsonNetwork.class);

for (Double[][] d : topoNet.arcs) {
    for (int i = 0; i < d.length; i++) {
        for (int j = 0; j < d[i].length; j++) {
            System.out.println(d[i][j]);
        }
    }
}

In this case, topoNet.arcs is a list of size 2, and as you might expect it contains a couple of two-dimensional Double arrays.

ThisClark
  • 14,352
  • 10
  • 69
  • 100
  • Hmmm. I get `java.lang.NullPointerException` on the `d : topoNet.arcs` call, both in my full program and in this MWE. Could it be a Java version thing? – gregmacfarlane Jul 12 '17 at 02:50
  • I'm using Java 8 and Gson 2.6.2. Here's a direct link to download the project as I've got it currently running in my workspace. You should be able to simply import it and see how it goes on your end: https://drive.google.com/file/d/0B_iQPwucAbKASEowWkx3TTBUN00/view?usp=sharing . Click the download icon top-right to get the ZIP file. – ThisClark Jul 12 '17 at 03:25
  • Ha. Turns out I was reading from a debuggin json where I had taken out the arcs element. **facepalm** – gregmacfarlane Jul 12 '17 at 12:49