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;
}
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);
}
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);
}
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());
}
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
}
Aggregations