List of usage examples for com.google.gson JsonElement getAsJsonPrimitive
public JsonPrimitive getAsJsonPrimitive()
From source file:org.geogit.web.api.repo.FilteredChangesResource.java
License:Open Source License
@Override protected Representation post(Representation entity) throws ResourceException { try {//from w ww.j a v a 2 s . com 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.geppetto.frontend.controllers.ConnectionHandler.java
License:Open Source License
/** * @return//ww w .ja v a2 s . c o m */ private Gson getGson() { GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() { @Override public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return new Date(json.getAsJsonPrimitive().getAsLong()); } }); return builder.create(); }
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; }//from w w w . j a v a2 s . c o 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.query.impl.TwoWayFieldBridgeProjection.java
License:LGPL
public void addDocumentFieldsRecursively(Document tmp, JsonElement value, String fieldName) { if (value == null || value.isJsonNull()) { return;//from w w w . j a v a 2 s . c o m } PrimitiveProjection configuredProjection = primitiveProjections.get(fieldName); if (configuredProjection != null) { // Those projections are handled separately, see the calling method return; } if (value.isJsonObject()) { JsonObject jsonObject = value.getAsJsonObject(); for (Map.Entry<String, JsonElement> entry : jsonObject.entrySet()) { String nestedFieldName = fieldName + "." + entry.getKey(); JsonElement nestedFieldValue = entry.getValue(); addDocumentFieldsRecursively(tmp, nestedFieldValue, nestedFieldName); } } else if (value.isJsonArray()) { JsonArray jsonArray = value.getAsJsonArray(); for (JsonElement nestedValue : jsonArray) { addDocumentFieldsRecursively(tmp, nestedValue, fieldName); } } else { JsonPrimitive primitive = value.getAsJsonPrimitive(); if (primitive.isBoolean()) { tmp.add(new StringField(fieldName, String.valueOf(primitive.getAsBoolean()), Store.NO)); } else if (primitive.isNumber()) { tmp.add(new DoubleField(fieldName, primitive.getAsDouble(), Store.NO)); } else if (primitive.isString()) { tmp.add(new StringField(fieldName, primitive.getAsString(), Store.NO)); } else { // TODO HSEARCH-2255 Better raise an exception? tmp.add(new StringField(fieldName, primitive.getAsString(), Store.NO)); } } }
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. *//* w ww . j a v a 2 s .c o m*/ 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
private static ContentsKind getKind(@Nullable JsonElement e) { if (e == null || e.isJsonNull()) return ContentsKind.None; if (e.isJsonArray() || e.isJsonObject()) throw new RuntimeException("Values must be simple " + e); JsonPrimitive prim = e.getAsJsonPrimitive(); if (prim.isBoolean()) return ContentsKind.String; if (prim.isNumber()) return ContentsKind.Double; if (prim.isString()) return ContentsKind.String; throw new RuntimeException("Unexpected JSON value " + prim); }
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();// w w w . j a va 2s.c om 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// ww w . java 2 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()); }//w ww . j a v a2 s.co 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(); }
From source file:org.ireas.intuition.IntuitionLoader.java
License:Open Source License
private Optional<JsonObject> getDomainObject(final JsonElement element) { Optional<JsonObject> domainObject = Optional.absent(); // two types of valid responses: // (1) {"messages": {"<domain>": { } } } // --> messages found // (2) {"messages": {"<domain>": false} } // --> no messages available if (!element.isJsonObject()) { throw new IllegalArgumentException(); }/* ww w.ja v a 2 s .co m*/ JsonObject rootObject = element.getAsJsonObject(); if (!rootObject.has(KEY_MESSAGES)) { throw new IllegalArgumentException(); } JsonElement messagesElement = rootObject.get(KEY_MESSAGES); if (!messagesElement.isJsonObject()) { throw new IllegalArgumentException(); } JsonObject messagesObject = messagesElement.getAsJsonObject(); if (!messagesObject.has(domain)) { throw new IllegalArgumentException(); } JsonElement domainElement = messagesObject.get(domain); if (domainElement.isJsonObject()) { // valid response (1): messages found domainObject = Optional.of(domainElement.getAsJsonObject()); } else if (domainElement.isJsonPrimitive()) { JsonPrimitive domainPrimitive = domainElement.getAsJsonPrimitive(); if (!domainPrimitive.isBoolean()) { throw new IllegalArgumentException(); } boolean domainBoolean = domainPrimitive.getAsBoolean(); if (domainBoolean) { throw new IllegalArgumentException(); } // valid response (2): no messages available } else { throw new IllegalArgumentException(); } return domainObject; }