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:org.mobicents.servlet.sip.restcomm.http.TranscriptionsEndpoint.java

License:Open Source License

public TranscriptionsEndpoint() {
    super();//from   w w w  . jav  a  2s.c o m
    final ServiceLocator services = ServiceLocator.getInstance();
    dao = services.get(DaoManager.class).getTranscriptionsDao();
    final TranscriptionConverter converter = new TranscriptionConverter(configuration);
    final GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Transcription.class, converter);
    builder.setPrettyPrinting();
    gson = builder.create();
    xstream = new XStream();
    xstream.alias("RestcommResponse", RestCommResponse.class);
    xstream.registerConverter(converter);
    xstream.registerConverter(new TranscriptionListConverter(configuration));
    xstream.registerConverter(new RestCommResponseConverter(configuration));
}

From source file:org.mobicents.tools.http.balancer.HttpRequestHandler.java

License:Open Source License

private void writeStatisticResponse(MessageEvent e) {
    GsonBuilder builder = new GsonBuilder();
    Gson gson = builder.setPrettyPrinting().create();
    JsonElement je = gson.toJsonTree(new StatisticObject(balancerRunner));
    JsonObject jo = new JsonObject();
    jo.add("Metrics", je);
    String output = jo.toString();
    HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
    response.setHeader(HttpHeaders.Names.CONTENT_TYPE, APPLICATION_JSON);
    ChannelBuffer buf = ChannelBuffers.copiedBuffer(output, Charset.forName("UTF-8"));
    response.setContent(buf);/*  ww  w  .  ja va2s .  c o  m*/
    ChannelFuture future = e.getChannel().write(response);
    future.addListener(ChannelFutureListener.CLOSE);
}

From source file:org.moe.natjgen.Configuration.java

License:Apache License

private static GsonBuilder getJsonBuilder() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(Configuration.class, new Configuration.Serializer());
    builder.registerTypeAdapter(Configuration.class, new Configuration.Deserializer());
    builder.registerTypeAdapter(UnitActionManager.class, new UnitActionManager.Serializer());
    builder.registerTypeAdapter(UnitActionManager.class, new UnitActionManager.Deserializer());
    builder.registerTypeAdapter(UnitRule.class, new UnitRule.Serializer());
    builder.registerTypeAdapter(UnitRule.class, new UnitRule.Deserializer());
    builder.registerTypeAdapter(Condition.class, new Condition.Serializer());
    builder.registerTypeAdapter(Condition.class, new Condition.Deserializer());
    builder.registerTypeAdapter(Action.class, new Action.Serializer());
    builder.registerTypeAdapter(Action.class, new Action.Deserializer());
    builder.setPrettyPrinting();
    return builder;
}

From source file:org.moe.natjgen.TypeConfiguration.java

License:Apache License

private GsonBuilder getJsonBuilder() {
    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeAdapter(MatchScope.class, new MatchScope.Serializer());
    builder.registerTypeAdapter(MatchScope.class, new MatchScope.Deserializer());
    builder.registerTypeAdapter(MatchArgument.class, new MatchArgument.Serializer());
    builder.registerTypeAdapter(MatchArgument.class, new MatchArgument.Deserializer());
    builder.registerTypeAdapter(MatchItem.class, new MatchItem.Serializer());
    builder.registerTypeAdapter(MatchItem.class, new MatchItem.Deserializer());
    builder.setPrettyPrinting();
    return builder;
}

From source file:org.molgenis.util.GsonHttpMessageConverter.java

License:Apache License

/**
 * Construct a new {@code GsonHttpMessageConverter} with a default {@link Gson#Gson() Gson}.
 *///w  ww.j ava 2 s .c  o m
public GsonHttpMessageConverter(boolean prettyPrinting) {
    super(new MediaType("application", "json", DEFAULT_CHARSET));

    GsonBuilder builder = new GsonBuilder().setDateFormat(MolgenisDateFormat.DATEFORMAT_DATETIME)
            .disableHtmlEscaping();
    if (prettyPrinting) {
        builder = builder.setPrettyPrinting();
    }

    gson = builder.create();
}

From source file:org.mule.runtime.extension.api.persistence.ExtensionModelJsonSerializer.java

License:Open Source License

private GsonBuilder gsonBuilder(SerializationContext serializationContext, boolean prettyPrint) {
    Set<String> registeredTypeIds = registeredTypes.stream().map(MetadataTypeUtils::getTypeId)
            .filter(Optional::isPresent).map(Optional::get).collect(Collectors.toSet());

    final ObjectTypeReferenceHandler referenceHandler = new RestrictedTypesObjectTypeReferenceHandler(
            serializationContext, registeredTypeIds);

    final DefaultImplementationTypeAdapterFactory configurationModelTypeAdapterFactory = new DefaultImplementationTypeAdapterFactory<>(
            ConfigurationModel.class, ImmutableConfigurationModel.class);
    final DefaultImplementationTypeAdapterFactory connectionProviderModelTypeAdapterFactory = new DefaultImplementationTypeAdapterFactory<>(
            ConnectionProviderModel.class, ImmutableConnectionProviderModel.class);
    final DefaultImplementationTypeAdapterFactory sourceModelTypeAdapterFactory = new DefaultImplementationTypeAdapterFactory<>(
            SourceModel.class, ImmutableSourceModel.class);
    final DefaultImplementationTypeAdapterFactory sourceCallbackModelTypeAdapterFactory = new DefaultImplementationTypeAdapterFactory<>(
            SourceCallbackModel.class, ImmutableSourceCallbackModel.class);
    final DefaultImplementationTypeAdapterFactory parameterModelTypeAdapterFactory = new DefaultImplementationTypeAdapterFactory<>(
            ParameterModel.class, ImmutableParameterModel.class);
    final DefaultImplementationTypeAdapterFactory parameterGroupModelTypeAdapterFactory = new DefaultImplementationTypeAdapterFactory<>(
            ParameterGroupModel.class, ImmutableParameterGroupModel.class);
    final DefaultImplementationTypeAdapterFactory exclusiveParametersTypeAdapterFactory = new DefaultImplementationTypeAdapterFactory<>(
            ExclusiveParametersModel.class, ImmutableExclusiveParametersModel.class);
    final DefaultImplementationTypeAdapterFactory outputModelTypeAdapterFactory = new DefaultImplementationTypeAdapterFactory<>(
            OutputModel.class, ImmutableOutputModel.class);
    final MuleVersionTypeAdapter muleVersionTypeAdapter = new MuleVersionTypeAdapter();
    final DefaultImplementationTypeAdapterFactory<ErrorModel, ImmutableErrorModel> errorModelTypeAdapter = new DefaultImplementationTypeAdapterFactory<>(
            ErrorModel.class, ImmutableErrorModel.class);
    final DefaultImplementationTypeAdapterFactory<OAuthGrantType, AuthorizationCodeGrantType> oauthGrantTypeAdapter = new DefaultImplementationTypeAdapterFactory<>(
            OAuthGrantType.class, AuthorizationCodeGrantType.class);

    final GsonBuilder gsonBuilder = new GsonBuilder()
            .registerTypeAdapterFactory(new OptionalTypeAdapterFactory())
            .registerTypeAdapter(MetadataType.class, new MetadataTypeGsonTypeAdapter(referenceHandler))
            .registerTypeAdapter(MuleVersion.class, muleVersionTypeAdapter)
            .registerTypeAdapter(ImportedTypeModel.class, new ImportedTypesModelTypeAdapter(referenceHandler))
            .registerTypeAdapter(SubTypesModel.class, new SubTypesModelTypeAdapter(referenceHandler))
            .registerTypeAdapter(XmlDslModel.class, new XmlDslModelTypeAdapter())
            .registerTypeAdapter(ParameterDslConfiguration.class, new ElementDslModelTypeAdapter())
            .registerTypeAdapterFactory(new ModelPropertyMapTypeAdapterFactory())
            .registerTypeAdapterFactory(sourceModelTypeAdapterFactory)
            .registerTypeAdapterFactory(sourceCallbackModelTypeAdapterFactory)
            .registerTypeAdapterFactory(parameterModelTypeAdapterFactory)
            .registerTypeAdapterFactory(parameterGroupModelTypeAdapterFactory)
            .registerTypeAdapterFactory(exclusiveParametersTypeAdapterFactory)
            .registerTypeAdapterFactory(configurationModelTypeAdapterFactory)
            .registerTypeAdapterFactory(connectionProviderModelTypeAdapterFactory)
            .registerTypeAdapterFactory(new OperationModelTypeAdapterFactory())
            .registerTypeAdapterFactory(
                    new DefaultImplementationTypeAdapterFactory<>(RouteModel.class, ImmutableRouteModel.class))
            .registerTypeAdapterFactory(outputModelTypeAdapterFactory)
            .registerTypeAdapterFactory(errorModelTypeAdapter)
            .registerTypeAdapterFactory(oauthGrantTypeAdapter);

    if (prettyPrint) {
        gsonBuilder.setPrettyPrinting();
    }/*from  w  w w.j a  v a2  s  .  c  om*/
    return gsonBuilder;
}

From source file:org.mule.runtime.extension.api.persistence.metadata.AbstractMetadataResultJsonSerializer.java

License:Open Source License

AbstractMetadataResultJsonSerializer(boolean prettyPrint) {

    final DefaultImplementationTypeAdapterFactory operationModelTypeAdapterFactory = new DefaultImplementationTypeAdapterFactory<>(
            OperationModel.class, ImmutableOperationModel.class);
    final DefaultImplementationTypeAdapterFactory sourceModelTypeAdapterFactory = new DefaultImplementationTypeAdapterFactory<>(
            SourceModel.class, ImmutableSourceModel.class);
    final DefaultImplementationTypeAdapterFactory sourceCallbackModelTypeAdapterFactory = new DefaultImplementationTypeAdapterFactory<>(
            SourceCallbackModel.class, ImmutableSourceCallbackModel.class);
    final DefaultImplementationTypeAdapterFactory parameterModelTypeAdapterFactory = new DefaultImplementationTypeAdapterFactory<>(
            ParameterModel.class, ImmutableParameterModel.class);
    final DefaultImplementationTypeAdapterFactory parameterGroupModelTypeAdapterFactory = new DefaultImplementationTypeAdapterFactory<>(
            ParameterGroupModel.class, ImmutableParameterGroupModel.class);
    final DefaultImplementationTypeAdapterFactory exclusiveParametersTypeAdapterFactory = new DefaultImplementationTypeAdapterFactory<>(
            ExclusiveParametersModel.class, ImmutableExclusiveParametersModel.class);
    final DefaultImplementationTypeAdapterFactory outputModelTypeAdapterFactory = new DefaultImplementationTypeAdapterFactory<>(
            OutputModel.class, ImmutableOutputModel.class);
    final DefaultImplementationTypeAdapterFactory<ErrorModel, ImmutableErrorModel> errorModelTypeAdapter = new DefaultImplementationTypeAdapterFactory<>(
            ErrorModel.class, ImmutableErrorModel.class);

    final GsonBuilder gsonBuilder = new GsonBuilder()
            .registerTypeAdapterFactory(new FailureCodeTypeAdapterFactory())
            .registerTypeAdapter(MetadataType.class, new MetadataTypeGsonTypeAdapter())
            .registerTypeAdapterFactory(new OptionalTypeAdapterFactory())
            .registerTypeAdapterFactory(new ModelPropertyMapTypeAdapterFactory())
            .registerTypeAdapterFactory(new ComponentResultTypeAdapterFactory())
            .registerTypeAdapter(MetadataKey.class, new MetadataKeyTypeAdapter())
            .registerTypeAdapterFactory(sourceModelTypeAdapterFactory)
            .registerTypeAdapterFactory(sourceCallbackModelTypeAdapterFactory)
            .registerTypeAdapterFactory(parameterModelTypeAdapterFactory)
            .registerTypeAdapterFactory(parameterGroupModelTypeAdapterFactory)
            .registerTypeAdapterFactory(exclusiveParametersTypeAdapterFactory)
            .registerTypeAdapterFactory(operationModelTypeAdapterFactory)
            .registerTypeAdapterFactory(outputModelTypeAdapterFactory)
            .registerTypeAdapterFactory(errorModelTypeAdapter);

    if (prettyPrint) {
        gsonBuilder.setPrettyPrinting();
    }/*from   www .  j a va2 s.c o m*/

    this.gson = gsonBuilder.create();
}

From source file:org.netxms.websvc.json.JsonTools.java

License:Open Source License

/**
 * Create JSON representation for given object
 * /* w  w w  .j  a v  a  2s.c o  m*/
 * @param object object to serialize
 * @return JSON code
 */
public static String jsonFromObject(Object object) {
    if (object instanceof JsonObject)
        return ((JsonObject) object).toString();
    if (object instanceof ResponseContainer)
        return ((ResponseContainer) object).toJson();

    GsonBuilder builder = new GsonBuilder();
    builder.setPrettyPrinting(); // FIXME: remove for production
    builder.registerTypeAdapter(Date.class, new DateAdapter());
    builder.registerTypeAdapter(InetAddress.class, new InetAddressAdapter());
    builder.registerTypeAdapter(InetAddressEx.class, new InetAddressExAdapter());
    builder.registerTypeAdapter(MacAddress.class, new MacAddressAdapter());
    builder.setExclusionStrategies(new ExclusionStrategy() {
        @Override
        public boolean shouldSkipField(FieldAttributes f) {
            return f.getAnnotation(Internal.class) != null;
        }

        @Override
        public boolean shouldSkipClass(Class<?> c) {
            return c.isAnnotationPresent(Internal.class);
        }
    });
    return builder.create().toJson(object);
}

From source file:org.objectpocket.ObjectPocketImpl.java

License:Apache License

private Gson configureGson() {
    if (gson == null) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        // null serialization
        if (serializeNulls) {
            gsonBuilder.serializeNulls();
        }/*from   w  w w .jav  a  2s .  c  om*/

        // This is where the referencing entry magic happens
        gsonBuilder.registerTypeAdapterFactory(new CustomTypeAdapterFactory(this));

        // add custom type adapters
        for (Type type : typeAdapterMap.keySet()) {
            for (Object typeAdapter : typeAdapterMap.get(type)) {
                gsonBuilder.registerTypeAdapter(type, typeAdapter);
            }
        }
        // pretty printing
        if (prettyPrinting) {
            gsonBuilder.setPrettyPrinting();
        }
        gson = gsonBuilder.create();
    }
    return gson;
}

From source file:org.onebusaway.admin.json.LowerCaseWDashesGsonJsonTool.java

License:Apache License

private Gson buildGsonObject() {
    GsonBuilder gbuilder = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_DASHES);

    setTypeAdapters(gbuilder);// w  ww  .  ja  v a  2 s  .  co m

    if (prettyPrintOutput)
        gbuilder.setPrettyPrinting();

    Gson gson = gbuilder.create();

    return gson;
}