List of usage examples for com.google.gson JsonElement isJsonPrimitive
public boolean isJsonPrimitive()
From source file:org.eclipse.smarthome.storage.json.StringObjectMapDeserializer.java
License:Open Source License
@Override public Map<String, Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { Map<String, Object> map = new HashMap<String, Object>(); JsonObject obj = json.getAsJsonObject(); for (Map.Entry<String, JsonElement> me : obj.entrySet()) { String k = me.getKey();/*from w w w. j a v a2 s.co m*/ JsonElement v = me.getValue(); if (v.isJsonPrimitive() && ((JsonPrimitive) v).isNumber()) { map.put(k, v.getAsBigDecimal()); } else { Object value = context.deserialize(v, Object.class); map.put(k, value); } } return map; }
From source file:org.eel.kitchen.jsonschema.GsonProvider.java
License:Open Source License
private JsonNode gsonToJsonNode(final JsonElement element) { if (element.isJsonNull()) return factory.nullNode(); if (element.isJsonPrimitive()) return gsonToValueNode(element.getAsJsonPrimitive()); return element.isJsonArray() ? gsonToArrayNode(element) : gsonToObjectNode(element); }
From source file:org.geogit.web.api.repo.BatchedObjectResource.java
License:Open Source License
@Override protected Representation post(Representation entity) throws ResourceException { try {// w w w .j a va2 s . co m final Reader body = entity.getReader(); final JsonParser parser = new JsonParser(); final JsonElement messageJson = parser.parse(body); final List<ObjectId> want = new ArrayList<ObjectId>(); final List<ObjectId> have = new ArrayList<ObjectId>(); if (messageJson.isJsonObject()) { final JsonObject message = messageJson.getAsJsonObject(); final JsonArray wantArray; final JsonArray haveArray; if (message.has("want") && message.get("want").isJsonArray()) { wantArray = message.get("want").getAsJsonArray(); } else { wantArray = new JsonArray(); } if (message.has("have") && message.get("have").isJsonArray()) { haveArray = message.get("have").getAsJsonArray(); } else { haveArray = new JsonArray(); } for (final JsonElement e : wantArray) { if (e.isJsonPrimitive()) { want.add(ObjectId.valueOf(e.getAsJsonPrimitive().getAsString())); } } for (final JsonElement e : haveArray) { if (e.isJsonPrimitive()) { have.add(ObjectId.valueOf(e.getAsJsonPrimitive().getAsString())); } } } final GeoGIT ggit = (GeoGIT) getApplication().getContext().getAttributes().get("geogit"); final Repository repository = ggit.getRepository(); final Deduplicator deduplicator = ggit.command(CreateDeduplicator.class).call(); return new BinaryPackedObjectsRepresentation(new BinaryPackedObjects(repository.getObjectDatabase()), want, have, deduplicator); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.geogit.web.api.repo.FilteredChangesResource.java
License:Open Source License
@Override protected Representation post(Representation entity) throws ResourceException { try {// w ww.j a v a 2 s . c om final Reader body = entity.getReader(); final JsonParser parser = new JsonParser(); final JsonElement messageJson = parser.parse(body); final List<ObjectId> tracked = new ArrayList<ObjectId>(); RepositoryFilter filter = new RepositoryFilter(); ObjectId commitId = ObjectId.NULL; if (messageJson.isJsonObject()) { final JsonObject message = messageJson.getAsJsonObject(); final JsonArray trackedArray; if (message.has("tracked") && message.get("tracked").isJsonArray()) { trackedArray = message.get("tracked").getAsJsonArray(); } else { trackedArray = new JsonArray(); } if (message.has("commitId") && message.get("commitId").isJsonPrimitive()) { commitId = ObjectId.valueOf(message.get("commitId").getAsJsonPrimitive().getAsString()); } else { commitId = ObjectId.NULL; } for (final JsonElement e : trackedArray) { if (e.isJsonPrimitive()) { tracked.add(ObjectId.valueOf(e.getAsJsonPrimitive().getAsString())); } } if (message.has("filter") && message.get("filter").isJsonArray()) { JsonArray filterArray = message.get("filter").getAsJsonArray(); for (final JsonElement e : filterArray) { if (e.isJsonObject()) { JsonObject filterObject = e.getAsJsonObject(); String featureType = null; String filterType = null; String filterText = null; if (filterObject.has("featurepath") && filterObject.get("featurepath").isJsonPrimitive()) { featureType = filterObject.get("featurepath").getAsJsonPrimitive().getAsString(); } if (filterObject.has("type") && filterObject.get("type").isJsonPrimitive()) { filterType = filterObject.get("type").getAsJsonPrimitive().getAsString(); } if (filterObject.has("filter") && filterObject.get("filter").isJsonPrimitive()) { filterText = filterObject.get("filter").getAsJsonPrimitive().getAsString(); } if (featureType != null && filterType != null && filterText != null) { filter.addFilter(featureType, filterType, filterText); } } } } } final GeoGIT ggit = (GeoGIT) getApplication().getContext().getAttributes().get("geogit"); final Repository repository = ggit.getRepository(); RevCommit commit = repository.getCommit(commitId); ObjectId parent = ObjectId.NULL; if (commit.getParentIds().size() > 0) { parent = commit.getParentIds().get(0); } Iterator<DiffEntry> changes = ggit.command(DiffOp.class).setNewVersion(commit.getId()) .setOldVersion(parent).setReportTrees(true).call(); FilteredDiffIterator filteredChanges = new FilteredDiffIterator(changes, repository, filter) { @Override protected boolean trackingObject(ObjectId objectId) { return tracked.contains(objectId); } }; return new FilteredDiffIteratorRepresentation(new BinaryPackedChanges(repository), filteredChanges); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.hibernate.search.elasticsearch.client.impl.JestClient.java
License:LGPL
private String getErrorType(JestResult result) { JsonElement error = result.getJsonObject().get("error"); if (error == null || !error.isJsonObject()) { return null; }/*from w w w .j a v a 2s . c om*/ JsonElement errorType = error.getAsJsonObject().get("type"); if (errorType == null || !errorType.isJsonPrimitive()) { return null; } return errorType.getAsString(); }
From source file:org.hibernate.search.elasticsearch.query.impl.JsonDrivenProjection.java
License:LGPL
@Override public Object convertHit(JsonObject hit, ConversionContext conversionContext) { JsonElement value = extractFieldValue(hit.get("_source").getAsJsonObject(), absoluteName); if (value == null || value.isJsonNull()) { return null; }/* w ww. jav a 2s . co m*/ // TODO: HSEARCH-2255 should we do it? if (!value.isJsonPrimitive()) { throw LOG.unsupportedProjectionOfNonJsonPrimitiveFields(value); } JsonPrimitive primitive = value.getAsJsonPrimitive(); if (primitive.isBoolean()) { return primitive.getAsBoolean(); } else if (primitive.isNumber()) { // TODO HSEARCH-2255 this will expose a Gson-specific Number implementation; Can we somehow return an Integer, // Long... etc. instead? return primitive.getAsNumber(); } else if (primitive.isString()) { return primitive.getAsString(); } else { // TODO HSEARCH-2255 Better raise an exception? return primitive.toString(); } }
From source file:org.hibernate.search.elasticsearch.schema.impl.json.AnalysisJsonElementEquivalence.java
License:LGPL
/** * Determines whether two {@link JsonElement}s should be considered equivalent. * @param left An element whose equivalence to {@code right} will be tested. * @param right An element whose equivalence to {@code left} will be tested. * @return {@code true} if {@code left} and {@code right} are equivalent, {@code false} otherwise. *///ww w.j a v a2 s . c om public boolean isEquivalent(JsonElement left, JsonElement right) { if (left == null || right == null) { return left == right; } else { if (left.isJsonPrimitive() && right.isJsonPrimitive()) { return isPrimitiveEquivalent(left.getAsJsonPrimitive(), right.getAsJsonPrimitive()); } else if (left.isJsonArray() && right.isJsonArray()) { return isArrayEquivalent(left.getAsJsonArray(), right.getAsJsonArray()); } else if (left.isJsonObject() && right.isJsonObject()) { return isObjectEquivalent(left.getAsJsonObject(), right.getAsJsonObject()); } else { return isElementEquivalent(left, right); } } }
From source file:org.hillview.storage.JsonFileLoader.java
License:Open Source License
void append(IAppendableColumn[] columns, JsonElement e) { if (!e.isJsonObject()) this.error("JSON array element is not a JsonObject"); JsonObject obj = e.getAsJsonObject(); for (IAppendableColumn col : columns) { JsonElement el = obj.get(col.getName()); if (el == null || el.isJsonNull()) { col.appendMissing();/*from www . j av a 2 s . com*/ continue; } if (!el.isJsonPrimitive()) this.error("JSON array element is a non-primitive field"); JsonPrimitive prim = el.getAsJsonPrimitive(); if (prim.isBoolean()) { col.append(prim.getAsBoolean() ? "true" : "false"); } else if (prim.isNumber()) { col.append(prim.getAsDouble()); } else if (prim.isString()) { col.parseAndAppendString(prim.getAsString()); } else { this.error("Unexpected Json value" + prim.toString()); } } }
From source file:org.i3xx.step.uno.impl.util.GsonHashMapDeserializer.java
License:Apache License
public Object deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { //System.out.println("GND deserialize: "+json.toString()); if (json.isJsonNull()) return null; else if (json.isJsonPrimitive()) return handlePrimitive(json.getAsJsonPrimitive()); else if (json.isJsonArray()) return handleArray(json.getAsJsonArray(), context); else/* w w w .j av a2 s .c o m*/ return handleObject(json.getAsJsonObject(), context); }
From source file:org.immutables.mongo.fixture.holder.HolderJsonSerializer.java
License:Apache License
@Override public Holder deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { JsonObject root = (JsonObject) json; ImmutableHolder.Builder builder = ImmutableHolder.builder(); if (root.has("id")) { builder.id(root.get("id").getAsString()); }//from ww w . j av a 2 s . c o m JsonElement value = root.get(VALUE_PROPERTY); if (value == null) { throw new JsonParseException(String.format("%s not found for %s in JSON", VALUE_PROPERTY, type)); } if (value.isJsonObject()) { final String valueTypeName = value.getAsJsonObject().get(Holder.TYPE_PROPERTY).getAsString(); try { Class<?> valueType = Class.forName(valueTypeName); builder.value(context.deserialize(value, valueType)); } catch (ClassNotFoundException e) { throw new JsonParseException( String.format("Couldn't construct value class %s for %s", valueTypeName, type), e); } } else if (value.isJsonPrimitive()) { final JsonPrimitive primitive = value.getAsJsonPrimitive(); if (primitive.isString()) { builder.value(primitive.getAsString()); } else if (primitive.isNumber()) { builder.value(primitive.getAsInt()); } else if (primitive.isBoolean()) { builder.value(primitive.getAsBoolean()); } } else { throw new JsonParseException(String.format("Couldn't deserialize %s : %s. Not a primitive or object", VALUE_PROPERTY, value)); } return builder.build(); }