你能避免 Gson 转换“<”吗?和“>”转换成 unicode 转义序列?

发布于 2024-10-01 14:03:09 字数 557 浏览 6 评论 0 原文

我注意到 Gson 转换字符串“<”转换为 JSON 输出中的 unicode 转义序列。你能以某种方式避免这种情况,或者做像“<”这样的字符吗?和“>”总是必须在 JSON 中转义?

考虑这个打印 {"s":"\u003c"}; 的示例我想要的只是 {"s":"<"}

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

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

上下文:我正在创建的 JSON 片段与 HTML 页面甚至 JavaScript 无关;它只是用于将某些结构化信息传递给另一个软件(嵌入设备中,用 C 语言编写)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

悲欢浪云 2024-10-08 14:03:10

如果您使用Retrofit和GsonConverterFactory,那么解决方案可能有点难看。我不认为这是最好的解决方案,但这是我唯一想到的并且对我有用。

因此,您需要创建自己的 GsonConverterFactory,在其中通过禁用 HTML 转义来修改 Gson 创建。

为了创建自定义的 GsonConverterFactory,您还需要创建 GsonRequestBodyConverter 和 GsonResponseBodyConverter,因为这些类在原始库中是私有的。

您可以通过以下方式完成:

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)
        }
    }
}

因此,此代码片段与原始类的代码基本相同,但转换为 Kotlin。这里唯一有效的改变是:
fun create(gson: Gson? = Gson().newBuilder().disableHtmlEscaping().create())

您可以像使用默认的GsonConverterFactory一样使用此类>:

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

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()
柳若烟 2024-10-08 14:03:09

您需要 禁用 HTML 转义

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

You need to disable HTML escaping.

Gson gson = new GsonBuilder().disableHtmlEscaping().create();
作死小能手 2024-10-08 14:03:09

Ampasand 符号被替换为 \u0026 ,通过使用它它得到了解决。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文