use of com.google.gson.GsonBuilder in project Java-Tutorial by gpcodervn.
the class JsonAdapterExample method main.
public static void main(String[] args) {
final GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.registerTypeAdapter(AmazonBook.class, new CustomTypeAdapter());
gsonBuilder.setPrettyPrinting();
final Gson gson = gsonBuilder.create();
final AmazonBook book = new AmazonBook();
book.setTitle("Head First Design Patterns");
book.setIsbn10("0596007124");
book.setIsbn13("978-0596007126");
book.setPrice(52.41);
Calendar c = Calendar.getInstance();
c.set(2004, Calendar.OCTOBER, 1);
book.setPublishedDate(c.getTime());
String[] authors = new String[] { "Eric Freeman", "Bert Bates", "Kathy Sierra", "Elisabeth Robson" };
book.setAuthors(authors);
System.out.println("Convert Book object to JSON string: ");
final String json = gson.toJson(book);
System.out.println(json);
System.out.println("Convert JSON String to Book object: ");
final AmazonBook parsedBook1 = gson.fromJson(json, AmazonBook.class);
System.out.println(parsedBook1);
}
use of com.google.gson.GsonBuilder in project Java-Tutorial by gpcodervn.
the class SerializedNameExample method main.
public static void main(String[] args) {
final GsonBuilder builder = new GsonBuilder();
final Gson gson = builder.create();
final Student student = new Student(1, "GP Coder", "Java Dev");
final String json = gson.toJson(student);
System.out.println("Json: " + json);
final Student student2 = gson.fromJson(json, Student.class);
System.out.println("Java Object: " + student2);
}
use of com.google.gson.GsonBuilder in project Java-Tutorial by gpcodervn.
the class LargeDataSerializerStreamingTest 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 LargeDataSerializer());
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, "outputSerialiserStreaming.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 LargeDataSerializerTest 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 LargeDataSerializer());
gsonBuilder.setPrettyPrinting();
final Gson gson = gsonBuilder.create();
final LargeData data = new LargeData();
data.create(10485760);
final String json = gson.toJson(data);
final File dir = new File("data");
dir.mkdirs();
try (PrintStream out = new PrintStream(new File(dir, "outputSerialiser.json"), "UTF-8")) {
out.println(json);
}
stopWatch.stop();
System.out.println("Done in " + stopWatch.getTime());
}
use of com.google.gson.GsonBuilder in project epadd by ePADD.
the class LabelManager method writeObjectToStream.
/*
argument is the directory where json and csv files will be generated. Label meta data will be stored
in a json file whereas the mapping of docids to labelids will be stored as csv.
*/
public void writeObjectToStream(String dirname) {
// writing labelinfo map to json format
FileWriter writer = null;
try {
String str = dirname + File.separator + JSONFILENAME;
writer = new FileWriter(str);
Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.toJson(labelInfoMap, writer);
writer.close();
} catch (IOException e) {
log.warn("Unable to write labelinfo file");
} finally {
try {
writer.close();
} catch (IOException e) {
}
}
// writing docToLabelIDmap to csv
try {
FileWriter fw = new FileWriter(dirname + File.separator + CSVFILENAME);
CSVWriter csvwriter = new CSVWriter(fw, ',', '"', '\n');
// write the header line: "DocID,LabelID ".
List<String> line = new ArrayList<>();
line.add("DocID");
line.add("LabelID");
csvwriter.writeNext(line.toArray(new String[line.size()]));
// write the records
for (String docid : docToLabelID.keySet()) {
for (String labid : docToLabelID.get(docid)) {
line = new ArrayList<>();
line.add(docid);
line.add(labid);
csvwriter.writeNext(line.toArray(new String[line.size()]));
}
}
csvwriter.close();
fw.close();
} catch (IOException e) {
log.warn("Unable to write docid to label map in csv file");
return;
}
}
Aggregations