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.sensorhub.impl.module.ModuleConfigJsonFile.java

License:Mozilla Public License

public ModuleConfigJsonFile(String moduleConfigPath) {
    configFile = new File(moduleConfigPath);
    configMap = new LinkedHashMap<String, ModuleConfig>();

    // init json serializer/deserializer
    final GsonBuilder builder = new GsonBuilder();
    builder.setPrettyPrinting();
    builder.disableHtmlEscaping();//from  ww  w .  j  a  v a 2 s .  c om
    builder.registerTypeAdapterFactory(new RuntimeTypeAdapterFactory<Object>(Object.class, OBJ_CLASS_FIELD));

    gson = builder.create();
    readJSON();
}

From source file:org.sonatype.nexus.extdirect.internal.ExtDirectGsonBuilderConfigurator.java

License:Open Source License

@Override
public void configure(final GsonBuilder builder, final GlobalConfiguration configuration) {
    if (configuration.getDebug()) {
        builder.setPrettyPrinting();
    }//from  w  w  w  . j a  v  a 2 s.c  o  m
    builder.serializeNulls();
    builder.disableHtmlEscaping();

    builder.setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
}

From source file:org.springframework.http.converter.json.GsonFactoryBean.java

License:Apache License

@Override
public void afterPropertiesSet() {
    GsonBuilder builder = (this.base64EncodeByteArrays
            ? GsonBuilderUtils.gsonBuilderWithBase64EncodedByteArrays()
            : new GsonBuilder());
    if (this.serializeNulls) {
        builder.serializeNulls();//from  w ww.  j a  va  2s .com
    }
    if (this.prettyPrinting) {
        builder.setPrettyPrinting();
    }
    if (this.disableHtmlEscaping) {
        builder.disableHtmlEscaping();
    }
    if (this.dateFormatPattern != null) {
        builder.setDateFormat(this.dateFormatPattern);
    }
    this.gson = builder.create();
}

From source file:org.sweble.wikitext.example.Serializer.java

License:Apache License

private Gson getJsonSerializer() {
    final AstNodeJsonTypeAdapter<WtNode> nodeConverter = AstNodeJsonTypeAdapter.forNodeType(WtNode.class);
    EngineAstNodeConverter.setup(new WikiConfigImpl(), nodeConverter);

    // As long as GSON does not handle Object collections and polymorphism 
    // correctly, the "warnings" attribute cannot be serialized
    nodeConverter.suppressProperty("warnings");
    nodeConverter.setNodeFactory(new NodeFactory<WtNode>() {
        NodeFactory<WtNode> nf = nodeConverter.getNodeFactory();

        @Override/* ww  w.ja  v  a 2 s . com*/
        public WtNode instantiateNode(Class<?> clazz) {
            return nf.instantiateNode(clazz);
        }

        @Override
        public WtNode instantiateDefaultChild(NamedMemberId id, Class<?> childType) {
            return nf.instantiateDefaultChild(id, childType);
        }

        @Override
        public Object instantiateDefaultProperty(NamedMemberId id, Class<?> type) {
            if (id.memberName == "warnings")
                return Collections.EMPTY_LIST;
            return nf.instantiateDefaultProperty(id, type);
        }
    });

    AstRtDataJsonTypeAdapter<WtRtData> rtdConverter = new AstRtDataJsonTypeAdapter<WtRtData>(WtRtData.class);
    EngineAstNodeConverter.setup(rtdConverter);

    GsonBuilder builder = new GsonBuilder();
    builder.registerTypeHierarchyAdapter(RtData.class, rtdConverter);
    builder.registerTypeHierarchyAdapter(AstNode.class, nodeConverter);
    builder.serializeNulls();
    builder.setPrettyPrinting();
    return builder.create();
}

From source file:org.syphr.lametrictime.api.common.impl.GsonGenerator.java

License:Apache License

public static Gson create(boolean prettyPrint) {
    GsonBuilder builder = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
            .registerTypeAdapterFactory(new ApplicationTypeAdapterFactory())
            .registerTypeAdapterFactory(new ActionTypeAdapterFactory())
            .registerTypeAdapterFactory(new UpdateActionTypeAdapterFactory())
            .registerTypeAdapterFactory(RuntimeTypeAdapterFactory.of(Parameter.class, "data_type")
                    .registerSubtype(BooleanParameter.class, "bool")
                    .registerSubtype(StringParameter.class, "string")
                    .registerSubtype(IntegerParameter.class, "int"));

    // add Java 8 Time API support
    JSR310TypeAdapters.registerJSR310TypeAdapters(builder);

    if (prettyPrint) {
        builder.setPrettyPrinting();
    }//from   w w w.  j  a  va2s.co  m

    return builder.create();
}

From source file:org.syphr.sleepiq.api.impl.GsonGenerator.java

License:Apache License

public static Gson create(boolean prettyPrint) {
    GsonBuilder builder = new GsonBuilder();

    // add Java 8 Time API support
    JSR310TypeAdapters.registerJSR310TypeAdapters(builder);

    builder.registerTypeAdapter(TimeSince.class, new TimeSinceTypeAdapter());

    if (prettyPrint) {
        builder.setPrettyPrinting();
    }//from ww w .  j  a v a 2  s . c  o  m

    return builder.create();
}

From source file:org.thingsplode.synapse.serializers.gson.GsonSerializer.java

License:Apache License

public GsonSerializer(boolean prettyPrint, HashMap<Type, Object> typeaAdapters,
        List<ExclusionStrategy> exlusionStrategies) {
    GsonBuilder b = new GsonBuilder().registerTypeAdapter(Class.class, new ClassTypeAdapter())
            .registerTypeAdapter(HttpResponseStatus.class, new HttpResponseStatusAdapter());
    if (typeaAdapters != null && !typeaAdapters.isEmpty()) {
        typeaAdapters.forEach((k, v) -> {
            b.registerTypeAdapter(k, v);
        });//  ww  w  . java  2 s.c o m
    }
    if (exlusionStrategies != null && !exlusionStrategies.isEmpty()) {
        b.setExclusionStrategies((ExclusionStrategy[]) exlusionStrategies.toArray());
    }
    if (prettyPrint) {
        b.setPrettyPrinting();
    }
    this.gson = b.create();
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.DegreeWorksCourseRequests.java

License:Apache License

protected Gson getGson(OnlineSectioningHelper helper) {
    GsonBuilder builder = new GsonBuilder();
    if (helper.isDebugEnabled())
        builder.setPrettyPrinting();
    return builder.create();
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.PurdueCourseRequestsValidationProvider.java

License:Apache License

protected Gson getGson(OnlineSectioningHelper helper) {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override//w ww  . j ava 2  s.  c  om
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    }).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
        @Override
        public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(src));
        }
    }).registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
        @Override
        public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            try {
                return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                        .parse(json.getAsJsonPrimitive().getAsString());
            } catch (ParseException e) {
                throw new JsonParseException(e.getMessage(), e);
            }
        }
    });
    if (helper.isDebugEnabled())
        builder.setPrettyPrinting();
    return builder.create();
}

From source file:org.unitime.timetable.onlinesectioning.custom.purdue.XEStudentEnrollment.java

License:Open Source License

protected Gson getGson(OnlineSectioningHelper helper) {
    GsonBuilder builder = new GsonBuilder().registerTypeAdapter(DateTime.class, new JsonSerializer<DateTime>() {
        @Override//from ww w  .j a va 2s  .c  om
        public JsonElement serialize(DateTime src, Type typeOfSrc, JsonSerializationContext context) {
            return new JsonPrimitive(src.toString("yyyy-MM-dd'T'HH:mm:ss'Z'"));
        }
    }).registerTypeAdapter(DateTime.class, new JsonDeserializer<DateTime>() {
        @Override
        public DateTime deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                throws JsonParseException {
            return new DateTime(json.getAsJsonPrimitive().getAsString(), DateTimeZone.UTC);
        }
    });
    if (helper.isDebugEnabled())
        builder.setPrettyPrinting();
    return builder.create();
}