Commit 04f364cd authored by Setako's avatar Setako

[Change] parse pretty print as option

parent e554fd85
......@@ -18,16 +18,21 @@ open class GsonJsonParserService(
private val typeAdapterFactories: Components<TypeAdapterFactory>,
private val typeAdapters: Components<TypeAdapterPair>
) : JsonParserService, SystemLevel {
protected val prettyGson = GsonBuilder()
.also { builder -> typeAdapterFactories.forEach { builder.registerTypeAdapterFactory(it) } }
.also { builder -> typeAdapters.forEach { builder.registerTypeAdapter(it.type, it.typeAdapter) } }
.setPrettyPrinting().create()
protected val gson = GsonBuilder()
.also { builder -> typeAdapterFactories.forEach { builder.registerTypeAdapterFactory(it) } }
.also { builder -> typeAdapters.forEach { builder.registerTypeAdapter(it.type, it.typeAdapter) } }
.setPrettyPrinting().create()!!
.create()
override fun encode(obj: Any): Single<String> =
Single.defer { Single.just(gson.toJson(obj)) }
override fun encode(obj: Any, prettyPrint: Boolean): Single<String> =
Single.defer { Single.just((if (prettyPrint) prettyGson else gson).toJson(obj)) }
override fun encode(obj: Any, modelType: KType): Single<String> =
Single.defer { Single.just(gson.toJson(obj, modelType.javaType)) }
override fun encode(obj: Any, modelType: KType, prettyPrint: Boolean): Single<String> =
Single.defer { Single.just((if (prettyPrint) prettyGson else gson).toJson(obj, modelType.javaType)) }
override fun <T : Any> decode(encoded: String, modelClass: KClass<T>): Single<T> =
Single.defer { Single.just(gson.fromJson(encoded, modelClass.java)) }
......
......@@ -21,7 +21,7 @@ open class SnakeYamlParserService : YamlParserService, SystemLevel {
protected val representer = Representer(options)
protected val yaml = Yaml(CustomClassLoaderConstructor(this.javaClass.classLoader), representer, options)
override fun encode(obj: Any): Single<String> =
override fun encode(obj: Any, prettyPrint: Boolean): Single<String> =
Single.defer {
representer.addClassTag(obj::class.java, Tag.MAP)
Single.just(yaml.dump(obj))
......
......@@ -13,7 +13,7 @@ open class Toml4jTomlParserService : TomlParserService, SystemLevel {
protected val toml = Toml()
protected val tomlWriter = TomlWriter()
override fun encode(obj: Any): Single<String> = Single.defer { Single.just(tomlWriter.write(obj)) }
override fun encode(obj: Any, prettyPrint: Boolean): Single<String> = Single.defer { Single.just(tomlWriter.write(obj)) }
override fun <T : Any> decode(encoded: String, modelClass: KClass<T>): Single<T> =
Single.defer { Single.just(toml.read(encoded).to(modelClass.java)) }
......
......@@ -9,12 +9,14 @@ interface ParserService {
/**
* Encode the object to target format
*/
fun encode(obj: Any): Single<String>
fun encode(obj: Any, prettyPrint: Boolean): Single<String>
fun encode(obj: Any): Single<String> = encode(obj, true)
/**
* Encode the object with type to target format
*/
fun encode(obj: Any, modelType: KType): Single<String> = encode(obj)
fun encode(obj: Any, modelType: KType, prettyPrint: Boolean = true): Single<String> = encode(obj, prettyPrint)
fun encode(obj: Any, modelType: KType): Single<String> = encode(obj, modelType, true)
/**
* Parse the encoded string to object with model class
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment