Example usage for com.google.gson GsonBuilder create

List of usage examples for com.google.gson GsonBuilder create

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder create.

Prototype

public Gson create() 

Source Link

Document

Creates a Gson instance based on the current configuration.

Usage

From source file:com.android.ide.common.blame.parser.JsonEncodedGradleMessageParser.java

License:Apache License

@Override
public boolean parse(@NonNull String line, @NonNull OutputLineReader reader, @NonNull List<Message> messages,
        @NonNull ILogger logger) throws ParsingFailedException {
    Matcher m = MSG_PATTERN.matcher(line);
    if (!m.matches()) {
        return false;
    }/*from  ww w.  ja  va  2 s.  c  o  m*/
    String json = m.group(1);
    if (json.trim().isEmpty()) {
        return false;
    }

    GsonBuilder gsonBuilder = new GsonBuilder();
    MessageJsonSerializer.registerTypeAdapters(gsonBuilder);
    Gson gson = gsonBuilder.create();
    try {
        Message msg = gson.fromJson(json, Message.class);
        messages.add(msg);
        return true;
    } catch (JsonParseException e) {
        throw new ParsingFailedException(e);
    }
}

From source file:com.android.manifmerger.Actions.java

License:Apache License

@NonNull
public String persist() throws IOException {
    //noinspection SpellCheckingInspection
    GsonBuilder gson = new GsonBuilder().setPrettyPrinting();
    gson.enableComplexMapKeySerialization();
    MessageJsonSerializer.registerTypeAdapters(gson);
    return gson.create().toJson(this);
}

From source file:com.android.manifmerger.Actions.java

License:Apache License

@SuppressWarnings("SpellCheckingInspection")
@NonNull/*ww w .j  a v  a 2 s . c o  m*/
private static Gson getGsonParser() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.enableComplexMapKeySerialization();
    gsonBuilder.registerTypeAdapter(XmlNode.NodeName.class, new NodeNameDeserializer());
    MessageJsonSerializer.registerTypeAdapters(gsonBuilder);
    return gsonBuilder.create();
}

From source file:com.android.sdk.core.operations.ServiceOperation.java

License:Open Source License

private T getResultFromJson(JSONObject jsonObject) {
    requireNonNull(jsonObject, "responseJson");
    final GsonBuilder builder = new GsonBuilder();
    final Gson gson = builder.create();

    Object obj = gson.fromJson(jsonObject.toString(), resultType);
    ensureNonNull(obj);// ww  w . j a v  a 2s.co  m
    ensure(resultType.isAssignableFrom(obj.getClass()),
            "Expected type: " + resultType + " actual type: " + obj);

    return (T) obj;
}

From source file:com.android.tools.idea.stats.DistributionService.java

License:Apache License

@Nullable
private static List<Distribution> loadDistributionsFromJson(String jsonString) {
    Type fullRevisionType = new TypeToken<Revision>() {
    }.getType();/*  ww w.  j  ava  2s.c  o  m*/
    GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(fullRevisionType,
            new JsonDeserializer<Revision>() {
                @Override
                public Revision deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                        throws JsonParseException {
                    return Revision.parseRevision(json.getAsString());
                }
            });
    Gson gson = gsonBuilder.create();
    Type listType = new TypeToken<ArrayList<Distribution>>() {
    }.getType();
    try {
        return gson.fromJson(jsonString, listType);
    } catch (JsonParseException e) {
        LOG.error("Parse exception while reading distributions.json", e);
    }
    return null;
}

From source file:com.android.tools.idea.wizard.DistributionChartComponent.java

License:Apache License

@Nullable
private static List<Distribution> loadDistributionsFromJson(String jsonString) {
    Type fullRevisionType = new TypeToken<FullRevision>() {
    }.getType();/*  www.  j a  v  a  2 s  .c  om*/
    GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(fullRevisionType,
            new JsonDeserializer<FullRevision>() {
                @Override
                public FullRevision deserialize(JsonElement json, Type typeOfT,
                        JsonDeserializationContext context) throws JsonParseException {
                    return FullRevision.parseRevision(json.getAsString());
                }
            });
    Gson gson = gsonBuilder.create();
    Type listType = new TypeToken<ArrayList<Distribution>>() {
    }.getType();
    try {
        return gson.fromJson(jsonString, listType);
    } catch (JsonParseException e) {
        LOG.error(e);
    }
    return null;
}

From source file:com.android.volley.GsonUtils.java

License:Open Source License

/**
 * Create the standard {@link com.google.gson.Gson} configuration
 *
 * @param serializeNulls//  www. ja  va  2s . c  o m
 *            whether nulls should be serialized
 *
 * @return created gson, never null
 */
public static final Gson createGson(final boolean serializeNulls) {
    final GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new DateFormatter());
    builder.enableComplexMapKeySerialization();
    //      builder.setFieldNamingPolicy(LOWER_CASE_WITH_UNDERSCORES);
    if (serializeNulls)
        builder.serializeNulls();
    return builder.create();
    //        private static final Gson gson  = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().enableComplexMapKeySerialization()
    //                .serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss:SSS ")
    //                .setPrettyPrinting().setVersion(1.0).create();
}

From source file:com.antew.redditinpictures.library.json.JsonDeserializer.java

License:Apache License

public static Gson getGson() {
    if (gson == null) {
        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(Vote.class, new VoteAdapter());
        builder.serializeNulls();// w  w  w  .j a  v a 2s.c  om
        gson = builder.create();
    }

    return gson;
}

From source file:com.anyun.sample.db.doma.entity.DefaultSerializeEntity.java

@Override
public final String asJson() {
    GsonBuilder gb = new GsonBuilder();
    gb.disableHtmlEscaping();//from   w w  w. j  a  v a  2s .c o  m
    Gson gson = gb.create();
    return gson.toJson(this);
}

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());
        }/*  w w w .j  a  va2 s.co 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;
}