8.14. JSON Types

∥☆過路亽.° 提交于 2020-03-09 18:25:11
8.14. JSON Types
8.14.JSON类型
JSON data types are for storing JSON (JavaScript Object Notation) data, as specified in RFC 71591Such data can also be stored as text, but the JSON data types have the advantage of enforcing that each stored value is valid according to the JSON rules. There are also assorted JSON-specific functions and operators available for data stored in these data types; see Section 9.15.
JSON数据类型用于存储JSON(JavaScript Object Notation)数据(如RFC 71591中所述)。此类数据也可以存储为文本,但是JSON数据类型的优点是可以根据JSON规则强制每个存储的值均有效。对于这些数据类型中存储的数据,还提供了各种特定于JSON的函数和运算符。请参阅第9.15节
 
There are two JSON data types: json and jsonb. They accept almost identical sets of values as input.The major practical difference is one of efficiency. The json data type stores an exact copy of the input text, which processing functions must reparse on each execution; while jsonb data is stored in a decomposed binary format that makes it slightly slower to input due to added conversion overhead,but significantly faster to process, since no reparsing is needed. jsonb also supports indexing, which can be a significant advantage.
有两种JSON数据类型:json和jsonb。他们接受几乎相同的值集作为输入。主要的实际差异之一是效率。 json数据类型存储输入文本的精确副本,处理函数必须在每次执行时重新解析; jsonb数据以分解后的二进制格式存储,由于增加了转换开销,因此输入速度稍慢,但由于不需要重新解析,因此处理速度明显加快。 jsonb还支持索引,这是一个很大的优势。
 
Because the json type stores an exact copy of the input text, it will preserve semantically-insignificant white space between tokens, as well as the order of keys within JSON objects. Also, if a JSON object within the value contains the same key more than once, all the key/value pairs are kept. (The processing functions consider the last value as the operative one.) By contrast, jsonb does not preserve white space, does not preserve the order of object keys, and does not keep duplicate object keys. If duplicate keys are specified in the input, only the last value is kept.
因为json类型存储输入文本的精确副本,所以它将保留标记之间语义上无关紧要的空白以及JSON对象中键的顺序。另外,如果值中的JSON对象包含相同的键不止一次,则所有键/值对都会保留。 (处理函数将最后一个值视为可操作的值。)相反,jsonb不会保留空格,不会保留对象键的顺序,也不会保留重复的对象键。如果在输入中指定了重复的键,则仅保留最后一个值。
 
In general, most applications should prefer to store JSON data as jsonb, unless there are quite specialized needs, such as legacy assumptions about ordering of object keys.
通常,大多数应用程序应该将JSON数据存储在jsonb数据类型中,除非有非常特殊的需求,例如关于对象键顺序的传统假设。
 
PostgreSQL allows only one character set encoding per database. It is therefore not possible for the JSON types to conform rigidly to the JSON specification unless the database encoding is UTF8. Attempts to directly include characters that cannot be represented in the database encoding will fail; conversely, characters that can be represented in the database encoding but not in UTF8 will be allowed.
PostgreSQL每个数据库只允许一种字符集编码。因此,除非数据库编码为UTF8,否则JSON类型不可能严格符合JSON规范。尝试输入包含数据库编码中无法表示的字符会失败;相反,将允许使用可以在数据库编码中表示但不能以UTF8表示的字符。
 
RFC 7159 permits JSON strings to contain Unicode escape sequences denoted by \uXXXX. In the input function for the json type, Unicode escapes are allowed regardless of the database encoding, and are checked only for syntactic correctness (that is, that four hex digits follow \u). However, the input function for jsonb is stricter: it disallows Unicode escapes for non-ASCII characters (those above +007F) unless the database encoding is UTF8. The jsonb type also rejects \u0000 (because that cannot be represented in PostgreSQL's text type), and it insists that any use of Unicode surrogate pairs to designate characters outside the Unicode Basic Multilingual Plane be correct. Valid Unicode escapes are converted to the equivalent ASCII or UTF8 character for storage; this includes folding surrogate pairs into a single character.
RFC 7159允许JSON字符串包含\uXXXX表示的Unicode转义序列。 在json类型的输入函数中,无论数据库编码如何,均允许Unicode转义,并且仅检查语法正确性(即,在\u之后跟四个十六进制数字)。 但是,jsonb的输入函数更加严格:除非数据库编码为UTF8,否则它不允许非ASCII字符(U + 007F以上的字符)使用Unicode转义。 jsonb类型也拒绝\u0000(因为无法在PostgreSQL的文本类型中表示),并且坚持认为使用Unicode替代对来指定Unicode Basic Multilingual Plane之外的字符都是正确的。 有效的Unicode转义会转换为等效的ASCII或UTF8字符进行存储; 这包括将代理对存储为单个字符。
Note
Many of the JSON processing functions described in Section 9.15 will convert Unicode escapes to regular characters, and will therefore throw the same types of errors just described even if their input is of type json not jsonb. The fact that the json input function does not make these checks may be considered a historical artifact, although it does allow for simple storage (without processing) of JSON Unicode escapes in a non-UTF8 database encoding. In general, it is best to avoid mixing Unicode escapes in JSON with a non-UTF8 database encoding, if possible.
第9.15节中描述的许多JSON处理函数会将Unicode转义转换为常规字符,因此,即使输入类型为json而不是jsonb,它们也会抛出与上述类型相同的错误。json输入函数不进行这些检查的事实可能被认为是历史问题,尽管它确实允许以非UTF8数据库编码的形式简单存储(无需处理)JSON Unicode转义。通常,如果可能的话,最好避免在JSON中将Unicode转义与非UTF8数据库编码混合使用。
 
When converting textual JSON input into jsonb, the primitive types described by RFC 7159 are effectively mapped onto native PostgreSQL types, as shown in Table 8.23. Therefore, there are some minor additional constraints on what constitutes valid jsonb data that do not apply to the json type,nor to JSON in the abstract, corresponding to limits on what can be represented by the underlying data type. Notably, jsonb will reject numbers that are outside the range of the PostgreSQL numeric data type, while json will not. Such implementation-defined restrictions are permitted by RFC 7159.However, in practice such problems are far more likely to occur in other implementations, as it is common to represent JSON's number primitive type as IEEE 754 double precision floating point (which RFC 7159 explicitly anticipates and allows for). When using JSON as an interchange format with such systems, the danger of losing numeric precision compared to data originally stored by PostgreSQL should be considered.
将文本JSON输入转换为jsonb时,RFC 7159描述的原始类型与本地PostgreSQL类型相对应,如表8.23所示。因此,对于构成有效jsonb数据的内容存在一些较小的附加约束,这些约束不适用于json类型,也不适用于抽象的JSON,这对应于基础数据类型可以表示的内容的限制。值得注意的是,jsonb会拒绝PostgreSQL数字数据类型范围之外的数字,而json不会。 RFC 7159允许此类实现定义的限制。但是,实际上,在其他实现中更容易出现此类问题,因为通常将JSON的数字原始类型表示为IEEE 754双精度浮点(RFC 7159明确预期并允许)。在此类系统上使用JSON作为交换格式时,应考虑与PostgreSQL最初存储的数据相比失去数字精度的危险。
 
Conversely, as noted in the table there are some minor restrictions on the input format of JSON primitive types that do not apply to the corresponding PostgreSQL types.
相反,如表中所示,JSON基本类型的输入格式有一些较小的限制,不适用于相应的PostgreSQL类型。
 
 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!