Search in sources :

Example 91 with GsonBuilder

use of com.google.gson.GsonBuilder in project summerb by skarpushin.

the class EntityChangedEventAdapterTest method getFixture.

private Gson getFixture() {
    GsonBuilder b = new GsonBuilder();
    b.registerTypeAdapter(EntityChangedEvent.class, new EntityChangedEventAdapter());
    Gson gson = b.create();
    return gson;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson)

Example 92 with GsonBuilder

use of com.google.gson.GsonBuilder in project Applozic-Android-SDK by AppLozic.

the class GsonUtils method getObjectListFromJson.

public static Object getObjectListFromJson(String json, String key, Type type) {
    Gson gson = new GsonBuilder().setExclusionStrategies(new AnnotationExclusionStrategy()).create();
    JsonParser parser = new JsonParser();
    String element = parser.parse(json).getAsJsonObject().get(key).toString();
    return gson.fromJson(element, type);
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) JsonParser(com.google.gson.JsonParser)

Example 93 with GsonBuilder

use of com.google.gson.GsonBuilder in project artisan-worktables by codetaylor.

the class ModuleTools method onPreInitializationEvent.

@Override
public void onPreInitializationEvent(FMLPreInitializationEvent event) {
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    File configurationDirectory = event.getModConfigurationDirectory();
    Path configurationPath = Paths.get(configurationDirectory.toString(), ModuleTools.MOD_ID);
    if (!Files.exists(configurationPath)) {
        try {
            Files.createDirectories(configurationPath);
        } catch (IOException e) {
            event.getModLog().error("", e);
        }
    }
    Path generatedPath = Paths.get(configurationPath.toString(), "artisanworktables.module.Tools.Materials.Generated.json");
    Path customPath = Paths.get(configurationPath.toString(), "artisanworktables.module.Tools.Materials.Custom.json");
    // Delete the generated file if it exists.
    if (Files.exists(generatedPath)) {
        try {
            Files.delete(generatedPath);
        } catch (IOException e) {
            event.getModLog().error("", e);
        }
    }
    // Create and write the generated file.
    BufferedWriter writer = null;
    try {
        writer = Files.newBufferedWriter(generatedPath);
        gson.toJson(new DataCustomMaterialListFactory().create(), writer);
        writer.close();
    } catch (IOException e) {
        event.getModLog().error("", e);
    } finally {
        FileHelper.closeSilently(writer);
    }
    // Copy the generated file to the custom file if the custom file doesn't exist.
    if (!Files.exists(customPath)) {
        try {
            Files.copy(generatedPath, customPath);
        } catch (IOException e) {
            event.getModLog().error("", e);
        }
    }
    BufferedReader reader = null;
    try {
        reader = Files.newBufferedReader(customPath);
        DataCustomMaterialList dataCustomMaterialList = gson.fromJson(reader, DataCustomMaterialList.class);
        CustomMaterialListConverter customMaterialListConverter = new CustomMaterialListConverter(new CustomMaterialValidator(), new CustomMaterialConverter(new RecipeItemParser()));
        this.materialList = customMaterialListConverter.convert(dataCustomMaterialList, event.getModLog());
    } catch (IOException e) {
        event.getModLog().error("", e);
    } finally {
        FileHelper.closeSilently(reader);
    }
    super.onPreInitializationEvent(event);
}
Also used : Path(java.nio.file.Path) RecipeItemParser(com.codetaylor.mc.athenaeum.parser.recipe.item.RecipeItemParser) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 94 with GsonBuilder

use of com.google.gson.GsonBuilder in project Java-Tutorial by gpcodervn.

the class LargeDataTypeAdapterStreamingTest method main.

public static void main(final String[] args) throws IOException {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    // Configure GSON
    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(LargeData.class, new LargeDataTypeAdapter());
    gsonBuilder.setPrettyPrinting();
    final Gson gson = gsonBuilder.create();
    final LargeData data = new LargeData();
    data.create(10485760);
    final File dir = new File("data");
    dir.mkdirs();
    try (OutputStream os = new FileOutputStream(new File(dir, "outputTypeAdapterStreaming.json"));
        BufferedWriter out = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"))) {
        gson.toJson(data, out);
    }
    stopWatch.stop();
    System.out.println("Done in " + stopWatch.getTime());
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) Gson(com.google.gson.Gson) LargeData(com.gpcoder.gson.object.LargeData) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) StopWatch(org.apache.commons.lang3.time.StopWatch) BufferedWriter(java.io.BufferedWriter)

Example 95 with GsonBuilder

use of com.google.gson.GsonBuilder in project Java-Tutorial by gpcodervn.

the class CollectionExample2 method main.

public static void main(String[] args) {
    // Gson gson = new Gson();
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    List<Person> persons = new ArrayList<Person>();
    persons.add(new Person("GP Coder", "Viet Nam"));
    persons.add(new Person("Vincent", "Canada"));
    // Serialization
    String json = gson.toJson(persons);
    System.out.println(json);
    // Deserialization
    Type collectionType = new TypeToken<List<Person>>() {
    }.getType();
    List<Person> persons2 = gson.fromJson(json, collectionType);
// ==> persons2 is same as persons
}
Also used : Type(java.lang.reflect.Type) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) List(java.util.List) ArrayList(java.util.ArrayList)

Aggregations

GsonBuilder (com.google.gson.GsonBuilder)1067 Gson (com.google.gson.Gson)803 IOException (java.io.IOException)185 Test (org.junit.Test)141 ArrayList (java.util.ArrayList)101 JsonObject (com.google.gson.JsonObject)90 File (java.io.File)80 JsonElement (com.google.gson.JsonElement)78 HashMap (java.util.HashMap)67 List (java.util.List)62 Map (java.util.Map)59 Retrofit (retrofit2.Retrofit)56 Type (java.lang.reflect.Type)52 FileNotFoundException (java.io.FileNotFoundException)42 TypeToken (com.google.gson.reflect.TypeToken)40 ResponseBody (okhttp3.ResponseBody)39 FileOutputStream (java.io.FileOutputStream)38 Call (retrofit2.Call)38 JsonSyntaxException (com.google.gson.JsonSyntaxException)37 JsonParser (com.google.gson.JsonParser)36