使用content type application/json发送序列化为json而不是text/plain的对象是否有性能优势?

I know many frameworks (like Spring) can map and serialize data based on the content type, but in general I find that this process is easy enough that it isn't a compelling reason to use application/json over text/plain for JSON objects.

示例:

xhr.setRequestHeader("Content-type","text/plain");
// or
xhr.setRequestHeader("Content-type","application/json");
xhr.send(JSON.stringify({"foo": "bar"}));

推荐答案

Let's assume that you are talking about using JSON versus a custom format (using MIME type text/plain) for passing structured data.

Performance may be broken down into different components; e.g.

  • 将内容编码为格式所需的相对时间,
  • relative time taken to decode the format to give you the original content, and
  • 编码内容的相对大小.

理论上,我们可以说,假设优化设计和实现的自定义格式不会比JSON慢,也不会比JSON密度低.("证据"是显而易见的. Select JSON的最佳实现,并对格式进行一些不会影响性能的微小更改.)

但实际上,您必须比较实际格式和实际实现的性能.因此,答案是性能实际上取决于您在设计和实现格式及其相关编码/解码软件方面做得有多好.此外,这还取决于如何实现JSON.有许多具有不同性能特征的服务器端JSON库,以及将数据从"本机"数据 struct 映射到"本机"数据 struct 的不同方式.

这让我们看到了JSON(和XML)相对于自定义格式的真正优势.

  • With JSON and XML, there are libraries available for any mainstream language you chose to use to assist in encoding and decoding content. With a custom format, you have to roll your own encoding / decoding for the client and server sides.

  • With JSON and XML, there are standards that say what is well-formed that allow other people to implement encoders / decoders. With a custom format, you have to write the specification yourself if you want other people to be able to implement your format.

  • JSON和XML具有处理字符集编码和数据中出现的"元"字符等问题的标准方法.有了风俗习惯,你必须自己理解和解决这些问题.(如果你不这样做,你很可能会在赛道上遇到困难.)

  • 易变易变.发展基于JSON/XML的格式是一件相对简单的事情.但是使用自定义格式,您(至少)有更多的工作要做,并且根据您的设计 Select ,这可能非常困难.

For most application, these issues matter far more than performance. And that's why JSON or XML are widely used.

FOLLOWUP

但是,如果相反,您假设我没有使用自定义实现,并将发送MIME类型为text/Plain的JSON与MIME类型为application/json的发送JSON进行比较,会发生什么呢?

Then the answer is that it makes almost no performance difference.

  • 您可以在HTTP请求或响应头中节省6个字节,因为MIME类型字符串较短,但对于大小以数千字节为单位的典型HTTP消息来说,这并不重要.
  • Using a "text/plain" content type makes no difference to the work that needs to be done to encode / decode the request or response messages ... apart from the time taken to compare / copy 6 extra bytes, which is probably too small to measure.

In addition, using an inaccurate MIME type is (arguably) a violation of the HTTP specs. If you do this:

  • it is more likely that the receiver will mishandle the response; e.g. fail to decode it, or show it in a browser window, and

  • 假设客户端或服务器使用HTTP内容类型协商,则可能会中断该协商.

In short, I can think of no good reason to do this1, and a few good reasons not to do it.


1 - In general. Obviously, there will be edge cases ...

Json相关问答推荐

将嵌套的json中的字符串强制转换为数字

如何在改装Android中将ResponseBody转换为JSONObject

使用JQ合并JSON列表中的对象

在Snowflake中查询JSON时,属性名称是否支持绑定参数?

在linux控制台中解析json字符串的最简单方法是什么?

Delphi 11.3无法从变体创建/添加JSON

JOLT分裂和数组数据

如何使用 Google 表格应用程序脚本将 JSON 中的多个字段提取到 Google 表格中

派生类的属性没有得到价值

在 postgresql 中将行转换为 json 对象

使用基本身份验证通过 CURL 发布 JSON

Swift:如何从字典中删除空值?

jquery用json数据填充下拉列表

gson:将 null 视为空字符串

使用 Retrofit 解析动态密钥 Json 字符串

如何在 Django JSONField 数据上聚合(最小/最大等)?

如何将单引号转义成双引号转成单引号

如何将 mysqli 结果转换为 JSON?

Protocol Buffer vs Json - 何时 Select 一个而不是另一个

将 JSON 模式转换为 python 类