List of usage examples for com.google.gson JsonElement isJsonPrimitive
public boolean isJsonPrimitive()
From source file:com.ibm.common.geojson.as2.GeoAdapter.java
License:Apache License
@Override public GeoObject deserialize(JsonElement element, Type type, JsonDeserializationContext context) throws JsonParseException { GeoObject.Builder geo = null;//from ww w .ja v a 2s.c om checkArgument(element.isJsonObject()); JsonObject obj = element.getAsJsonObject(); checkArgument(obj.has("type")); GeoObject.Type et = Enums.getIfPresent(GeoObject.Type.class, obj.get("type").getAsString().toUpperCase()) .orNull(); checkArgument(et != null); switch (et) { case FEATURE: geo = GeoMakers.feature(); break; case FEATURECOLLECTION: geo = GeoMakers.featureCollection(); type = Feature.class; break; case GEOMETRYCOLLECTION: geo = GeoMakers.geometryCollection(); type = Geometry.class; break; case LINESTRING: geo = GeoMakers.linestring(); type = Position.class; break; case MULTILINESTRING: geo = GeoMakers.multiLineString(); type = LineString.class; break; case MULTIPOINT: geo = GeoMakers.multipoint(); type = Position.class; break; case MULTIPOLYGON: geo = GeoMakers.multiPolygon(); type = Polygon.class; break; case POINT: geo = GeoMakers.point(); type = null; break; case POLYGON: geo = GeoMakers.polygon(); type = LineString.class; break; } for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { JsonElement el = entry.getValue(); String name = entry.getKey(); if ("crs".equals(name)) { CRS.Builder cb = new CRS.Builder(); JsonObject o = el.getAsJsonObject(); if (o.has("type")) cb.type(o.get("type").getAsString()); if (o.has("properties")) { JsonObject p = o.get("properties").getAsJsonObject(); for (Map.Entry<String, JsonElement> e : p.entrySet()) { cb.set(e.getKey(), context.deserialize(e.getValue(), Object.class)); } } geo.crs(cb.get()); } else if ("properties".equals(name)) { geo.set("properties", context.deserialize(el, Map.class)); } else if ("bbox".equals(name)) { BoundingBox.Builder bb = new BoundingBox.Builder(); float[] points = context.deserialize(el, float[].class); bb.add(points); geo.boundingBox(bb.get()); } else if ("features".equals(name)) { Feature[] features = context.deserialize(el, Feature[].class); FeatureCollection.Builder fcb = (FeatureCollection.Builder) geo; for (Feature f : features) fcb.add(f); } else if ("coordinates".equals(name)) { switch (et) { case LINESTRING: { LineString.Builder lsb = (LineString.Builder) geo; float[][] positions = context.deserialize(el, float[][].class); boolean ring = ring(positions); if (ring) lsb.linearRing(); for (int n = 0; n < positions.length; n++) { if (!ring || (ring && n < positions.length - 1)) lsb.add(toPosition(positions[n])); } break; } case MULTIPOINT: { MultiPoint.Builder lsb = (MultiPoint.Builder) geo; float[][] positions = context.deserialize(el, float[][].class); for (float[] pos : positions) lsb.add(toPosition(pos)); break; } case MULTILINESTRING: { MultiLineString.Builder mlb = (MultiLineString.Builder) geo; float[][][] positions = context.deserialize(el, float[][][].class); for (float[][] lines : positions) { LineString.Builder lsb = GeoMakers.linestring(); boolean ring = ring(lines); if (ring) lsb.linearRing(); for (int n = 0; n < lines.length; n++) { if (!ring || (ring && n < lines.length - 1)) lsb.add(toPosition(lines[n])); } for (float[] pos : lines) lsb.add(toPosition(pos)); mlb.add(lsb); } break; } case POLYGON: { Polygon.Builder mlb = (Polygon.Builder) geo; float[][][] positions = context.deserialize(el, float[][][].class); for (float[][] lines : positions) { LineString.Builder lsb = GeoMakers.linestring(); for (float[] pos : lines) lsb.add(toPosition(pos)); mlb.add(lsb); } break; } case MULTIPOLYGON: { MultiPolygon.Builder mpb = (MultiPolygon.Builder) geo; float[][][][] positions = context.deserialize(el, float[][][][].class); for (float[][][] polygons : positions) { Polygon.Builder pb = GeoMakers.polygon(); for (float[][] lines : polygons) { LineString.Builder lsb = GeoMakers.linestring(); for (float[] pos : lines) lsb.add(toPosition(pos)); pb.add(lsb); } mpb.add(pb); } break; } case POINT: Point.Builder pb = (Point.Builder) geo; float[] position = context.deserialize(el, float[].class); pb.position(toPosition(position)); break; default: break; } } else if ("geometries".equals(name)) { Geometry[] geos = context.deserialize(el, Geometry[].class); GeometryCollection.Builder fcb = (GeometryCollection.Builder) geo; for (Geometry<?, ?> g : geos) fcb.add(g); } else { if (el.isJsonArray()) { geo.set(name, context.deserialize(el, Object.class)); } else if (el.isJsonObject()) { geo.set(name, context.deserialize(el, GeoObject.class)); } else if (el.isJsonPrimitive()) { JsonPrimitive p = el.getAsJsonPrimitive(); if (p.isBoolean()) geo.set(name, p.getAsBoolean()); else if (p.isNumber()) geo.set(name, p.getAsNumber()); else if (p.isString()) geo.set(name, p.getAsString()); } } } return geo.get(); }
From source file:com.ibm.g11n.pipeline.resfilter.GlobalizeJsResource.java
License:Open Source License
/** * The override of addBundleStrings is necessary because globalizejs treats arrays of strings * as a single long string, with each piece separated by a space, rather than treating it like * a real JSON array./*from w ww . j av a2s . c o m*/ */ @Override protected int addBundleStrings(JsonObject obj, String keyPrefix, Bundle bundle, int sequenceNum) { for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (value.isJsonObject()) { sequenceNum = addBundleStrings(value.getAsJsonObject(), encodeResourceKey(keyPrefix, key, false), bundle, sequenceNum); } else if (value.isJsonArray()) { JsonArray ar = value.getAsJsonArray(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < ar.size(); i++) { JsonElement arrayEntry = ar.get(i); if (arrayEntry.isJsonPrimitive() && arrayEntry.getAsJsonPrimitive().isString()) { if (i > 0) { sb.append(" "); // } sb.append(arrayEntry.getAsString()); } else { throw new IllegalResourceFormatException( "Arrays must contain only strings in a globalizejs resource."); } } sequenceNum++; bundle.addResourceString(encodeResourceKey(keyPrefix, key, true), sb.toString(), sequenceNum); } else if (!value.isJsonPrimitive() || !value.getAsJsonPrimitive().isString()) { throw new IllegalResourceFormatException("The value of JSON element " + key + " is not a string."); } else { sequenceNum++; bundle.addResourceString(encodeResourceKey(keyPrefix, key, true), value.getAsString(), sequenceNum); } } return sequenceNum; }
From source file:com.ibm.g11n.pipeline.resfilter.impl.GlobalizeJsResource.java
License:Open Source License
/** * The override of addBundleStrings is necessary because globalizejs treats arrays of strings * as a single long string, with each piece separated by a space, rather than treating it like * a real JSON array.//from w w w . j a v a2 s .c o m */ @Override protected int addBundleStrings(JsonObject obj, String keyPrefix, LanguageBundleBuilder bb, int sequenceNum) throws ResourceFilterException { for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (value.isJsonObject()) { sequenceNum = addBundleStrings(value.getAsJsonObject(), encodeResourceKey(keyPrefix, key, false), bb, sequenceNum); } else if (value.isJsonArray()) { JsonArray ar = value.getAsJsonArray(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < ar.size(); i++) { JsonElement arrayEntry = ar.get(i); if (arrayEntry.isJsonPrimitive() && arrayEntry.getAsJsonPrimitive().isString()) { if (i > 0) { sb.append(" "); // } sb.append(arrayEntry.getAsString()); } else { throw new IllegalResourceFormatException( "Arrays must contain only strings in a globalizejs resource."); } } sequenceNum++; bb.addResourceString(encodeResourceKey(keyPrefix, key, true), sb.toString(), sequenceNum); } else if (!value.isJsonPrimitive() || !value.getAsJsonPrimitive().isString()) { throw new IllegalResourceFormatException("The value of JSON element " + key + " is not a string."); } else { sequenceNum++; bb.addResourceString(encodeResourceKey(keyPrefix, key, true), value.getAsString(), sequenceNum); } } return sequenceNum; }
From source file:com.ibm.g11n.pipeline.resfilter.impl.JsonResource.java
License:Open Source License
protected int addBundleStrings(JsonObject obj, String keyPrefix, LanguageBundleBuilder bb, int sequenceNum) throws ResourceFilterException { for (Map.Entry<String, JsonElement> entry : obj.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (value.isJsonObject()) { sequenceNum = addBundleStrings(value.getAsJsonObject(), encodeResourceKey(keyPrefix, key, false), bb, sequenceNum);// ww w . ja v a 2 s .co m } else if (value.isJsonArray()) { JsonArray ar = value.getAsJsonArray(); for (int i = 0; i < ar.size(); i++) { JsonElement arrayEntry = ar.get(i); String arrayKey = encodeResourceKey(keyPrefix, key, false) + "[" + Integer.toString(i) + "]"; if (arrayEntry.isJsonPrimitive() && arrayEntry.getAsJsonPrimitive().isString()) { sequenceNum++; bb.addResourceString(ResourceString.with(arrayKey, arrayEntry.getAsString()) .sequenceNumber(sequenceNum)); } else { sequenceNum = addBundleStrings(arrayEntry.getAsJsonObject(), arrayKey, bb, sequenceNum); } } } else if (!value.isJsonPrimitive() || !value.getAsJsonPrimitive().isString()) { throw new IllegalResourceFormatException("The value of JSON element " + key + " is not a string."); } else { sequenceNum++; bb.addResourceString( ResourceString.with(encodeResourceKey(keyPrefix, key, true), value.getAsString()) .sequenceNumber(sequenceNum)); } } return sequenceNum; }
From source file:com.ibm.g11n.pipeline.resfilter.JsonResource.java
License:Open Source License
@Override public Bundle parse(InputStream inStream) throws IOException { Bundle bundle = new Bundle(); try (InputStreamReader reader = new InputStreamReader(new BomInputStream(inStream), StandardCharsets.UTF_8)) { JsonElement root = new JsonParser().parse(reader); if (!root.isJsonObject()) { throw new IllegalResourceFormatException("The root JSON element is not an JSON object."); }/*w w w . j av a 2s.c om*/ int sequenceNum = 0; for (Map.Entry<String, JsonElement> entry : root.getAsJsonObject().entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (!value.isJsonPrimitive() || !value.getAsJsonPrimitive().isString()) { throw new IllegalResourceFormatException( "The value of JSON element " + key + " is not a string."); } sequenceNum++; bundle.addResourceString(key, value.getAsString(), sequenceNum); } } catch (JsonParseException e) { throw new IllegalResourceFormatException("Failed to parse the specified JSON contents.", e); } return bundle; }
From source file:com.ibm.internal.iotf.devicemgmt.handler.ObserveRequestHandler.java
License:Open Source License
private JsonElement trimResponse(String name, JsonElement newValue) { JsonElement previousValue = responseMap.get(name); if (previousValue == null) { return null; }//w w w. j a va 2s. c o m if (previousValue.isJsonPrimitive()) { if (previousValue.equals(newValue)) { return null; } else { return newValue; } } boolean bModified = false; JsonObject response = new JsonObject(); for (Map.Entry<String, JsonElement> en : ((JsonObject) previousValue).entrySet()) { String key = en.getKey(); JsonElement value = en.getValue(); JsonElement otherValue = ((JsonObject) newValue).get(key); if (otherValue == null || !otherValue.equals(value)) { response.add(key, otherValue); en.setValue(otherValue); bModified = true; } } if (bModified) { return response; } else { return null; } }
From source file:com.ibm.streamsx.topology.generator.spl.OperatorGenerator.java
License:Open Source License
private void parallelAnnotation(JsonObject op, StringBuilder sb) { boolean parallel = jboolean(op, "parallelOperator"); if (parallel) { boolean partitioned = jboolean(op, "partitioned"); JsonObject parallelInfo = op.get("parallelInfo").getAsJsonObject(); sb.append("@parallel(width="); JsonElement width = parallelInfo.get(OpProperties.WIDTH); if (width.isJsonPrimitive()) { sb.append(width.getAsString()); } else {//from w w w.j a va 2 s . co m splValueSupportingSubmission(width.getAsJsonObject(), sb); } if (partitioned) { sb.append(", partitionBy=["); JsonArray partitionedPorts = array(parallelInfo, "partitionedPorts"); for (int i = 0; i < partitionedPorts.size(); i++) { JsonObject partitionedPort = partitionedPorts.get(i).getAsJsonObject(); if (i > 0) sb.append(", "); sb.append("{port="); sb.append(getSPLCompatibleName(GsonUtilities.jstring(partitionedPort, "name"))); sb.append(", attributes=["); JsonArray partitionKeys = partitionedPort.get("partitionedKeys").getAsJsonArray(); for (int j = 0; j < partitionKeys.size(); j++) { if (j != 0) sb.append(", "); sb.append(partitionKeys.get(j).getAsString()); } sb.append("]}"); } sb.append("]"); } JsonArray broadcastPorts = parallelInfo.get("broadcastPorts").getAsJsonArray(); if (broadcastPorts.size() > 0) { sb.append(", broadcast=["); for (int i = 0; i < broadcastPorts.size(); i++) { if (i != 0) sb.append(", "); sb.append(getSPLCompatibleName(broadcastPorts.get(i).getAsString())); } sb.append("]"); } sb.append(")"); sb.append("\n"); } }
From source file:com.ibm.streamsx.topology.generator.spl.SPLGenerator.java
License:Open Source License
/** * Add an arbitrary SPL value./*from w ww.ja v a2s .com*/ * JsonObject has a type and a value. */ static void value(StringBuilder sb, JsonObject tv) { JsonElement value = tv.get("value"); String type = JParamTypes.TYPE_SPL_EXPRESSION; if (tv.has("type")) { type = tv.get("type").getAsString(); } else { if (value.isJsonPrimitive()) { JsonPrimitive pv = value.getAsJsonPrimitive(); if (pv.isString()) type = "RSTRING"; } else if (value.isJsonArray()) { type = "RSTRING"; } } if (value.isJsonArray()) { JsonArray array = value.getAsJsonArray(); for (int i = 0; i < array.size(); i++) { if (i != 0) sb.append(", "); value(sb, type, array.get(i)); } } else { value(sb, type, value); } }
From source file:com.ibm.streamsx.topology.internal.streaminganalytics.VcapServices.java
License:Open Source License
/** * Get the top-level VCAP services object. * //w w w . ja v a 2 s . c o m * Object can be one of the following: * <ul> * <li>JsonObject - assumed to contain VCAP_SERVICES </li> * <li>String - assumed to contain serialized VCAP_SERVICES JSON, or the * location of a file containing the serialized VCAP_SERVICES JSON</li> * <li>null - assumed to be in the environment variable VCAP_SERVICES</li> * </ul> */ public static JsonObject getVCAPServices(JsonElement rawServices) throws IOException { JsonParser parser = new JsonParser(); String vcapString; String vcapContents = null; if (rawServices == null || rawServices.isJsonNull()) { // if rawServices is null, then pull from the environment vcapString = System.getenv("VCAP_SERVICES"); if (vcapString == null) { throw new IllegalStateException( "VCAP_SERVICES are not defined, please set environment variable VCAP_SERVICES or configuration property: " + VCAP_SERVICES); } // resulting string can be either the serialized JSON or filename if (Files.isRegularFile(Paths.get(vcapString))) { Path vcapFile = Paths.get(vcapString); vcapContents = new String(Files.readAllBytes(vcapFile), StandardCharsets.UTF_8); } else { vcapContents = vcapString; } } else if (rawServices.isJsonObject()) { return rawServices.getAsJsonObject(); } else if (rawServices.isJsonPrimitive()) { // String can be either the serialized JSON or filename String rawString = rawServices.getAsString(); if (Files.isRegularFile(Paths.get(rawString))) { Path vcapFile = Paths.get(rawString); vcapContents = new String(Files.readAllBytes(vcapFile), StandardCharsets.UTF_8); } else vcapContents = rawString; } else { throw new IllegalArgumentException("Unknown VCAP_SERVICES object class: " + rawServices.getClass()); } return parser.parse(vcapContents).getAsJsonObject(); }
From source file:com.iheart.quickio.client.QuickIo.java
License:Open Source License
private void move(final JsonElement json) { if (!json.isJsonPrimitive()) { return;/*from www . jav a 2s .co m*/ } JsonPrimitive pr = json.getAsJsonPrimitive(); if (!pr.isString()) { return; } this.reconnect(pr.getAsString()); }