Example usage for com.google.gson JsonElement getAsJsonObject

List of usage examples for com.google.gson JsonElement getAsJsonObject

Introduction

In this page you can find the example usage for com.google.gson JsonElement getAsJsonObject.

Prototype

public JsonObject getAsJsonObject() 

Source Link

Document

convenience method to get this element as a JsonObject .

Usage

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  w ww.  j ava 2s  .c o m*/
    });
    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   w  w w .  j  ava  2 s. com*/
    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());
    }/* ww  w . ja v  a 2 s.c  o m*/

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

From source file:com.atlauncher.data.loaders.forge.DownloadsTypeAdapter.java

License:Open Source License

@Override
public Downloads deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    DownloadsItem artifact = null;/*  w  w  w  .  j a  v  a2 s. com*/

    final JsonObject rootJsonObject = json.getAsJsonObject();

    if (rootJsonObject.has("artifact")) {
        final JsonObject artifactObject = rootJsonObject.getAsJsonObject("artifact");
        artifact = Gsons.DEFAULT_ALT.fromJson(artifactObject, DownloadsItem.class);
    }

    return new Downloads(artifact);
}

From source file:com.atlauncher.data.mojang.DownloadsTypeAdapter.java

License:Open Source License

@Override
public Downloads deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    DownloadsItem artifact = null;//from w ww .j a v a2 s.c o  m
    Map<String, DownloadsItem> classifiers = new HashMap<String, DownloadsItem>();

    final JsonObject rootJsonObject = json.getAsJsonObject();

    if (rootJsonObject.has("artifact")) {
        final JsonObject artifactObject = rootJsonObject.getAsJsonObject("artifact");
        artifact = Gsons.DEFAULT_ALT.fromJson(artifactObject, DownloadsItem.class);
    }

    if (rootJsonObject.has("classifiers")) {
        final JsonObject classifiersObject = rootJsonObject.getAsJsonObject("classifiers");

        Set<Entry<String, JsonElement>> entrySet = classifiersObject.entrySet();

        for (Map.Entry<String, JsonElement> entry : entrySet) {
            classifiers.put(entry.getKey(),
                    Gsons.DEFAULT_ALT.fromJson(entry.getValue().getAsJsonObject(), DownloadsItem.class));
        }
    }

    return new Downloads(artifact, classifiers);
}

From source file:com.atlauncher.data.mojang.MojangArgumentsTypeAdapter.java

License:Open Source License

@Override
public MojangArguments deserialize(JsonElement json, Type type, JsonDeserializationContext context)
        throws JsonParseException {
    List<ArgumentRule> game = new ArrayList<ArgumentRule>();
    List<ArgumentRule> jvm = new ArrayList<ArgumentRule>();

    final JsonObject rootJsonObject = json.getAsJsonObject();
    final JsonArray gameArray = rootJsonObject.getAsJsonArray("game");
    final JsonArray jvmArray = rootJsonObject.getAsJsonArray("jvm");

    for (JsonElement gameArg : gameArray) {
        if (gameArg.isJsonObject()) {
            JsonObject argument = gameArg.getAsJsonObject();
            game.add(Gsons.DEFAULT_ALT.fromJson(argument, ArgumentRule.class));
        } else {/*from   w  w  w .jav a 2  s.  c  om*/
            String argument = gameArg.getAsString();
            game.add(new ArgumentRule(null, argument));
        }
    }

    for (JsonElement gameArg : jvmArray) {
        if (gameArg.isJsonObject()) {
            JsonObject argument = gameArg.getAsJsonObject();
            jvm.add(Gsons.DEFAULT_ALT.fromJson(argument, ArgumentRule.class));
        } else {
            String argument = gameArg.getAsString();
            jvm.add(new ArgumentRule(null, argument));
        }
    }

    return new MojangArguments(game, jvm);
}

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

License:Open Source License

@Override
public List<Connection> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
        throws JsonParseException {
    assertJsonObject(json);/*from   www  .  jav  a 2  s .  c  om*/

    final JsonObject object = json.getAsJsonObject();

    requiredValue("id", String.class, object, context);
    requiredValue("tenant", String.class, object, context);
    requiredValue("authorize", String.class, object, context);
    requiredValue("callback", String.class, object, context);

    requiredValue("strategies", JsonArray.class, object, context);
    final JsonArray strategies = object.getAsJsonArray("strategies");
    return mergeConnections(strategies, context);
}

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

License:Open Source License

private List<Connection> parseStrategy(JsonElement json, JsonDeserializationContext context) {
    final JsonObject strategy = json.getAsJsonObject();
    String name = requiredValue("name", String.class, strategy, context);
    requiredValue("connections", Object.class, strategy, context);

    JsonArray connectionsArray = strategy.getAsJsonArray("connections");
    List<Connection> connections = new ArrayList<>();
    for (int i = 0; i < connectionsArray.size(); i++) {
        final JsonObject connectionJson = connectionsArray.get(i).getAsJsonObject();
        requiredValue("name", String.class, connectionJson, context);
        Type mapType = new TypeToken<LinkedTreeMap<String, Object>>() {
        }.getType();//from w  w  w. j a v a2s  . c  o  m
        Map<String, Object> values = context.deserialize(connectionJson, mapType);
        connections.add(Connection.newConnectionFor(name, values));
    }
    return connections;
}

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 {/* w ww.j  a v  a  2  s .c  om*/
        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);
        }//  w ww  .j av a2s .c o 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;
}