Search in sources :

Example 31 with GsonBuilder

use of com.google.gson.GsonBuilder in project MyDiary by erttyy8821.

the class ExportAsyncTask method outputBackupJson.

private void outputBackupJson() throws IOException {
    Writer writer = new FileWriter(backupJsonFilePath);
    Gson gson = new GsonBuilder().create();
    gson.toJson(backupManager, writer);
    writer.close();
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) FileWriter(java.io.FileWriter) Gson(com.google.gson.Gson) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 32 with GsonBuilder

use of com.google.gson.GsonBuilder in project immutables by immutables.

the class JaxrsTest method propagateGsonAttributes.

@Test
public void propagateGsonAttributes() {
    Gson gson = new GsonBuilder().serializeNulls().disableHtmlEscaping().setPrettyPrinting().create();
    GsonOptions options = new GsonOptions(gson, true);
    JsonReader reader = new JsonReader(new StringReader(""));
    options.setReaderOptions(reader);
    check(reader.isLenient());
    JsonWriter writer = new JsonWriter(new StringWriter());
    options.setWriterOptions(writer);
    check(writer.isLenient());
    check(!writer.isHtmlSafe());
    check(writer.getSerializeNulls());
    // checks pretty printing
    check(gson.toJson(Collections.singletonMap("k", "v"))).is("{\n  \"k\": \"v\"\n}");
}
Also used : GsonOptions(org.immutables.gson.stream.GsonMessageBodyProvider.GsonOptions) StringWriter(java.io.StringWriter) GsonBuilder(com.google.gson.GsonBuilder) StringReader(java.io.StringReader) Gson(com.google.gson.Gson) JsonReader(com.google.gson.stream.JsonReader) JsonWriter(com.google.gson.stream.JsonWriter) Test(org.junit.Test)

Example 33 with GsonBuilder

use of com.google.gson.GsonBuilder in project immutables by immutables.

the class Ent method main.

public static void main(String... args) throws UnknownHostException {
    MongoClient client = new MongoClient("localhost");
    RepositorySetup setup = RepositorySetup.builder().database(client.getDB("test")).executor(MoreExecutors.listeningDecorator(Executors.newCachedThreadPool())).gson(new GsonBuilder().registerTypeAdapterFactory(new GsonAdaptersEnt()).create()).build();
    EntRepository repository = new EntRepository(setup);
    EntRepository.Criteria where = repository.criteria().uuid("8b7a881c-6ccb-4ada-8f6a-60cc99e6aa20").actionIn("BAN", "IPBAN");
    Criteria or = where.expiresAbsent().or().with(where).expiresGreaterThan(TimeInstant.of(1467364749679L));
    System.out.println(or);
    repository.find(or).fetchAll().getUnchecked();
}
Also used : Criteria(org.immutables.mongo.fixture.ent.EntRepository.Criteria) MongoClient(com.mongodb.MongoClient) GsonBuilder(com.google.gson.GsonBuilder) RepositorySetup(org.immutables.mongo.repository.RepositorySetup) Criteria(org.immutables.mongo.fixture.ent.EntRepository.Criteria)

Example 34 with GsonBuilder

use of com.google.gson.GsonBuilder in project scss-lint-plugin by idok.

the class Lint method parse.

//    public File file;
//
//    public static Lint read(String xml) {
//        XStream xstream = new XStream();
//        xstream.alias("lint", Lint.class);
//        xstream.alias("file", File.class);
//        xstream.alias("issue", Issue.class);
//        xstream.addImplicitCollection(File.class, "issues");
//        xstream.useAttributeFor(File.class, "name");
//        xstream.useAttributeFor(Issue.class, "linter");
//        xstream.useAttributeFor(Issue.class, "line");
//        xstream.useAttributeFor(Issue.class, "column");
//        xstream.useAttributeFor(Issue.class, "length");
//        xstream.useAttributeFor(Issue.class, "severity");
//        xstream.useAttributeFor(Issue.class, "reason");
//        return (Lint) xstream.fromXML(xml);
//    }
//
//    public static class File {
//        public String name;
//        public List<Issue> issues = new ArrayList<Issue>();
//    }
public static Map<String, List<Issue>> parse(String json) {
    GsonBuilder builder = new GsonBuilder();
    //        builder.registerTypeAdapterFactory(adapter);
    Gson g = builder.setPrettyPrinting().create();
    Type listType = new TypeToken<Map<String, List<Issue>>>() {
    }.getType();
    return g.fromJson(json, listType);
}
Also used : Type(java.lang.reflect.Type) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Map(java.util.Map)

Example 35 with GsonBuilder

use of com.google.gson.GsonBuilder in project MinecraftForge by MinecraftForge.

the class MetadataCollection method from.

public static MetadataCollection from(@Nullable InputStream inputStream, String sourceName) {
    if (inputStream == null) {
        return new MetadataCollection();
    }
    InputStreamReader reader = new InputStreamReader(inputStream);
    try {
        MetadataCollection collection;
        Gson gson = new GsonBuilder().registerTypeAdapter(ArtifactVersion.class, new ArtifactVersionAdapter()).create();
        JsonParser parser = new JsonParser();
        JsonElement rootElement = parser.parse(reader);
        if (rootElement.isJsonArray()) {
            collection = new MetadataCollection();
            JsonArray jsonList = rootElement.getAsJsonArray();
            collection.modList = new ModMetadata[jsonList.size()];
            int i = 0;
            for (JsonElement mod : jsonList) {
                collection.modList[i++] = gson.fromJson(mod, ModMetadata.class);
            }
        } else {
            collection = gson.fromJson(rootElement, MetadataCollection.class);
        }
        collection.parseModMetadataList();
        return collection;
    } catch (JsonParseException e) {
        FMLLog.log(Level.ERROR, e, "The mcmod.info file in %s cannot be parsed as valid JSON. It will be ignored", sourceName);
        return new MetadataCollection();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) JsonParseException(com.google.gson.JsonParseException) JsonParseException(com.google.gson.JsonParseException) IOException(java.io.IOException) JsonArray(com.google.gson.JsonArray) ArtifactVersion(net.minecraftforge.fml.common.versioning.ArtifactVersion) JsonElement(com.google.gson.JsonElement) JsonParser(com.google.gson.JsonParser)

Aggregations

GsonBuilder (com.google.gson.GsonBuilder)268 Gson (com.google.gson.Gson)175 IOException (java.io.IOException)44 ArrayList (java.util.ArrayList)28 JsonElement (com.google.gson.JsonElement)27 Type (java.lang.reflect.Type)27 Map (java.util.Map)26 JsonObject (com.google.gson.JsonObject)20 HashMap (java.util.HashMap)20 Test (org.junit.Test)18 JsonSyntaxException (com.google.gson.JsonSyntaxException)15 Date (java.util.Date)14 File (java.io.File)13 InputStream (java.io.InputStream)13 List (java.util.List)13 OkHttpClient (okhttp3.OkHttpClient)13 Retrofit (retrofit2.Retrofit)13 InputStreamReader (java.io.InputStreamReader)11 JsonParseException (com.google.gson.JsonParseException)9 JsonParser (com.google.gson.JsonParser)9