Example usage for com.google.gson JsonObject get

List of usage examples for com.google.gson JsonObject get

Introduction

In this page you can find the example usage for com.google.gson JsonObject get.

Prototype

public JsonElement get(String memberName) 

Source Link

Document

Returns the member with the specified name.

Usage

From source file:com.arangodb.impl.InternalGraphDriverImpl.java

License:Apache License

private String convertToString(EdgeDefinitionEntity edgeDefinition) {
    JsonObject rawEdgeDefinition = (JsonObject) EntityFactory
            .toJsonElement(new MapBuilder().put("edgeDefinition", edgeDefinition).get(), false);
    JsonElement edgeDefinitionJson = rawEdgeDefinition.get("edgeDefinition");
    return edgeDefinitionJson.toString();
}

From source file:com.arcbees.plugin.idea.wizards.createproject.FetchArchetypes.java

License:Apache License

private GsonBuilder createGsonBuilder() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new Date(json.getAsJsonPrimitive().getAsLong());
        }//from ww w  .  ja va  2s  . c om
    });
    gsonBuilder.registerTypeAdapter(ArchetypeCollection.class, new JsonDeserializer<ArchetypeCollection>() {
        public ArchetypeCollection deserialize(JsonElement json, Type typeOft,
                JsonDeserializationContext context) throws JsonParseException {
            JsonObject parentJson = json.getAsJsonObject();

            GsonBuilder gsonBuilder = new GsonBuilder();
            gsonBuilder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
                public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                        throws JsonParseException {
                    return new Date(json.getAsJsonPrimitive().getAsLong());
                }
            });
            Gson gson = gsonBuilder.create();

            ArchetypeCollection parent = gson.fromJson(json, ArchetypeCollection.class);
            List<Archetype> archetypes = null;

            if (parentJson.get("items").isJsonArray()) {
                JsonElement itemsJson = parentJson.get("items");
                archetypes = gson.fromJson(itemsJson, new TypeToken<List<Archetype>>() {
                }.getType());
            } else {
                Archetype single = gson.fromJson(parentJson.get("items"), Archetype.class);
                archetypes = new ArrayList<Archetype>();
                archetypes.add(single);
            }
            parent.setArchetypes(archetypes);
            return parent;
        }
    });
    return gsonBuilder;
}

From source file:com.arcbees.vcs.util.PolymorphicTypeAdapter.java

License:Apache License

@Override
public T deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    JsonPrimitive jsonPrimitive = (JsonPrimitive) jsonObject.get(CLASSNAME);
    String className = jsonPrimitive.getAsString();

    Class<?> clazz;/*from www . j a  va2s.co  m*/
    try {
        clazz = Class.forName(className);
    } catch (ClassNotFoundException e) {
        throw new JsonParseException(e.getMessage());
    }

    return context.deserialize(jsonObject.get(VALUE), clazz);
}

From source file:com.arpnetworking.kairosdb.HistogramDataPointFactory.java

License:Apache License

@Override
public DataPoint getDataPoint(final long timestamp, final JsonElement json) throws IOException {
    final TreeMap<Double, Integer> binValues = new TreeMap<>();

    final JsonObject object = json.getAsJsonObject();
    final double min = object.get("min").getAsDouble();
    final double max = object.get("max").getAsDouble();
    final double mean = object.get("mean").getAsDouble();
    final double sum = object.get("sum").getAsDouble();
    final JsonObject bins = object.get("bins").getAsJsonObject();

    for (Map.Entry<String, JsonElement> entry : bins.entrySet()) {
        binValues.put(Double.parseDouble(entry.getKey()), entry.getValue().getAsInt());
    }//from w w w .  ja va 2  s.  com

    return new HistogramDataPointImpl(timestamp, 7, binValues, min, max, mean, sum);
}

From source file:com.asakusafw.lang.inspection.json.NodeAdapter.java

License:Apache License

@Override
public InspectionNode deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonObject() == false) {
        throw new JsonParseException("node must be an object"); //$NON-NLS-1$
    }/*  w ww  .  j a va2s  .com*/
    JsonObject object = (JsonObject) json;
    String id = object.get(KEY_ID).getAsString();
    String title = object.get(KEY_TITLE).getAsString();
    List<Port> inputs = context.deserialize(object.get(KEY_INPUTS), TYPE_PORTS);
    List<Port> outputs = context.deserialize(object.get(KEY_OUTPUTS), TYPE_PORTS);
    Map<String, String> properties = context.deserialize(object.get(KEY_PROPERTIES), TYPE_PROPERTIES);
    List<InspectionNode> elements = context.deserialize(object.get(KEY_ELEMENTS), TYPE_NODES);

    InspectionNode result = new InspectionNode(id, title);
    put(inputs, result.getInputs());
    put(outputs, result.getOutputs());
    result.getProperties().putAll(properties);
    put(elements, result.getElements());
    return result;
}

From source file:com.asakusafw.lang.inspection.json.PortAdapter.java

License:Apache License

@Override
public InspectionNode.Port deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    if (json.isJsonObject() == false) {
        throw new JsonParseException("port must be an object"); //$NON-NLS-1$
    }/*from  www.  ja  v a2 s. c  o  m*/
    JsonObject object = (JsonObject) json;
    String id = object.get(KEY_ID).getAsString();
    Map<String, String> properties = context.deserialize(object.get(KEY_PROPERTIES), TYPE_PROPERTIES);
    Set<PortReference> opposites = context.deserialize(object.get(KEY_OPPOSITES), TYPE_REFERENCES);

    InspectionNode.Port result = new InspectionNode.Port(id);
    result.getProperties().putAll(properties);
    result.getOpposites().addAll(opposites);
    return result;
}

From source file:com.asakusafw.testdriver.json.JsonObjectDriver.java

License:Apache License

private JsonElement property(JsonObject context, PropertyName name) {
    assert context != null;
    assert name != null;
    String jsName = toJsName(name);
    JsonElement element = context.get(jsName);
    return element;
}

From source file:com.auth0.android.lock.internal.configuration.GsonDeserializer.java

License:Open Source License

<U> U requiredValue(String name, Type type, JsonObject object, JsonDeserializationContext context)
        throws JsonParseException {
    U value = context.deserialize(object.get(name), type);
    if (value == null) {
        throw new JsonParseException(String.format("Missing required attribute %s", name));
    }/*from   ww  w  . j av  a  2s .c o  m*/
    return value;
}

From source file:com.autoclavestudios.jbower.config.internal.JsonRegistryTranslator.java

License:Apache License

@Override
public Registry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {

    Registry registry = new Registry();
    try {/* ww  w . j  a va  2 s.  co m*/
        if (json.isJsonObject()) {
            JsonObject jsonObject = json.getAsJsonObject();
            if (jsonObject.has("search"))
                registry.search(ConvertToArray(jsonObject.get("search").getAsJsonArray()));
            if (jsonObject.has("publish"))
                registry.publish(ConvertToArray(jsonObject.get("publish").getAsJsonArray()));
            if (jsonObject.has("register"))
                registry.register(ConvertToArray(jsonObject.get("register").getAsJsonArray()));

        } else if (json.isJsonPrimitive()) {
            registry.all(json.getAsString());

        } else {
            throw new JsonParseException("Registry object is not a Json Object or Primitive");
        }
    } catch (MalformedURLException e) {
        logger.error("Malformed URL: {}", json.getAsString());
    }
    return registry;
}

From source file:com.avapira.bobroreader.hanabira.entity.HanabiraEntity.java

License:Open Source License

private static HanabiraThread createThreadWithPosts(JsonObject threadObject, @Nullable String boardKey) {
    int threadId = threadObject.get("thread_id").getAsInt();
    LocalDateTime modifiedDate = extractLocatDateTime(threadObject.get("last_modified"));

    HanabiraThread thread = Hanabira.getStem().findThreadById(threadId);
    boolean oldThread = thread != null;
    if (oldThread) {
        if (modifiedDate == null || !thread.isUpToDate(modifiedDate)) {
            // update thread meta
            thread.setPostsCount(threadObject.get("posts_count").getAsInt());
            thread.setArchived(threadObject.get("archived").getAsBoolean());
            thread.setFilesCount(threadObject.get("files_count").getAsInt());
            thread.setTitle(threadObject.get("title").getAsString());
            thread.setLastHit(extractLocatDateTime(threadObject.get("last_hit")));
            thread.setModifiedDate(modifiedDate);
        }/*from ww w .j av  a  2 s  . co  m*/
    } else {
        // brand new thread
        int displayId = threadObject.get("display_id").getAsInt();
        boolean archived = threadObject.get("archived").getAsBoolean();
        int postsCount = threadObject.get("posts_count").getAsInt();
        int filesCount = threadObject.get("files_count").getAsInt();
        String title = threadObject.get("title").getAsString();
        int boardId = boardKey != null ? HanabiraBoard.Info.getIdForKey(boardKey)
                : threadObject.get("board_id").getAsInt();
        boolean autosage = threadObject.get("autosage").getAsBoolean();
        LocalDateTime lastHit = extractLocatDateTime(threadObject.get("last_hit"));

        thread = new HanabiraThread(displayId, threadId, modifiedDate, postsCount, filesCount, boardId,
                archived, title, autosage, lastHit);
        Hanabira.getStem().saveThread(thread, boardKey);
    }

    // cache posts
    for (JsonElement postElement : threadObject.getAsJsonArray("posts")) {
        createAndSavePost(postElement.getAsJsonObject(), threadId, boardKey);
    }

    if (!oldThread) {
        // set thread creation date
        HanabiraPost opPost = Hanabira.getStem().findPostByDisplayId(boardKey, thread.getDisplayId());
        if (opPost == null || !opPost.isOp()) {
            throw new InputMismatchException("Op post not received");
        }
        thread.setCreatedDate(opPost.getCreatedDate());
    }
    return thread;
}