在text / plain上使用application / json的优点? [英] Advantages of using application/json over text/plain?

查看:1184
本文介绍了在text / plain上使用application / json的优点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用内容类型application / json在text / plain上发送序列化为json的对象是否有任何性能优势?我知道很多框架(比如Spring)可以根据内容类型对数据进行映射和序列化,但总的来说,我发现这个过程非常简单,因此对于JSON对象使用application / json over text / plain不是一个令人信服的理由。

Is there any performance advantage of using content type application/json sending an object serialized to json over 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.

推荐答案

我们假设您正在讨论使用JSON与自定义格式(使用MIME类型 text / plain )用于传递结构化数据。

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.


  • 将内容编码为格式所需的相对时间,

  • 解码所需的相对时间格式为您提供原始内容,

  • 编码内容的相对大小。

理论上,我们可以说假设的最佳设计和实现的自定义格式将不会比JSON更慢或更密集。 (证据很明显。选择JSON的最佳实现,并对不影响性能的格式进行一些小改动。)

In theory, we can say that a hypothetical optimally designed and implemented custom format will be no slower or no less dense than JSON. (The "proof" is obvious. Choose an optimal implementation of JSON and make some minor change to the format that doesn't affect performance.)

实际上,你必须比较实际格式和实际实现的性能。答案因此,性能实际上取决于您设计和实现格式及其相关编码/解码软件的工作有多好。此外,它还取决于您如何实现JSON。有许多服务器端JSON库具有不同的性能特征,以及将数据映射到本机数据结构的不同方式。

In reality though, you have to compare performance of actual formats and actual implementations. The answer therefore that the performance really depends on how good a job you do of designing and implementing the format and its associated encoding/decoding software. Furthermore, it also depends on how you implement JSON. There are a number of server-side JSON libraries with different performance characteristics, as well as different ways of mapping data from / to "native" data structures.

这给我们带来了了解JSON(和XML)相对于自定义格式的真正优势。

This brings us to the real advantages of JSON (and XML) over custom formats.


  • 使用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.

使用JSON和XML,有标准说明了什么格式良好,允许其他人实现编码器/解码器。使用自定义格式,如果您希望其他人能够实现您的格式,您必须自己编写规范。

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具有标准方式处理charset编码和出现在数据中的meta字符等问题。通过自定义,您必须了解并解决这些问题。 (如果你不这样做,你可能会遇到困难。)

JSON and XML have standard ways of dealing with issues like charset encoding and "meta" characters appearing in data. With a custom you have to understand and address these issues your self. (And if you don't, you are likely to get into difficulty down the track.)

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

Ease of change. It is a relatively simple matter to evolve a JSON / XML based format. But with a custom format, you have (at least) got more work to do, and depending on your design choices, it could be very difficult.

对于大多数应用程序,这些问题远远超过性能。这就是JSON或XML被广泛使用的原因。

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

关注


但是如果相反你假设我没有使用自定义实现并将发送JSON与mime类型的text / plain发送到mime类型application / json?

But what if instead you assume that I'm not using a custom implementation and compare sending JSON with a mime type of text/plain to that of mime type application/json?

然后答案是它几乎没有 性能的区别。

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


  • 在HTTP请求或响应头中保存6个字节,因为mime类型字符串较短,但这对于典型的HTTP消息来说无关紧要大小以千字节为单位。

  • 使用text / plain内容类型对编码/解码请求或响应消息需要完成的工作没有任何区别。除了比较/复制6个额外字节所花费的时间,这可能太小而无法衡量。

此外,使用不准确的MIME类型(可以说)违反了HTTP规范。如果你这样做:

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内容类型协商,假设您的客户端或服务器使用它。 / p>

you may break HTTP content type negotiation, assuming that your client or server uses it.

简而言之,我认为没有良好的理由这样做,还有一些好的理由不这样做。

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

这篇关于在text / plain上使用application / json的优点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆