List of usage examples for com.google.gson JsonParseException JsonParseException
public JsonParseException(Throwable cause)
From source file:uk.ac.horizon.aestheticodes.controllers.MarkerMapAdapter.java
License:Open Source License
@Override public Map<String, Marker> deserialize(JsonElement json, Type unused, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonArray()) { throw new JsonParseException("Unexpected type: " + json.getClass().getSimpleName()); }/*from www . j a va2 s . c om*/ Map<String, Marker> result = new HashMap<String, Marker>(); JsonArray array = json.getAsJsonArray(); for (JsonElement element : array) { if (element.isJsonObject()) { Marker marker = context.deserialize(element, Marker.class); result.put(marker.getCode(), marker); } else { throw new JsonParseException("some meaningful message"); } } return result; }
From source file:uk.co.bubblebearapps.contactsintegration.DateTimeDeserializer.java
License:Open Source License
@Override public DateTime deserialize(final JsonElement element, final Type type, final JsonDeserializationContext jdc) throws JsonParseException { String json = element.getAsString(); if (json == null || TextUtils.isEmpty(json)) { throw new JsonParseException("element was null or empty"); } else {/* w ww . jav a 2 s . c o m*/ return DateTime.parse(json); } }
From source file:uk.org.openeyes.oink.domain.json.AtomFeedGsonAdapter.java
License:Open Source License
@Override public AtomFeed deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonParser parser = new JsonParser(); String jsonString = json.toString(); InputStream is = new ByteArrayInputStream(jsonString.getBytes()); try {/*from ww w . j a va 2s . com*/ ResourceOrFeed resourceOrFeed = parser.parseGeneral(is); return resourceOrFeed.getFeed(); } catch (Exception e) { e.printStackTrace(); throw new JsonParseException("Invalid AtomFeed structure: " + e.getMessage()); } finally { try { is.close(); } catch (IOException e) { } } }
From source file:uk.org.openeyes.oink.domain.json.ResourceGsonAdapter.java
License:Open Source License
@Override public Resource deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonParser parser = new JsonParser(); String jsonString = json.toString(); InputStream is = new ByteArrayInputStream(jsonString.getBytes()); try {/* w ww. j av a 2s . co m*/ ResourceOrFeed resourceOrFeed = parser.parseGeneral(is); return resourceOrFeed.getResource(); } catch (Exception e) { throw new JsonParseException("Invalid Resource structure: " + e.getMessage()); } finally { try { is.close(); } catch (IOException e) { } } }
From source file:whitebox.serialization.CartographicElementDeserializer.java
License:Open Source License
@Override public CartographicElement deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException { if (workingDirectory == null || paletteDirectory == null) { throw new JsonParseException("The current working directory or palette directory must be set"); }/*from w w w.j a v a 2s . co m*/ GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(MapLayer.class, new MapLayerDeserializer(workingDirectory, paletteDirectory)); gsonBuilder.registerTypeAdapter(CartographicElement.class, new CartographicElementDeserializer(workingDirectory, paletteDirectory)); Gson gson = gsonBuilder.create(); Type clrType = new TypeToken<Color>() { }.getType(); Color clr; Type fontType = new TypeToken<Font>() { }.getType(); Font font; Type bbType = new TypeToken<BoundingBox>() { }.getType(); BoundingBox bb; JsonObject jo = je.getAsJsonObject(); String elementType = jo.getAsJsonPrimitive("cartographicElementType").getAsString(); String name = jo.getAsJsonPrimitive("name").getAsString(); switch (elementType) { case "MAP_AREA": MapArea ma = new MapArea(name); ma.setVisible(jo.getAsJsonPrimitive("isVisible").getAsBoolean()); ma.setHeight(jo.getAsJsonPrimitive("height").getAsInt()); ma.setWidth(jo.getAsJsonPrimitive("width").getAsInt()); ma.setLineWidth(jo.getAsJsonPrimitive("lineWidth").getAsFloat()); ma.setReferenceMarksSize(jo.getAsJsonPrimitive("referenceMarksSize").getAsInt()); ma.setRotation(jo.getAsJsonPrimitive("rotation").getAsDouble()); ma.setBackgroundVisible(jo.getAsJsonPrimitive("isBackgroundVisible").getAsBoolean()); ma.setBorderVisible(jo.getAsJsonPrimitive("isBorderVisible").getAsBoolean()); ma.setNeatlineVisible(jo.getAsJsonPrimitive("isNeatlineVisible").getAsBoolean()); ma.setReferenceMarksVisible(jo.getAsJsonPrimitive("isReferenceMarksVisible").getAsBoolean()); ma.setSizeMaximizedToScreenSize(jo.getAsJsonPrimitive("isSizeMaximizedToScreenSize").getAsBoolean()); ma.setUpperLeftX(jo.getAsJsonPrimitive("upperLeftX").getAsInt()); ma.setUpperLeftY(jo.getAsJsonPrimitive("upperLeftY").getAsInt()); ma.setElementNumber(jo.getAsJsonPrimitive("elementNumber").getAsInt()); ma.setName(jo.getAsJsonPrimitive("name").getAsString()); clr = gson.fromJson(jo.get("backgroundColour"), clrType); ma.setBackgroundColour(clr); clr = gson.fromJson(jo.get("borderColour"), clrType); ma.setBorderColour(clr); clr = gson.fromJson(jo.get("fontColour"), clrType); ma.setFontColour(clr); font = gson.fromJson(jo.get("labelFont"), fontType); ma.setLabelFont(font); bb = gson.fromJson(jo.get("currentExtent"), bbType); ma.setCurrentExtent(bb); bb = gson.fromJson(jo.get("currentMapExtent"), bbType); ma.setCurrentMapExtent(bb); Type listOfLayerObject = new TypeToken<List<MapLayer>>() { }.getType(); ArrayList<MapLayer> mlList = gson.fromJson(jo.get("layersList"), listOfLayerObject); for (MapLayer ml : mlList) { ma.addLayer(ml); } ma.setActiveLayer(jo.getAsJsonPrimitive("activeLayerOverlayNumber").getAsInt()); return ma; case "MAP_TITLE": MapTitle mt = new MapTitle(name); mt.setVisible(jo.getAsJsonPrimitive("isVisible").getAsBoolean()); mt.setLabel(jo.getAsJsonPrimitive("label").getAsString()); mt.setElementNumber(jo.getAsJsonPrimitive("elementNumber").getAsInt()); mt.setMargin(jo.getAsJsonPrimitive("margin").getAsInt()); mt.setBackgroundVisible(jo.getAsJsonPrimitive("isBackgroundVisible").getAsBoolean()); mt.setBorderVisible(jo.getAsJsonPrimitive("isBorderVisible").getAsBoolean()); mt.setOutlineVisible(jo.getAsJsonPrimitive("isOutlineVisible").getAsBoolean()); clr = gson.fromJson(jo.get("backColour"), clrType); mt.setBackColour(clr); clr = gson.fromJson(jo.get("borderColour"), clrType); mt.setBorderColour(clr); clr = gson.fromJson(jo.get("fontColour"), clrType); mt.setFontColour(clr); clr = gson.fromJson(jo.get("outlineColour"), clrType); mt.setOutlineColour(clr); font = gson.fromJson(jo.get("labelFont"), fontType); mt.setLabelFont(font); mt.setUpperLeftX(jo.getAsJsonPrimitive("upperLeftX").getAsInt()); mt.setUpperLeftY(jo.getAsJsonPrimitive("upperLeftY").getAsInt()); mt.setHeight(jo.getAsJsonPrimitive("height").getAsInt()); mt.setWidth(jo.getAsJsonPrimitive("width").getAsInt()); if (jo.has("rotation")) { mt.setRotation(jo.getAsJsonPrimitive("rotation").getAsDouble()); } return mt; case "MAP_TEXT_AREA": MapTextArea mta = new MapTextArea(name); mta.setVisible(jo.getAsJsonPrimitive("isVisible").getAsBoolean()); mta.setLabel(jo.getAsJsonPrimitive("label").getAsString()); mta.setUpperLeftX(jo.getAsJsonPrimitive("upperLeftX").getAsInt()); mta.setUpperLeftY(jo.getAsJsonPrimitive("upperLeftY").getAsInt()); mta.setElementNumber(jo.getAsJsonPrimitive("elementNumber").getAsInt()); mta.setHeight(jo.getAsJsonPrimitive("height").getAsInt()); mta.setWidth(jo.getAsJsonPrimitive("width").getAsInt()); mta.setMargin(jo.getAsJsonPrimitive("margin").getAsInt()); mta.setBackgroundVisible(jo.getAsJsonPrimitive("isBackgroundVisible").getAsBoolean()); mta.setBorderVisible(jo.getAsJsonPrimitive("isBorderVisible").getAsBoolean()); clr = gson.fromJson(jo.get("backColour"), clrType); mta.setBackColour(clr); clr = gson.fromJson(jo.get("borderColour"), clrType); mta.setBorderColour(clr); clr = gson.fromJson(jo.get("fontColour"), clrType); mta.setFontColour(clr); font = gson.fromJson(jo.get("labelFont"), fontType); mta.setLabelFont(font); if (jo.has("rotation")) { mta.setRotation(jo.getAsJsonPrimitive("rotation").getAsFloat()); } return mta; case "NORTH_ARROW": NorthArrow na = new NorthArrow(name); na.setVisible(jo.getAsJsonPrimitive("isVisible").getAsBoolean()); na.setUpperLeftX(jo.getAsJsonPrimitive("upperLeftX").getAsInt()); na.setUpperLeftY(jo.getAsJsonPrimitive("upperLeftY").getAsInt()); na.setElementNumber(jo.getAsJsonPrimitive("elementNumber").getAsInt()); na.setMargin(jo.getAsJsonPrimitive("margin").getAsInt()); na.setMarkerSize(jo.getAsJsonPrimitive("markerSize").getAsInt()); na.setLineWidth(jo.getAsJsonPrimitive("lineWidth").getAsFloat()); na.setBackgroundVisible(jo.getAsJsonPrimitive("isBackgroundVisible").getAsBoolean()); na.setBorderVisible(jo.getAsJsonPrimitive("isBorderVisible").getAsBoolean()); clr = gson.fromJson(jo.get("backColour"), clrType); na.setBackColour(clr); clr = gson.fromJson(jo.get("borderColour"), clrType); na.setBorderColour(clr); clr = gson.fromJson(jo.get("outlineColour"), clrType); na.setOutlineColour(clr); return na; case "MAP_IMAGE": String fileName = jo.getAsJsonPrimitive("fileName").getAsString(); // see whether it exists, and if it doesn't, see whether a file of the same // name exists in the working directory or any of its subdirectories. if (!new File(fileName).exists()) { flag = true; findFile(new File(workingDirectory), new File(fileName).getName()); if (!retFile.equals("")) { fileName = retFile; } else { throw new JsonParseException("Could not locate image file referred to in map file."); } } MapImage mi = new MapImage(name, fileName); mi.setVisible(jo.getAsJsonPrimitive("isVisible").getAsBoolean()); mi.setElementNumber(jo.getAsJsonPrimitive("elementNumber").getAsInt()); mi.setLineWidth(jo.getAsJsonPrimitive("lineWidth").getAsFloat()); mi.setBorderVisible(jo.getAsJsonPrimitive("isBorderVisible").getAsBoolean()); clr = gson.fromJson(jo.get("borderColour"), clrType); mi.setBorderColour(clr); mi.setUpperLeftX(jo.getAsJsonPrimitive("upperLeftX").getAsInt()); mi.setUpperLeftY(jo.getAsJsonPrimitive("upperLeftY").getAsInt()); mi.setHeight(jo.getAsJsonPrimitive("height").getAsInt()); mi.setWidth(jo.getAsJsonPrimitive("width").getAsInt()); mi.setMaintainAspectRatio(jo.getAsJsonPrimitive("maintainAspectRatio").getAsBoolean()); return mi; case "NEATLINE": Neatline nl = new Neatline(name); nl.setVisible(jo.getAsJsonPrimitive("isVisible").getAsBoolean()); nl.setUpperLeftX(jo.getAsJsonPrimitive("upperLeftX").getAsInt()); nl.setUpperLeftY(jo.getAsJsonPrimitive("upperLeftY").getAsInt()); nl.setElementNumber(jo.getAsJsonPrimitive("elementNumber").getAsInt()); nl.setDoubleLineGap(jo.getAsJsonPrimitive("doubleLineGap").getAsInt()); nl.setInnerLineWidth(jo.getAsJsonPrimitive("innerLineWidth").getAsFloat()); nl.setOuterLineThickness(jo.getAsJsonPrimitive("outerLineWidth").getAsFloat()); nl.setBackgroundVisible(jo.getAsJsonPrimitive("isBackgroundVisible").getAsBoolean()); nl.setBorderVisible(jo.getAsJsonPrimitive("isBorderVisible").getAsBoolean()); nl.setDoubleLine(jo.getAsJsonPrimitive("isDoubleLine").getAsBoolean()); clr = gson.fromJson(jo.get("backgroundColour"), clrType); nl.setBackgroundColour(clr); clr = gson.fromJson(jo.get("borderColour"), clrType); nl.setBorderColour(clr); nl.setHeight(jo.getAsJsonPrimitive("height").getAsInt()); nl.setWidth(jo.getAsJsonPrimitive("width").getAsInt()); return nl; case "MAP_SCALE": MapScale ms = new MapScale(name); ms.setVisible(jo.getAsJsonPrimitive("isVisible").getAsBoolean()); ms.setUpperLeftX(jo.getAsJsonPrimitive("upperLeftX").getAsInt()); ms.setUpperLeftY(jo.getAsJsonPrimitive("upperLeftY").getAsInt()); ms.setElementNumber(jo.getAsJsonPrimitive("elementNumber").getAsInt()); clr = gson.fromJson(jo.get("backColour"), clrType); ms.setBackColour(clr); clr = gson.fromJson(jo.get("borderColour"), clrType); ms.setBorderColour(clr); clr = gson.fromJson(jo.get("fontColour"), clrType); ms.setFontColour(clr); clr = gson.fromJson(jo.get("outlineColour"), clrType); ms.setOutlineColour(clr); ms.setBackgroundVisible(jo.getAsJsonPrimitive("backgroundVisible").getAsBoolean()); ms.setBorderVisible(jo.getAsJsonPrimitive("borderVisible").getAsBoolean()); ms.setOutlineVisible(jo.getAsJsonPrimitive("outlineVisible").getAsBoolean()); ms.setConversionToMetres(jo.getAsJsonPrimitive("conversionToMetres").getAsDouble()); ms.setHeight(jo.getAsJsonPrimitive("height").getAsInt()); ms.setWidth(jo.getAsJsonPrimitive("width").getAsInt()); ms.setLineWidth(jo.getAsJsonPrimitive("lineWidth").getAsFloat()); ms.setLowerLabel(jo.getAsJsonPrimitive("lowerLabel").getAsString()); ms.setMargin(jo.getAsJsonPrimitive("margin").getAsInt()); ms.setNumberDivisions(jo.getAsJsonPrimitive("numberDivisions").getAsInt()); ms.setRepresentativeFractionVisible( jo.getAsJsonPrimitive("representativeFractionVisible").getAsBoolean()); if (jo.has("graphicalScaleVisible")) { // added as a property later. ms.setGraphicalScaleVisible(jo.getAsJsonPrimitive("graphicalScaleVisible").getAsBoolean()); } ms.setUnits(jo.getAsJsonPrimitive("units").getAsString()); ms.setMapAreaElementNumber(jo.getAsJsonPrimitive("mapAreaElementNumber").getAsInt()); if (jo.has("labelFont")) { font = gson.fromJson(jo.get("labelFont"), fontType); ms.setLabelFont(font); } return ms; case "LEGEND": Legend l = new Legend(name); l.setVisible(jo.getAsJsonPrimitive("isVisible").getAsBoolean()); l.setLabel(jo.getAsJsonPrimitive("label").getAsString()); font = gson.fromJson(jo.get("labelFont"), fontType); l.setLabelFont(font); l.setMargin(jo.getAsJsonPrimitive("margin").getAsInt()); l.setElementNumber(jo.getAsJsonPrimitive("elementNumber").getAsInt()); clr = gson.fromJson(jo.get("backgroundColour"), clrType); l.setBackgroundColour(clr); clr = gson.fromJson(jo.get("borderColour"), clrType); l.setBorderColour(clr); clr = gson.fromJson(jo.get("fontColour"), clrType); l.setFontColour(clr); l.setBackgroundVisible(jo.getAsJsonPrimitive("backgroundVisible").getAsBoolean()); l.setBorderVisible(jo.getAsJsonPrimitive("borderVisible").getAsBoolean()); l.setLineWidth(jo.getAsJsonPrimitive("lineWidth").getAsFloat()); l.setBorderWidth(jo.getAsJsonPrimitive("borderWidth").getAsFloat()); l.setHeight(jo.getAsJsonPrimitive("height").getAsInt()); l.setWidth(jo.getAsJsonPrimitive("width").getAsInt()); l.setUpperLeftX(jo.getAsJsonPrimitive("upperLeftX").getAsInt()); l.setUpperLeftY(jo.getAsJsonPrimitive("upperLeftY").getAsInt()); return l; case "CARTOGRAPHIC_ELEMENT_GROUP": CartographicElementGroup ceg = new CartographicElementGroup(name); ceg.setElementNumber(jo.getAsJsonPrimitive("elementNumber").getAsInt()); Type listOfCartographicElementsObject = new TypeToken<List<CartographicElement>>() { }.getType(); JsonElement je2 = jo.get("elementList"); ArrayList<CartographicElement> cartoElementList = gson.fromJson(je2, listOfCartographicElementsObject); ceg.setElementList(cartoElementList); return ceg; } return null; }
From source file:whitebox.serialization.MapInfoDeserializer.java
License:Open Source License
@Override public MapInfo deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException { try {/*from w w w . j a v a 2 s.c om*/ if (workingDirectory == null || paletteDirectory == null) { throw new JsonParseException("The current working directory or palette directory must be set."); } GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(PageFormat.class, new PageFormatDeserializer()); gsonBuilder.registerTypeAdapter(CartographicElement.class, new CartographicElementDeserializer(workingDirectory, paletteDirectory)); Gson gson = gsonBuilder.create(); Type fontType = new TypeToken<Font>() { }.getType(); Font font; JsonObject jo = je.getAsJsonObject(); MapInfo mi = new MapInfo(); mi.setMapName(jo.getAsJsonPrimitive("mapName").getAsString()); mi.setFileName(jo.getAsJsonPrimitive("fileName").getAsString()); mi.setPageVisible(jo.getAsJsonPrimitive("pageVisible").getAsBoolean()); mi.setMargin(jo.getAsJsonPrimitive("margin").getAsDouble()); PageFormat pf = gson.fromJson(jo.getAsJsonObject("pageFormat"), PageFormat.class); mi.setPageFormat(pf); if (jo.has("defaultFont")) { font = gson.fromJson(jo.get("defaultFont"), fontType); mi.setDefaultFont(font); } Type listOfCartographicElementsObject = new TypeToken<List<CartographicElement>>() { }.getType(); JsonElement je2 = jo.get("cartographicElementList"); ArrayList<CartographicElement> cartoElementList = gson.fromJson(je2, listOfCartographicElementsObject); ArrayList<MapArea> mapAreas = new ArrayList<>(); for (CartographicElement ce : cartoElementList) { if (ce instanceof MapArea) { mapAreas.add((MapArea) ce); } } for (int elementNumber = 0; elementNumber < cartoElementList.size(); elementNumber++) { for (CartographicElement ce : cartoElementList) { if (ce.getElementNumber() == elementNumber) { if (ce instanceof MapScale) { MapScale ms = (MapScale) ce; int mapAreaElementNumber = ms.getMapAreaElementNumber(); for (MapArea ma : mapAreas) { if (ma.getElementNumber() == mapAreaElementNumber) { ms.setMapArea(ma); mi.addNewCartographicElement(ms); } } } else if (ce instanceof Legend) { Legend l = (Legend) ce; for (MapArea ma : mapAreas) { l.addMapArea(ma); } // check to see if any MapAreas are contained in any carto element groups for (CartographicElement ce2 : cartoElementList) { if (ce instanceof CartographicElementGroup) { CartographicElementGroup ceg = (CartographicElementGroup) ce; List<CartographicElement> elementList = ceg.getElementList(); } } mi.addNewCartographicElement(l); } else { mi.addNewCartographicElement(ce); } } } } //mi.removeCartographicElement(0); // removes the default maparea return mi; } catch (Exception e) { System.err.println(e.getMessage()); return null; } }
From source file:whitebox.serialization.MapLayerDeserializer.java
License:Open Source License
@Override public MapLayer deserialize(JsonElement je, Type type, JsonDeserializationContext jdc) throws JsonParseException { if (workingDirectory == null || paletteDirectory == null) { throw new JsonParseException("The current working directory or palette directory must be set."); }//from ww w.j av a 2 s.c om GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(MapLayer.class, new MapLayerDeserializer(workingDirectory, paletteDirectory)); Gson gson = gsonBuilder.create(); Type clrType = new TypeToken<Color>() { }.getType(); Color clr; int alpha; double nonlinearity; String paletteFile; JsonObject jo = je.getAsJsonObject(); String layerType = jo.getAsJsonPrimitive("layerType").getAsString(); String layerTitle = jo.getAsJsonPrimitive("layerTitle").getAsString(); int overlayNumber = jo.getAsJsonPrimitive("overlayNumber").getAsInt(); boolean isVisible = jo.getAsJsonPrimitive("isVisible").getAsBoolean(); boolean isVisibleInLegend = true; if (jo.has("isVisibleInLegend")) { isVisibleInLegend = jo.getAsJsonPrimitive("isVisibleInLegend").getAsBoolean(); } switch (layerType) { case "RASTER": // find the header file String headerFile = jo.getAsJsonPrimitive("headerFile").getAsString(); // see whether it exists, and if it doesn't, see whether a file of the same // name exists in the working directory or any of its subdirectories. if (!new File(headerFile).exists()) { flag = true; findFile(new File(workingDirectory), new File(headerFile).getName()); if (!retFile.equals("")) { headerFile = retFile; } else { throw new JsonParseException("Could not locate data file referred to in map file."); } } double displayMinVal = jo.getAsJsonPrimitive("displayMinVal").getAsDouble(); double displayMaxVal = jo.getAsJsonPrimitive("displayMaxVal").getAsDouble(); nonlinearity = jo.getAsJsonPrimitive("nonlinearity").getAsDouble(); // find the palette file paletteFile = jo.getAsJsonPrimitive("paletteFile").getAsString(); // see whether it exists, and if it doesn't, see whether a file of the same // name exists in the working directory or any of its subdirectories. if (!new File(paletteFile).exists()) { flag = true; findFile(new File(paletteDirectory), new File(paletteFile).getName()); if (!retFile.equals("")) { paletteFile = retFile; } else { // could not locate the palette file so go with a default palette. paletteFile = paletteDirectory + "spectrum.pal"; } } alpha = jo.getAsJsonPrimitive("alpha").getAsInt(); boolean isPaletteReversed = jo.getAsJsonPrimitive("isPaletteReversed").getAsBoolean(); RasterLayerInfo rli = new RasterLayerInfo(headerFile, paletteFile, alpha, overlayNumber); rli.setDisplayMaxVal(displayMaxVal); rli.setDisplayMinVal(displayMinVal); rli.setNonlinearity(nonlinearity); rli.setLayerTitle(layerTitle); rli.setPaletteReversed(isPaletteReversed); rli.setVisible(isVisible); rli.setVisibleInLegend(isVisibleInLegend); return rli; case "VECTOR": // find the header file String fileName = jo.getAsJsonPrimitive("fileName").getAsString(); // see whether it exists, and if it doesn't, see whether a file of the same // name exists in the working directory or any of its subdirectories. if (!new File(fileName).exists()) { flag = true; findFile(new File(workingDirectory), new File(fileName).getName()); if (!retFile.equals("")) { fileName = retFile; } else { throw new JsonParseException("Could not locate data file referred to in map file."); } } alpha = jo.getAsJsonPrimitive("alpha").getAsInt(); String fillAttribute = jo.getAsJsonPrimitive("fillAttribute").getAsString(); String lineAttribute = jo.getAsJsonPrimitive("lineAttribute").getAsString(); float lineThickness = jo.getAsJsonPrimitive("lineThickness").getAsFloat(); float markerSize = jo.getAsJsonPrimitive("markerSize").getAsFloat(); // String markerStyle = jo.getAsJsonPrimitive("markerStyle").getAsString(); nonlinearity = jo.getAsJsonPrimitive("nonlinearity").getAsDouble(); String xyUnits = jo.getAsJsonPrimitive("xyUnits").getAsString(); boolean isDashed = jo.getAsJsonPrimitive("isDashed").getAsBoolean(); boolean isFilled = jo.getAsJsonPrimitive("isFilled").getAsBoolean(); boolean isFilledWithOneColour = jo.getAsJsonPrimitive("isFilledWithOneColour").getAsBoolean(); boolean isOutlined = jo.getAsJsonPrimitive("isOutlined").getAsBoolean(); boolean isOutlinedWithOneColour = jo.getAsJsonPrimitive("isOutlinedWithOneColour").getAsBoolean(); boolean isPaletteScaled = jo.getAsJsonPrimitive("isPaletteScaled").getAsBoolean(); // find the palette file paletteFile = jo.getAsJsonPrimitive("paletteFile").getAsString(); // see whether it exists, and if it doesn't, see whether a file of the same // name exists in the working directory or any of its subdirectories. if (!new File(paletteFile).exists()) { flag = true; findFile(new File(paletteDirectory), new File(paletteFile).getName()); if (!retFile.equals("")) { paletteFile = retFile; } else { // could not locate the palette file so go with a default palette. paletteFile = paletteDirectory + "spectrum.pal"; } } VectorLayerInfo vli = new VectorLayerInfo(fileName, paletteDirectory, alpha, overlayNumber); vli.setVisible(isVisible); vli.setVisibleInLegend(isVisibleInLegend); vli.setNonlinearity(nonlinearity); vli.setFillAttribute(fillAttribute); vli.setLineAttribute(lineAttribute); vli.setLineThickness(lineThickness); vli.setLayerTitle(layerTitle); vli.setMarkerSize(markerSize); vli.setPaletteFile(paletteFile); //marker style vli.setMarkerStyle( PointMarkers.findMarkerStyleFromString(jo.getAsJsonPrimitive("markerStyle").getAsString())); vli.setXYUnits(xyUnits); vli.setDashed(isDashed); vli.setFilled(isFilled); vli.setFilledWithOneColour(isFilledWithOneColour); vli.setOutlined(isOutlined); vli.setOutlinedWithOneColour(isOutlinedWithOneColour); vli.setPaletteScaled(isPaletteScaled); clr = gson.fromJson(jo.get("fillColour"), clrType); vli.setFillColour(clr); clr = gson.fromJson(jo.get("lineColour"), clrType); vli.setLineColour(clr); return vli; default: return null; } }
From source file:wvw.mobibench.service.servlet.msg.serial.EnumDeserializer.java
License:Apache License
@SuppressWarnings("unchecked") public Enum deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String jsonStr = json.getAsString().toLowerCase(); EnumSet enumSet = EnumSet.allOf((Class) typeOfT); for (Object obj : enumSet.toArray()) { String str = obj.toString().toLowerCase(); if (str.equals(jsonStr)) return (Enum) obj; }//from ww w . j a v a2s . c o m throw new JsonParseException( "Error deserializing JSON Enum: unknown " + "identifier \"" + json.getAsString() + "\""); }
From source file:xyz.kvantum.server.implementation.AccountSerializer.java
License:Apache License
@Override public Account deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { if (json instanceof JsonObject) { final JsonObject object = (JsonObject) json; return new Account(object.get("id").getAsInt(), object.get("username").getAsString(), ""); } else {/* w ww .ja va 2 s .com*/ throw new JsonParseException("Provided json not an object"); } }
From source file:zardoni.matteo.SerializzazioneTimeline.test.RuntimeTypeAdapterFactory.java
License:Apache License
public <R> TypeAdapter<R> create(Gson gson, TypeToken<R> type) { if (type.getRawType() != baseType) { return null; }//ww w .j a v a 2s. com final Map<String, TypeAdapter<?>> labelToDelegate = new LinkedHashMap<String, TypeAdapter<?>>(); final Map<Class<?>, TypeAdapter<?>> subtypeToDelegate = new LinkedHashMap<Class<?>, TypeAdapter<?>>(); for (Map.Entry<String, Class<?>> entry : labelToSubtype.entrySet()) { TypeAdapter<?> delegate = gson.getDelegateAdapter(this, TypeToken.get(entry.getValue())); labelToDelegate.put(entry.getKey(), delegate); subtypeToDelegate.put(entry.getValue(), delegate); } return (new TypeAdapter<R>() { @Override public R read(JsonReader in) throws IOException { JsonElement jsonElement = Streams.parse(in); JsonElement labelJsonElement = jsonElement.getAsJsonObject().remove(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException("cannot deserialize " + baseType + " because it does not define a field named " + typeFieldName); } String label = labelJsonElement.getAsString(); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) labelToDelegate.get(label); if (delegate == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } return delegate.fromJsonTree(jsonElement); } @Override public void write(JsonWriter out, R value) throws IOException { Class<?> srcType = value.getClass(); String label = subtypeToLabel.get(srcType); @SuppressWarnings("unchecked") // registration requires that subtype extends T TypeAdapter<R> delegate = (TypeAdapter<R>) subtypeToDelegate.get(srcType); if (delegate == null) { throw new JsonParseException( "cannot serialize " + srcType.getName() + "; did you forget to register a subtype?"); } JsonObject jsonObject = delegate.toJsonTree(value).getAsJsonObject(); if (jsonObject.has(typeFieldName)) { throw new JsonParseException("cannot serialize " + srcType.getName() + " because it already defines a field named " + typeFieldName); } JsonObject clone = new JsonObject(); clone.add(typeFieldName, new JsonPrimitive(label)); for (Map.Entry<String, JsonElement> e : jsonObject.entrySet()) { clone.add(e.getKey(), e.getValue()); } Streams.write(clone, out); } }).nullSafe(); }