List of usage examples for com.google.gson GsonBuilder setPrettyPrinting
public GsonBuilder setPrettyPrinting()
From source file:com.google.nigori.common.MessageLibrary.java
License:Apache License
private static Gson initializeGson() { GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(GetRequest.class, new TypeAdapterProtobuf()); gsonBuilder.registerTypeAdapter(GetResponse.class, new TypeAdapterProtobuf()); gsonBuilder.registerTypeAdapter(GetIndicesRequest.class, new TypeAdapterProtobuf()); gsonBuilder.registerTypeAdapter(GetIndicesResponse.class, new TypeAdapterProtobuf()); gsonBuilder.registerTypeAdapter(GetRevisionsRequest.class, new TypeAdapterProtobuf()); gsonBuilder.registerTypeAdapter(GetRevisionsResponse.class, new TypeAdapterProtobuf()); gsonBuilder.registerTypeAdapter(PutRequest.class, new TypeAdapterProtobuf()); gsonBuilder.registerTypeAdapter(DeleteRequest.class, new TypeAdapterProtobuf()); gsonBuilder.registerTypeAdapter(RegisterRequest.class, new TypeAdapterProtobuf()); gsonBuilder.registerTypeAdapter(UnregisterRequest.class, new TypeAdapterProtobuf()); gsonBuilder.registerTypeAdapter(AuthenticateRequest.class, new TypeAdapterProtobuf()); gsonBuilder.registerTypeAdapter(RevisionValue.class, new TypeAdapterProtobuf()); gsonBuilder.registerTypeAdapter(ByteString.class, new TypeAdapterByteString()); gsonBuilder.setPrettyPrinting(); return gsonBuilder.create(); }
From source file:com.gst.infrastructure.core.serialization.ExcludeNothingWithPrettyPrintingOnJsonSerializerGoogleGson.java
License:Apache License
public ExcludeNothingWithPrettyPrintingOnJsonSerializerGoogleGson() { final GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter()); builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter()); builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter()); builder.setPrettyPrinting(); this.gson = builder.create(); }
From source file:com.gst.infrastructure.core.serialization.GoogleGsonSerializerHelper.java
License:Apache License
public Gson createGsonBuilder(final boolean prettyPrint) { final GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter()); builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter()); builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter()); if (prettyPrint) { builder.setPrettyPrinting(); }//from www . ja v a2 s.c o m return builder.create(); }
From source file:com.gst.infrastructure.core.serialization.GoogleGsonSerializerHelper.java
License:Apache License
public Gson createGsonBuilderForPartialResponseFiltering(final boolean prettyPrint, final Set<String> responseParameters) { final ExclusionStrategy strategy = new ParameterListInclusionStrategy(responseParameters); final GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy); builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter()); builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter()); builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter()); if (prettyPrint) { builder.setPrettyPrinting(); }//from ww w . ja va 2 s. c om return builder.create(); }
From source file:com.gst.infrastructure.core.serialization.GoogleGsonSerializerHelper.java
License:Apache License
public Gson createGsonBuilderWithParameterExclusionSerializationStrategy(final Set<String> supportedParameters, final boolean prettyPrint, final Set<String> responseParameters) { final Set<String> parameterNamesToSkip = new HashSet<>(); if (!responseParameters.isEmpty()) { // strip out all known support parameters from expected response to // see if unsupported parameters requested for response. final Set<String> differentParametersDetectedSet = new HashSet<>(responseParameters); differentParametersDetectedSet.removeAll(supportedParameters); if (!differentParametersDetectedSet.isEmpty()) { throw new UnsupportedParameterException(new ArrayList<>(differentParametersDetectedSet)); }/*from ww w . j av a 2s . co m*/ parameterNamesToSkip.addAll(supportedParameters); parameterNamesToSkip.removeAll(responseParameters); } final ExclusionStrategy strategy = new ParameterListExclusionStrategy(parameterNamesToSkip); final GsonBuilder builder = new GsonBuilder().addSerializationExclusionStrategy(strategy); builder.registerTypeAdapter(LocalDate.class, new JodaLocalDateAdapter()); builder.registerTypeAdapter(DateTime.class, new JodaDateTimeAdapter()); builder.registerTypeAdapter(MonthDay.class, new JodaMonthDayAdapter()); if (prettyPrint) { builder.setPrettyPrinting(); } return builder.create(); }
From source file:com.haulmont.cuba.core.app.serialization.EntitySerialization.java
License:Apache License
protected Gson createGsonForSerialization(@Nullable View view, EntitySerializationOption... options) { GsonBuilder gsonBuilder = new GsonBuilder(); if (ArrayUtils.contains(options, EntitySerializationOption.PRETTY_PRINT)) { gsonBuilder.setPrettyPrinting(); }/*from w w w . j av a 2 s. c om*/ gsonBuilder.registerTypeHierarchyAdapter(Entity.class, new EntitySerializer(view, options)) .registerTypeHierarchyAdapter(Date.class, new DateSerializer()).create(); if (ArrayUtils.contains(options, EntitySerializationOption.SERIALIZE_NULLS)) { gsonBuilder.serializeNulls(); } return gsonBuilder.create(); }
From source file:com.ibm.common.activitystreams.internal.GsonWrapper.java
License:Apache License
/** * Constructor for GsonWrapper./*from ww w . ja va 2 s.com*/ * @param builder Builder */ protected GsonWrapper(Builder builder) { Schema schema = builder.schema != null ? builder.schema : Schema.make().get(); ASObjectAdapter base = new ASObjectAdapter(schema); GsonBuilder b = initGsonBuilder(builder, schema, base, builder.adapters.build()); if (builder.pretty) b.setPrettyPrinting(); this.gson = b.create(); this.charset = builder.charset; }
From source file:com.ibm.watson.developer_cloud.util.GsonSingleton.java
License:Open Source License
/** * Creates a {@link com.google.gson.Gson} object that can be use to serialize and deserialize Java objects. * * @param prettyPrint if true the JSON will be pretty printed * @return the {@link Gson}// www .ja v a 2s . c o m */ private static Gson createGson(Boolean prettyPrint) { GsonBuilder builder = new GsonBuilder(); registerTypeAdapters(builder); if (prettyPrint) { builder.setPrettyPrinting(); } builder.disableHtmlEscaping(); return builder.create(); }
From source file:com.javacreed.examples.gson.part1.Main.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 BookTypeAdapter()); gsonBuilder.setPrettyPrinting(); final Gson gson = gsonBuilder.create(); final Book book = new Book(); book.setAuthors(new Author[] { new Author(1, "Joshua Bloch") }); book.setTitle("Effective Java"); book.setIsbn("978-0321356680"); final String json = gson.toJson(book); System.out.println(json);/*from ww w . j a v a 2s . c o m*/ }
From source file:com.javacreed.examples.gson.part2.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(Book.class, new BookTypeAdapter()); final GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapterFactory(dtafBuilder.build()); gsonBuilder.setPrettyPrinting(); final Gson gson = gsonBuilder.create(); final Book book = new Book(); book.setAuthors(new Author[] { new Author(1, "Joshua Bloch") }); book.setTitle("Effective Java"); book.setIsbn("978-0321356680"); final String json = gson.toJson(book); System.out.println(json);//from www .jav a 2s. c o m }