/** * Convert String into JSON * * @param doc object to be converted */ @Override public void fromJSON(Object doc) { JSONDocument json = JSON.Parser.toJSON(doc); PACKED_CROP_RATINGS = new byte[json.getNumber("total_crop_tiles").intValue()]; PACKED_TILE_COORDINATES = new int[json.getNumber("total_coord_tiles").intValue()]; for (int i = 0; i < PACKED_CROP_RATINGS.length; i++) { PACKED_CROP_RATINGS[i] = json.getNumber("crop " + i).byteValue(); if (i < PACKED_TILE_COORDINATES.length) PACKED_TILE_COORDINATES[i] = json.getNumber("coord " + i).intValue(); } }
/** * Convert class into json * * @return JSON representation of class */ @Override public JSONDocument toJSON() { JSONDocument json = new JSONDocument(JSONDocument.Type.OBJECT); json.setNumber("total_crop_tiles", PACKED_CROP_RATINGS.length); json.setNumber("total_coord_tiles", PACKED_TILE_COORDINATES.length); for (int i = 0; i < PACKED_CROP_RATINGS.length; i++) { json.setNumber("crop " + i, PACKED_CROP_RATINGS[i]); if (i < PACKED_TILE_COORDINATES.length) json.setNumber("coord " + i, PACKED_TILE_COORDINATES[i]); } return json; }
@Override public void fromJSON(Object doc) { JSONDocument json = JSON.Parser.toJSON(doc); year = (int) json.getNumber("year"); seaLevel = (double) json.getNumber("sea-level"); List<Object> eventArray = json.get("events").array(); for (int i = 0; i < eventArray.size(); i++) { SpecialEventData data = new SpecialEventData(null); data.fromJSON(eventArray.get(i)); eventList.add(data); } List<Object> foodPriceArray = json.get("food-prices").array(); for (int i = 0; i < foodPriceArray.size(); i++) { foodPrice[i] = (int) foodPriceArray.get(i); } List<Object> regionArray = json.get("regions").array(); for (int i = 0; i < regionArray.size(); i++) { RegionData data = new RegionData(null); data.fromJSON(regionArray.get(i)); regionData[i] = data; } }
@Override public JSONDocument toJSON() { JSONDocument json = new JSONDocument(JSONDocument.Type.OBJECT); json.setNumber("year", year); json.setNumber("sea-level", seaLevel); json.setString("type", getType().name()); JSONDocument _eventArray = JSONDocument.createArray(eventList.size()); for (int i = 0; i < eventList.size(); i++) { _eventArray.set(i, eventList.get(i).toJSON()); } // getting only us regions!!!!!! this is due to the data being too large for JS client JSONDocument _regionArray = JSONDocument.createArray(regionData.length); int y = 0; for (int i = 0; i < regionData.length; i++) { // _regionArray.set(i, regionData[i].toJSON()); if (regionData[i].region.isUS()) { _regionArray.set(y, regionData[i].toJSON()); y++; } } JSONDocument _foodPriceArray = JSONDocument.createArray(foodPrice.length); for (int i = 0; i < foodPrice.length; i++) { _foodPriceArray.setNumber(i, foodPrice[i]); } json.set("events", _eventArray); json.set("food-prices", _foodPriceArray); json.set("regions", _regionArray); return json; }