Java Code Examples for com.google.gson.GsonBuilder#setPrettyPrinting()

The following examples show how to use com.google.gson.GsonBuilder#setPrettyPrinting() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GSONFactory.java    From prayer-times-android with Apache License 2.0 6 votes vote down vote up
public static Gson build() {
    GsonBuilder b = new GsonBuilder();
    if (BuildConfig.DEBUG) {
        b.setPrettyPrinting();
    }

    RuntimeTypeAdapterFactory<Times> subTypeFactory = RuntimeTypeAdapterFactory
            .of(Times.class, "source");
    for (Source source : Source.values()) {
        subTypeFactory = subTypeFactory.registerSubtype(source.clz, source.name());
    }


    BooleanSerializer booleanSerializer = new BooleanSerializer();
    b.registerTypeAdapterFactory(subTypeFactory);
    b.registerTypeAdapter(Boolean.class, booleanSerializer);
    b.registerTypeAdapter(boolean.class, booleanSerializer);
    b.registerTypeAdapter(TimeZone.class, new TimezoneSerializer());
    b.registerTypeAdapter(Sound.class, new SoundSerializer());

    return b.create();
}
 
Example 2
Source File: GsonWrapper.java    From activitystreams with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor for GsonWrapper.
 * @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;
}
 
Example 3
Source File: Note.java    From Explorer with Apache License 2.0 6 votes vote down vote up
public void exportToFile(String path, String filename) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.create();

    File dir = new File(path);
    if (!dir.exists()) {
        dir.mkdirs();
    } else if (dir.isFile()) {
        throw new RuntimeException("File already exists" + dir.toString());
    }

    File file = new File(dir.getPath() + "/" + filename + ".json");
    logger.info("Persist note {} into {}", filename, file.getAbsolutePath());

    String json = gson.toJson(this);
    FileOutputStream out = new FileOutputStream(file);
    out.write(json.getBytes(conf.getString(ConfVars.EXPLORER_ENCODING)));
    out.close();
}
 
Example 4
Source File: GSONFactory.java    From prayer-times-android with Apache License 2.0 6 votes vote down vote up
public static Gson build() {
    GsonBuilder b = new GsonBuilder();
    if (BuildConfig.DEBUG) {
        b.setPrettyPrinting();
    }

    RuntimeTypeAdapterFactory<Times> subTypeFactory = RuntimeTypeAdapterFactory
            .of(Times.class, "source");
    for (Source source : Source.values()) {
        subTypeFactory = subTypeFactory.registerSubtype(source.clz, source.name());
    }


    BooleanSerializer booleanSerializer = new BooleanSerializer();
    b.registerTypeAdapterFactory(subTypeFactory);
    b.registerTypeAdapter(Boolean.class, booleanSerializer);
    b.registerTypeAdapter(boolean.class, booleanSerializer);
    b.registerTypeAdapter(TimeZone.class, new TimezoneSerializer());
    b.registerTypeAdapter(Sound.class, new SoundSerializer());

    return b.create();
}
 
Example 5
Source File: RdapActionBase.java    From nomulus with Apache License 2.0 6 votes vote down vote up
void setPayload(ReplyPayloadBase replyObject) {
  if (requestMethod == Action.Method.HEAD) {
    return;
  }

  GsonBuilder gsonBuilder = new GsonBuilder();
  gsonBuilder.disableHtmlEscaping();
  if (formatOutputParam.orElse(false)) {
    gsonBuilder.setPrettyPrinting();
  }
  Gson gson = gsonBuilder.create();

  TopLevelReplyObject topLevelObject =
      TopLevelReplyObject.create(replyObject, rdapJsonFormatter.createTosNotice());

  response.setPayload(gson.toJson(topLevelObject.toJson()));
}
 
Example 6
Source File: GsonFactoryBean.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() {
  GsonBuilder builder = new GsonBuilder();
  if (this.serializeNulls) {
    builder.serializeNulls();
  }
  if (this.prettyPrinting) {
    builder.setPrettyPrinting();
  }
  if (this.disableHtmlEscaping) {
    builder.disableHtmlEscaping();
  }
  if (this.dateFormatPattern != null) {
    builder.setDateFormat(this.dateFormatPattern);
  }
  if (this.typeAdapterFactoryList != null) {
    typeAdapterFactoryList.forEach(builder::registerTypeAdapterFactory);
  }
  if (this.typeAdapterHierarchyFactoryMap != null) {
    typeAdapterHierarchyFactoryMap.forEach(builder::registerTypeHierarchyAdapter);
  }
  if (this.registerJavaTimeConverters) {
    Converters.registerInstant(builder);
    Converters.registerLocalDate(builder);
  }
  this.gson = builder.create();
}
 
Example 7
Source File: MCRSimpleModelJSONConverter.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public static String toJSON(MCRMetsSimpleModel model) {
    GsonBuilder gsonBuilder = new GsonBuilder();

    gsonBuilder.registerTypeAdapter(MCRMetsLink.class, new MCRMetsLinkTypeAdapter());
    gsonBuilder.registerTypeAdapter(MCRMetsAltoLink.class, new MCRAltoLinkTypeAdapter());
    gsonBuilder.setPrettyPrinting();

    return gsonBuilder.create().toJson(model);
}
 
Example 8
Source File: ServerResource.java    From open-rmbt with Apache License 2.0 5 votes vote down vote up
public static Gson getGson(boolean prettyPrint)
{
    GsonBuilder gb = new GsonBuilder()
            .registerTypeAdapter(DateTime.class, new MyDateTimeAdapter());
    if (prettyPrint)
        gb = gb.setPrettyPrinting();
    return gb.create();
}
 
Example 9
Source File: ChannelInstanceModelProvider.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private static Gson createGson ()
{
    final GsonBuilder builder = new GsonBuilder ();
    builder.setPrettyPrinting ();
    builder.setDateFormat ( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );
    return builder.create ();
}
 
Example 10
Source File: ParameterBuilder.java    From solidity-ide with Eclipse Public License 1.0 5 votes vote down vote up
public ParameterBuilder(String language) {
	gsonBuilder = new GsonBuilder();
	gsonBuilder.setPrettyPrinting();
	this.parameter = new Parameter();
	this.parameter.setLanguage(language);
	this.parameter.setSettings(defaultSettings());
}
 
Example 11
Source File: JsonDataStorage.java    From ClaimChunk with MIT License 5 votes vote down vote up
private Gson getGson() {
    GsonBuilder builder = new GsonBuilder();
    if (pretty) builder.setPrettyPrinting();
    return builder
            .serializeNulls()
            .create();
}
 
Example 12
Source File: JsonResponse.java    From zeppelin with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
  GsonBuilder gsonBuilder = new GsonBuilder();
  if (pretty) {
    gsonBuilder.setPrettyPrinting();
  }
  gsonBuilder.setExclusionStrategies(new JsonExclusionStrategy());
  Gson gson = gsonBuilder.create();
  return gson.toJson(this);
}
 
Example 13
Source File: MainView.java    From HiJson with Apache License 2.0 5 votes vote down vote up
/**
 * 复制节点内容.
 * @param path 节点路径
 * @param isFormat 是否带格式
 * @return 
 */
private String copyNodeContent(String path,boolean isFormat){
    String str = "";
    String arr[] = StringUtils.split(path, String.valueOf(dot));
    System.out.println("Get HashCode : " + tree.hashCode() + " . TabTitle : " + getTabTitle());
    JsonElement obj = (JsonElement)jsonEleTreeMap.get(tree.hashCode());
    if(arr.length>1){
        for(int i =1; i< arr.length; i++){
            int index = Kit.getIndex(arr[i]);
            String key = Kit.getKey(arr[i]);
            if(obj.isJsonPrimitive())break;
            if(index==-1){
                obj = obj.getAsJsonObject().get(key);
            }else{
                obj = obj.getAsJsonObject().getAsJsonArray(key).get(index);
            }
        }
    }
    if(obj!=null && !obj.isJsonNull()){
            GsonBuilder gb = new GsonBuilder();
            if(isFormat) gb.setPrettyPrinting();
            gb.serializeNulls();
            Gson gson = gb.create();
            str = gson.toJson(obj);
    }
    return str;
}
 
Example 14
Source File: ChannelServiceModelProvider.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
protected Gson createGson ()
{
    final GsonBuilder builder = new GsonBuilder ();
    builder.setPrettyPrinting ();
    builder.serializeNulls ();
    builder.setDateFormat ( "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" );
    builder.registerTypeAdapter ( DeployGroup.class, new DeployGroupTypeAdapter () );
    builder.registerTypeAdapter ( MetaKey.class, MetaKeyTypeAdapter.INSTANCE );
    return builder.create ();
}
 
Example 15
Source File: Ddf2JsonGenerator.java    From SI with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public Ddf2JsonGenerator() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.registerTypeAdapter(ObjectModel.class, new ObjectModelSerializer());
    gsonBuilder.registerTypeAdapter(ResourceModel.class, new ResourceModelSerializer());
    gsonBuilder.setPrettyPrinting();
    gson = gsonBuilder.create();
}
 
Example 16
Source File: JsonFunctions.java    From jphp with Apache License 2.0 5 votes vote down vote up
@Immutable
public static String json_encode(Memory memory, int options) {
    GsonBuilder builder;
    if (options != 0) {
        MemorySerializer serializer = new MemorySerializer();
        builder = JsonExtension.createGsonBuilder(serializer);

        if ((options & JsonConstants.JSON_PRETTY_PRINT) == JsonConstants.JSON_PRETTY_PRINT) {
            builder.setPrettyPrinting();
        }

        if ((options & JsonConstants.JSON_HEX_TAG) != JsonConstants.JSON_HEX_TAG) {
            builder.disableHtmlEscaping();
        }

        if ((options & JsonConstants.JSON_FORCE_OBJECT) == JsonConstants.JSON_FORCE_OBJECT) {
            serializer.setForceObject(true);
        }

        if ((options & JsonConstants.JSON_NUMERIC_CHECK) == JsonConstants.JSON_NUMERIC_CHECK) {
            serializer.setNumericCheck(true);
        }

        if ((options & JsonConstants.JSON_UNESCAPED_UNICODE) == JsonConstants.JSON_UNESCAPED_UNICODE) {
            serializer.unescapedUnicode(true);
        }
    } else {
        builder = JsonExtension.DEFAULT_GSON_BUILDER;
    }

    Gson gson = builder.create();
    return gson.toJson(memory);
}
 
Example 17
Source File: JsonKeyspaceBeanSerializer.java    From mr4c with Apache License 2.0 4 votes vote down vote up
public void serializeKeyspaceBean(KeyspaceBean keyspace, Writer writer) throws IOException {
	GsonBuilder builder = new GsonBuilder();
	builder.setPrettyPrinting();
	Gson gson = builder.create();
		gson.toJson(keyspace,writer);
}
 
Example 18
Source File: GsonHelper.java    From sumk with Apache License 2.0 4 votes vote down vote up
public static GsonBuilder builder(String module) {
	if (module == null || module.isEmpty()) {
		module = "sumk";
	}

	DateTimeTypeAdapter da = new DateTimeTypeAdapter();
	String format = AppInfo.get(module + ".gson.date.format");
	if (StringUtil.isNotEmpty(format)) {
		da.setDateFormat(format);
	}

	GsonBuilder gb = new GsonBuilder().registerTypeAdapter(Date.class, da);

	if (AppInfo.getBoolean(module + ".gson.disableHtmlEscaping", false)) {
		gb.disableHtmlEscaping();
	}
	if (AppInfo.getBoolean(module + ".gson.shownull", false)) {
		gb.serializeNulls();
	}
	if (AppInfo.getBoolean(module + ".gson.disableInnerClassSerialization", false)) {
		gb.disableInnerClassSerialization();
	}
	if (AppInfo.getBoolean(module + ".gson.generateNonExecutableJson", false)) {
		gb.generateNonExecutableJson();
	}
	if (AppInfo.getBoolean(module + ".gson.serializeSpecialFloatingPointValues", false)) {
		gb.serializeSpecialFloatingPointValues();
	}

	if (AppInfo.getBoolean(module + ".gson.longSerialize2String", false)) {
		gb.setLongSerializationPolicy(LongSerializationPolicy.STRING);
	}

	if (AppInfo.getBoolean(module + ".gson.prettyPrinting", false)) {
		gb.setPrettyPrinting();
	}
	if (AppInfo.getBoolean(module + ".gson.date.adaper", true)) {
		DateAdapters.registerAll(gb);
	}
	return gb;
}
 
Example 19
Source File: JsonDatasetBeanSerializer.java    From mr4c with Apache License 2.0 4 votes vote down vote up
public String serializeDataFileBean(DataFileBean file) {
	GsonBuilder builder = new GsonBuilder();
	builder.setPrettyPrinting();
	Gson gson = builder.create();
		return gson.toJson(file);
}
 
Example 20
Source File: SwaggerModel.java    From swagger-for-elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a valid JSON representation of the model, according to the Swagger schema.
 */
public String toJson() {
    GsonBuilder gson = new GsonBuilder();
    gson.setPrettyPrinting();
    return gson.create().toJson(this);
}