Search in sources :

Example 96 with GsonBuilder

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);
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) AmazonBook(com.gpcoder.gson.object.AmazonBook) Calendar(java.util.Calendar) Gson(com.google.gson.Gson)

Example 97 with GsonBuilder

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);
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) Student(com.gpcoder.gson.object.Student)

Example 98 with GsonBuilder

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());
}
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 99 with GsonBuilder

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());
}
Also used : PrintStream(java.io.PrintStream) GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) LargeData(com.gpcoder.gson.object.LargeData) File(java.io.File) StopWatch(org.apache.commons.lang3.time.StopWatch)

Example 100 with GsonBuilder

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;
    }
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) CSVWriter(au.com.bytecode.opencsv.CSVWriter)

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