Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.opencarte.db; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParseException; import com.opencarte.model.DataSet; import com.opencarte.model.Geometry; import com.opencarte.model.ParisTaxi; import com.opencarte.model.ParisTaxi.PrisTaxiAux; import java.io.IOException; import java.lang.reflect.Type; import java.net.URL; import org.apache.commons.io.IOUtils; /** * * @author Yami */ public class TaxisParser extends Parser { private String myFileContent = null; private GsonBuilder myBuilder; private Gson myGson; public TaxisParser(String fileName) { super(fileName); try { this.myFileContent = IOUtils.toString(new URL(fileName).openStream()); myBuilder = new GsonBuilder(); } catch (IOException ex) { System.out.println(ex); //Logger.getLogger(Parser.class.getName()).log(Level.SEVERE, null, ex); } } @Override public ParisTaxi[] getDataSet() { //GsonBuilder myBuilder; //Gson myGson; DataSet[] dataset = super.getDataSet(); ParisTaxi[] parisTaxis = new ParisTaxi[dataset.length]; PrisTaxiAux[] prisTaxiAux = new PrisTaxiAux[dataset.length]; JsonDeserializer<PrisTaxiAux> paristaxisauxDes = new JsonDeserializer<PrisTaxiAux>() { @Override public PrisTaxiAux deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException { JsonObject deserialize = je.getAsJsonObject(); return new PrisTaxiAux(deserialize.get("fields").getAsJsonObject().get("city").getAsString(), deserialize.get("fields").getAsJsonObject().get("address").getAsString(), //deserialize.get("fields").getAsJsonObject().get("address_precision").getAsString(), deserialize.get("fields").getAsJsonObject().get("station_name").getAsString(), deserialize.get("fields").getAsJsonObject().get("zip_code").getAsInt()); } }; myBuilder.registerTypeAdapter(PrisTaxiAux.class, paristaxisauxDes); myGson = myBuilder.create(); try { prisTaxiAux = myGson.fromJson(myFileContent, PrisTaxiAux[].class); } catch (JsonParseException ex) { System.out.println("Invalid JSON: " + ex.getMessage()); } for (int i = 0; i < dataset.length; i++) { //parisTaxis[i].setDatasetid(dataset[i].getDatasetid()); parisTaxis[i] = new ParisTaxi(dataset[i].getDatasetid(), dataset[i].getRecordid(), dataset[i].getGeometry(), prisTaxiAux[i]);// TaxietGeometry(dataset[i].getGeometry()); //parisTaxis[i].setFields(); } return parisTaxis; } }