Example usage for com.google.gson GsonBuilder setPrettyPrinting

List of usage examples for com.google.gson GsonBuilder setPrettyPrinting

Introduction

In this page you can find the example usage for com.google.gson GsonBuilder setPrettyPrinting.

Prototype

public GsonBuilder setPrettyPrinting() 

Source Link

Document

Configures Gson to output Json that fits in a page for pretty printing.

Usage

From source file:com.javacreed.examples.gson.part3.Example1.java

License:Apache License

public static void main(final String[] args) throws IOException {
    // Configure GSON
    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    final Gson gson = gsonBuilder.create();

    final Author joshuaBloch = new Author();
    joshuaBloch.setId(1);/*from w w w.  j  a  va  2  s  . c  o  m*/
    joshuaBloch.setName("Joshua Bloch");

    final Author nealGafter = new Author();
    nealGafter.setId(2);
    nealGafter.setName("Neal Gafter");

    final Book javaPuzzlers = new Book();
    javaPuzzlers.setTitle("Java Puzzlers: Traps, Pitfalls, and Corner Cases");
    javaPuzzlers.setIsbn("032133678X");
    javaPuzzlers.setAuthors(new Author[] { joshuaBloch, nealGafter });

    final Book effectiveJava = new Book();
    effectiveJava.setTitle("Effective Java (2nd Edition)");
    effectiveJava.setIsbn("0321356683");
    effectiveJava.setAuthors(new Author[] { joshuaBloch });

    final Book[] books = new Book[] { javaPuzzlers, effectiveJava };

    final String json = gson.toJson(books);
    System.out.println(json);
}

From source file:com.javacreed.examples.gson.part3.Example2.java

License:Apache License

public static void main(final String[] args) throws IOException {
    // Configure GSON
    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(Book.class, new BookSerialiser());
    gsonBuilder.setPrettyPrinting();
    final Gson gson = gsonBuilder.create();

    final Author joshuaBloch = new Author();
    joshuaBloch.setId(1);/*from  w  w  w.j a  v  a 2  s. c  o  m*/
    joshuaBloch.setName("Joshua Bloch");

    final Author nealGafter = new Author();
    nealGafter.setId(2);
    nealGafter.setName("Neal Gafter");

    final Book javaPuzzlers = new Book();
    javaPuzzlers.setTitle("Java Puzzlers: Traps, Pitfalls, and Corner Cases");
    javaPuzzlers.setIsbn("032133678X");
    javaPuzzlers.setAuthors(new Author[] { joshuaBloch, nealGafter });

    final Book effectiveJava = new Book();
    effectiveJava.setTitle("Effective Java (2nd Edition)");
    effectiveJava.setIsbn("0321356683");
    effectiveJava.setAuthors(new Author[] { joshuaBloch });

    final Book[] books = new Book[] { javaPuzzlers, effectiveJava };

    final String json = gson.toJson(books);
    System.out.println(json);
}

From source file:com.javacreed.examples.gson.part3.Main.java

License:Apache License

public static void main(final String[] args) throws IOException {
    // Configure GSON
    final DataTypeAdapterFactory.Builder dtafBuilder = new DataTypeAdapterFactory.Builder();
    dtafBuilder.add(A.class, new ATypeAdapter());

    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(dtafBuilder.build());
    gsonBuilder.setPrettyPrinting();

    final Gson gson = gsonBuilder.create();

    final B b = new B();

    final String json = gson.toJson(b);
    System.out.println(json);/*  www .j a va 2  s.  co m*/
}

From source file:com.javacreed.examples.gson.part4.Main.java

License:Apache License

public static void main(final String[] args) throws IOException {

    final Class<?> type = new TypeToken<List<A>>() {
    }.getRawType();/*  ww w. j  av  a 2 s . c o m*/

    // Configure GSON
    final DataTypeAdapterFactory.Builder dtafBuilder = new DataTypeAdapterFactory.Builder();
    dtafBuilder.add(A.class, new ATypeAdapter());
    dtafBuilder.add(type, new ListATypeAdapter());

    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapterFactory(dtafBuilder.build());
    gsonBuilder.setPrettyPrinting();

    final Gson gson = gsonBuilder.create();

    final List<B> list = Arrays.asList(new B());

    final String json = gson.toJson(list, type);
    System.out.println(json);
}

From source file:com.julauncher.workers.InstanceInstaller.java

License:Open Source License

public InstanceInstaller(String instanceName, Pack pack, PackVersion version, boolean isReinstall,
        boolean isServer, String shareCode, boolean showModsChooser) {
    this.instanceName = instanceName;
    this.pack = pack;
    this.version = version;
    this.isReinstall = isReinstall;
    this.isServer = isServer;
    this.shareCode = shareCode;
    this.showModsChooser = showModsChooser;
    if (isServer) {
        serverLibraries = new ArrayList<File>();
    }//from  ww w . j  av a 2s . c o m
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapterFactory(new EnumTypeAdapterFactory());
    builder.registerTypeAdapter(Date.class, new DateTypeAdapter());
    builder.registerTypeAdapter(File.class, new FileTypeAdapter());
    builder.setPrettyPrinting();
    this.gson = builder.create();
}

From source file:com.pmeade.arya.Arya.java

License:Open Source License

/**
 * Obtain the Google Gson singleton. If the singleton does not exist,
 * it will be created on the first call to this method.
 * @return Gson object, properly configured for JSON (de)serialization
 *///from  w  w  w  .  ja  v a2  s.c  om
private Gson getGson() {
    // if we don't have a Gson singleton yet
    if (gson == null) {
        // create a builder object to generate a customized Gson object
        GsonBuilder gsonBuilder = new GsonBuilder();
        // register Arya's serializer to handle serialization of every
        // object that derives from Object (i.e.: all objects)
        gsonBuilder.registerTypeHierarchyAdapter(Object.class, new AryaSerializer(this));
        // register Arya's deserializer to handle deserialization of every
        // object that derives from Object (i.e.: all objects)
        gsonBuilder.registerTypeHierarchyAdapter(Object.class, new AryaDeserializer(this));
        // if Arya was configured for pretty printing
        if (prettyPrinting) {
            // then, configure our Gson singleton for pretty printing
            gsonBuilder.setPrettyPrinting();
        }
        // create the Gson singleton
        gson = gsonBuilder.create();
    }
    // return the Gson singleton to the caller
    return gson;
}

From source file:com.sktelecom.cep.notebook.repo.JDBCNotebookRepo.java

License:Apache License

private Note getNote(String json) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.create();//from  www.  ja  v  a2s  .c  o  m

    Note note = gson.fromJson(json, Note.class);
    for (Paragraph p : note.getParagraphs()) {
        if (p.getStatus() == Status.PENDING || p.getStatus() == Status.RUNNING) {
            p.setStatus(Status.ABORT);
        }
    }
    return note;
}

From source file:com.sktelecom.cep.notebook.repo.JDBCNotebookRepo.java

License:Apache License

@Override
public void save(Note note) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.create();/* w w w.  ja  v  a 2 s.  c om*/
    String json = gson.toJson(note);

    Note orgNote = get(note.id());
    if (orgNote == null) {
        if (note.getName() == null) {
            note.setName("Note " + note.id());
        }
        //share_type : ALL / ASSIGN / NONE
        //obj_status : CREATED, SHARED, DROPPED
        jdbcTemplate.update(
                "insert into workspace_object(wrkspc_obj_id, wrkspc_obj_type, share_type, obj_status, create_user_id, own_user_id) values (?, ?, ?, ?, ?, ?)",
                note.id(), "NOTEBOOK", "NONE", "CREATED", note.getUserId(), note.getUserId());
        jdbcTemplate.update(
                "insert into workspace_assign(wrkspc_id, wrkspc_obj_id, update_date, update_user_id) values (?, ?, NOW(), ?)",
                note.getWorkspaceId(), note.id(), note.getUserId());
        jdbcTemplate.update(
                "insert into notebook(note_id, note_name, note_content, update_date, update_user_id) values (?, ?, ?, NOW(), ?)",
                note.id(), note.getName(), json, note.getUserId());
    } else {
        jdbcTemplate.update(
                "update notebook set note_content = ?, note_name = ?, update_date = NOW(), update_user_id = ? where note_id = ?",
                json, note.getName(), note.getUserId(), note.id());
    }
}

From source file:com.softwaremagico.tm.json.CharacterJsonManager.java

License:Open Source License

public static String toJson(CharacterPlayer characterPlayer) {
    if (characterPlayer != null) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        gsonBuilder.setExclusionStrategies(new AnnotationExclusionStrategy()).create();
        gsonBuilder.registerTypeAdapter(IValue.class, new IValueSerializer<IValue>());
        gsonBuilder.registerTypeAdapter(Faction.class, new FactionAdapter());
        gsonBuilder.registerTypeAdapter(Blessing.class, new BlessingAdapter());
        gsonBuilder.registerTypeAdapter(AvailableBenefice.class, new AvailableBeneficeAdapter());
        gsonBuilder.registerTypeAdapter(AvailableSkill.class, new AvailableSkillAdapter());
        gsonBuilder.registerTypeAdapter(CharacteristicDefinition.class, new CharacteristicDefinitionAdapter());
        gsonBuilder.registerTypeAdapter(Race.class, new RaceAdapter());
        gsonBuilder.registerTypeAdapter(Weapon.class, new WeaponAdapter());
        gsonBuilder.registerTypeAdapter(Armour.class, new ArmourAdapter());
        gsonBuilder.registerTypeAdapter(Shield.class, new ShieldAdapter());
        gsonBuilder.registerTypeAdapter(CyberneticDevice.class, new CyberneticDeviceAdapter());
        // final Gson gson = new
        // GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create();
        Gson gson = gsonBuilder.create();
        String jsonText = gson.toJson(characterPlayer);
        return jsonText;
    }/*  w w w .ja va  2  s .  co m*/
    return null;
}

From source file:com.softwaremagico.tm.json.CharacterJsonManager.java

License:Open Source License

public static CharacterPlayer fromJson(String jsonText) {
    if (jsonText != null && jsonText.length() > 0) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        gsonBuilder.registerTypeAdapter(IValue.class, new InterfaceAdapter<IValue>());
        gsonBuilder.registerTypeAdapter(Faction.class, new FactionAdapter());
        gsonBuilder.registerTypeAdapter(Blessing.class, new BlessingAdapter());
        gsonBuilder.registerTypeAdapter(AvailableBenefice.class, new AvailableBeneficeAdapter());
        gsonBuilder.registerTypeAdapter(AvailableSkill.class, new AvailableSkillAdapter());
        gsonBuilder.registerTypeAdapter(CharacteristicDefinition.class, new CharacteristicDefinitionAdapter());
        gsonBuilder.registerTypeAdapter(Race.class, new RaceAdapter());
        gsonBuilder.registerTypeAdapter(Weapon.class, new WeaponAdapter());
        gsonBuilder.registerTypeAdapter(Armour.class, new ArmourAdapter());
        gsonBuilder.registerTypeAdapter(Shield.class, new ShieldAdapter());
        gsonBuilder.registerTypeAdapter(CyberneticDevice.class, new CyberneticDeviceAdapter());
        Gson gson = gsonBuilder.create();

        CharacterPlayer characterPlayer = gson.fromJson(jsonText, CharacterPlayer.class);
        return characterPlayer;
    }/*  w w w  .j ava  2 s . co m*/
    return null;
}