Jonik
Jonik

Reputation: 81761

Can you avoid Gson converting "<" and ">" into unicode escape sequences?

I noticed that Gson converts the string "<" into an unicode escape sequence in JSON output. Can you avoid this somehow, or do characters like "<" and ">" always have to be escaped in JSON?

Consider this example which prints {"s":"\u003c"}; I'd want simply {"s":"<"}.

public static void main(String[] args) {
    Gson gson = new GsonBuilder().create();
    System.out.println(gson.toJson(new Foo()));  
}

static class Foo {
    String s = "<";
}

Context: the piece of JSON I'm creating has nothing to do with HTML pages or even JavaScript; it's just used to pass certain structured information to another piece of software (embedded in a device, written in C).

Upvotes: 162

Views: 51880

Answers (3)

Oleg Golomoz
Oleg Golomoz

Reputation: 532

If you use Retrofit and GsonConverterFactory, then the solution might be a bit ugly. I don't think it's the best solution, but it's the only I came to and it works for me.

So you need to create your own GsonConverterFactory in which you modify the Gson creation by disabling the HTML escaping.

In order to create your custom GsonConverterFactory you will need to create GsonRequestBodyConverter and GsonResponseBodyConverter too, because these classes are private in the original library.

You can do it in the following way:

class CustomGsonConverterFactory private constructor(private val gson: Gson) : Converter.Factory() {
    override fun responseBodyConverter(
        type: Type, annotations: Array<Annotation>, retrofit: Retrofit
    ): Converter<ResponseBody, *> {
        val adapter: TypeAdapter<*> = gson.getAdapter(TypeToken.get(type))
        return CustomGsonResponseBodyConverter(gson, adapter)
    }

    override fun requestBodyConverter(
        type: Type,
        parameterAnnotations: Array<Annotation>,
        methodAnnotations: Array<Annotation>,
        retrofit: Retrofit
    ): Converter<*, RequestBody> {
        val adapter: TypeAdapter<*> = gson.getAdapter(TypeToken.get(type))
        return CustomGsonRequestBodyConverter(gson, adapter)
    }

    internal class CustomGsonResponseBodyConverter<T>(
        private val gson: Gson,
        private val adapter: TypeAdapter<T>
    ) :
        Converter<ResponseBody, T> {
        @Throws(IOException::class)
        override fun convert(value: ResponseBody): T {
            val jsonReader = gson.newJsonReader(value.charStream())
            value.use {
                val result = adapter.read(jsonReader)
                if (jsonReader.peek() != JsonToken.END_DOCUMENT) {
                    throw JsonIOException("JSON document was not fully consumed.")
                }
                return result
            }
        }
    }

    internal class CustomGsonRequestBodyConverter<T>(
        private val gson: Gson,
        private val adapter: TypeAdapter<T>
    ) :
        Converter<T, RequestBody> {
        @Throws(IOException::class)
        override fun convert(value: T): RequestBody {
            val buffer = Buffer()
            val writer: Writer = OutputStreamWriter(buffer.outputStream(), StandardCharsets.UTF_8)
            val jsonWriter = gson.newJsonWriter(writer)
            adapter.write(jsonWriter, value)
            jsonWriter.close()
            return buffer.readByteString().toRequestBody(MEDIA_TYPE)
        }

        companion object {
            private val MEDIA_TYPE: MediaType = ("application/json; charset=UTF-8").toMediaType()
        }
    }

    companion object {
        @JvmOverloads
        fun create(gson: Gson? = Gson().newBuilder().disableHtmlEscaping().create()): CustomGsonConverterFactory {
            if (gson == null) throw NullPointerException("gson == null")
            return CustomGsonConverterFactory(gson)
        }
    }
}

So, this code snippet is basically the same code that the original classes have, but converted to Kotlin. The only change here, which does the trick, is this: fun create(gson: Gson? = Gson().newBuilder().disableHtmlEscaping().create())

You can use this class in the same way as the default GsonConverterFactory:

Retrofit.Builder()
            .client(OkHttpClient.Builder().build())
            .baseUrl(BASE_URL)
            .addCallAdapterFactory(RestApiResponseAdapterFactory())
            .addConverterFactory(CustomGsonConverterFactory.create())
            .build()

Upvotes: 0

ROOPA P D
ROOPA P D

Reputation: 21

the Ampasand symbol was replacing with \u0026 , by using this it got resolved.

Upvotes: 2

BalusC
BalusC

Reputation: 1108802

You need to disable HTML escaping.

Gson gson = new GsonBuilder().disableHtmlEscaping().create();

Upvotes: 313

Related Questions