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:es.chatclient.logic.Controller.java

private Controller(ClientGUIController guiController) {
    this.userNick = null;
    this.guiController = guiController;
    this.executorService = Executors.newFixedThreadPool(10);
    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(NetworkMessage.class, new RequestMessageTypeAdapter());
    gsonBuilder.registerTypeAdapter(ConversDataMessage.class, new ConversDataMessageTypeAdapter());
    gsonBuilder.registerTypeAdapter(Message.class, new ServerMessageTypeAdapter());
    gsonBuilder.setPrettyPrinting();

    Message m = new Message("String1", "String2", "String3", "String4", "String5", "String6");

    this.gson = gsonBuilder.create();

    System.err.println("TO JSON MESSAGE");
    String json = gson.toJson(m);
    System.out.println(json);/*from   w w w  .j  a v a 2  s  . co  m*/

    System.err.println("FROM JSON MESSAGE");
    Message m2 = gson.fromJson(json, Message.class);

    System.out.println(m2.getClientId());
    System.out.println(m2.getConverId());
    System.out.println(m2.getMsgText());
    System.out.println(m2.getUserNick());

}

From source file:es.chatserver.logic.Controller.java

private Controller() {

    this.perController = PersistenceController.getInstance();

    //        Conver c = new Conver("Conver prueba otra");
    //        Client client = new Client("cliente", "pass");
    //        // w  ww.j a v a  2  s .  co  m
    //        this.addConver(c, "adrinfer");

    final GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(RequestMessage.class, new RequestMessageTypeAdapter());
    gsonBuilder.registerTypeAdapter(ConversDataMessage.class, new ConversDataMessageTypeAdapter());
    gsonBuilder.registerTypeAdapter(es.chatserver.server.messages.Message.class,
            new ServerMessageTypeAdapter());
    gsonBuilder.setPrettyPrinting();

    this.gson = gsonBuilder.create();

}

From source file:eu.vranckaert.worktime.web.json.model.JsonEntity.java

License:Apache License

public String toJSON() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Date.class, new DateTimeSerializer());
    builder.excludeFieldsWithoutExposeAnnotation();
    builder.setPrettyPrinting();
    Gson gson = builder.create();/*ww  w .  j a v  a  2  s . c  om*/

    String json = gson.toJson(this);
    return json;
}

From source file:fi.craplab.roameo.data.JsonExporter.java

License:Open Source License

private Gson createGson() {
    GsonBuilder gsonBuilder = new GsonBuilder();

    gsonBuilder.setPrettyPrinting();
    gsonBuilder.excludeFieldsWithoutExposeAnnotation();

    if (!mExportPhoneNumbers) {
        gsonBuilder.setExclusionStrategies(new PhoneNumberExclusionStrategy());
    }/*from ww  w .  j  a  va2  s  . c o  m*/

    return gsonBuilder.create();
}

From source file:fr.bmartel.bboxapi.examples.request.CallLog.java

License:Open Source License

public static void main(String[] args) {

    BboxApi api = new BboxApi();

    String pass = ExampleUtils.getPassword();

    api.setPassword(pass);/*from w w  w .  jav  a2s.c  om*/

    CallLogResponse callLogResponse = api.getFullCallLog();

    if (callLogResponse.getStatus() == HttpStatus.OK) {

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.setPrettyPrinting().create();
        Type listOfTestObject = new TypeToken<List<CallLogList>>() {
        }.getType();
        String callLlog = gson.toJson(callLogResponse.getCallLogList(), listOfTestObject);

        LOGGER.debug(callLlog);

    } else {
        LOGGER.error("http error  : " + callLogResponse.getStatus());
    }
}

From source file:fr.bmartel.bboxapi.examples.request.Consumption.java

License:Open Source License

public static void main(String[] args) {

    BboxApi api = new BboxApi();

    String pass = ExampleUtils.getPassword();

    api.setPassword(pass);//from   w  w  w.  j  a v  a 2 s  . c  o m

    ConsumptionResponse consumptionResponse = api.getConsumptionData();

    if (consumptionResponse.getStatus() == HttpStatus.OK) {

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.setPrettyPrinting().create();
        Type listOfTestObject = new TypeToken<List<ProfileEntry>>() {
        }.getType();
        String consumptionList = gson.toJson(consumptionResponse.getProfileList(), listOfTestObject);

        LOGGER.debug(consumptionList);

        LOGGER.debug("session valid : " + consumptionResponse.isValidSession());
    } else {
        LOGGER.error("http error  : " + consumptionResponse.getStatus());
    }
}

From source file:fr.bmartel.bboxapi.examples.request.DeviceInfo.java

License:Open Source License

public static void main(String[] args) {

    BboxApi api = new BboxApi();

    DeviceInfoResponse deviceInfoResponse = api.getDeviceInfo(false);

    if (deviceInfoResponse.getStatus() == HttpStatus.OK) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.setPrettyPrinting().create();
        Type listOfTestObject = new TypeToken<List<BboxDeviceEntry>>() {
        }.getType();//from   w w w  . j  a  v  a  2 s . c  o  m
        String deviceList = gson.toJson(deviceInfoResponse.getDeviceList(), listOfTestObject);

        LOGGER.debug(deviceList);

    } else {
        LOGGER.error("http error  : " + deviceInfoResponse.getStatus());
    }
}

From source file:fr.bmartel.bboxapi.examples.request.Hosts.java

License:Open Source License

public static void main(String[] args) {

    BboxApi api = new BboxApi();

    HostsResponse hostResponse = api.getHosts();

    if (hostResponse.getStatus() == HttpStatus.OK) {

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.setPrettyPrinting().create();
        Type listOfTestObject = new TypeToken<List<HostItem>>() {
        }.getType();//from w  w w  .  j  av a2 s .c  om
        String hostList = gson.toJson(hostResponse.getHostList(), listOfTestObject);

        LOGGER.debug(hostList);
    } else {
        LOGGER.error("http error  : " + hostResponse.getStatus());
    }
}

From source file:fr.bmartel.bboxapi.examples.request.Summary.java

License:Open Source License

public static void main(String[] args) {

    BboxApi api = new BboxApi();

    SummaryResponse summaryResponse = api.getDeviceSummary(false);

    if (summaryResponse.getStatus() == HttpStatus.OK) {

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder.setPrettyPrinting().create();
        Type listOfTestObject = new TypeToken<List<ApiSummary>>() {
        }.getType();/*from  www .  j av a 2 s  .  c o m*/
        String summary = gson.toJson(summaryResponse.getSummary(), listOfTestObject);

        LOGGER.debug(summary);
    } else {
        LOGGER.error("http error  : " + summaryResponse.getStatus());
    }
}

From source file:fr.bmartel.bboxapi.examples.request.VoiceMail.java

License:Open Source License

public static void main(String[] args) {

    BboxApi api = new BboxApi();

    String pass = ExampleUtils.getPassword();

    api.setPassword(pass);/*  www.j  ava2  s. com*/

    VoiceMailResponse voiceMailResponse = api.getVoiceMailData();

    if (voiceMailResponse.getStatus() == HttpStatus.OK) {

        GsonBuilder gsonBuilder = new GsonBuilder().disableHtmlEscaping();
        Gson gson = gsonBuilder.setPrettyPrinting().create();
        Type listOfTestObject = new TypeToken<List<VoiceMailEntry>>() {
        }.getType();
        String voiceMailList = gson.toJson(voiceMailResponse.getVoiceMailList(), listOfTestObject);

        LOGGER.debug(voiceMailList);
    } else {
        LOGGER.error("http error  : " + voiceMailResponse.getStatus());
    }
}