Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5384a73

Browse files
committed
Built-in JSON data type.
Like the XML data type, we simply store JSON data as text, after checking that it is valid. More complex operations such as canonicalization and comparison may come later, but this is enough for not. There are a few open issues here, such as whether we should attempt to detect UTF-8 surrogate pairs represented as \uXXXX\uYYYY, but this gets the basic framework in place.
1 parent 4c6cedd commit 5384a73

File tree

11 files changed

+1059
-5
lines changed

11 files changed

+1059
-5
lines changed

doc/src/sgml/datatype.sgml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@
269269
<entry></entry>
270270
<entry>XML data</entry>
271271
</row>
272+
273+
<row>
274+
<entry><type>json</type></entry>
275+
<entry></entry>
276+
<entry>JSON data</entry>
277+
</row>
272278
</tbody>
273279
</tgroup>
274280
</table>
@@ -4169,6 +4175,32 @@ SET xmloption TO { DOCUMENT | CONTENT };
41694175
</sect2>
41704176
</sect1>
41714177

4178+
<sect1 id="datatype-json">
4179+
<title><acronym>JSON</> Type</title>
4180+
4181+
<indexterm zone="datatype-json">
4182+
<primary>JSON</primary>
4183+
</indexterm>
4184+
4185+
<para>
4186+
The <type>json</type> data type can be used to store JSON data. Such
4187+
data can also be stored as <type>text</type>, but the
4188+
<type>json</type> data type has the advantage of checking that each
4189+
stored value is a valid JSON value.
4190+
</para>
4191+
4192+
<para>
4193+
<productname>PostgreSQL</productname> allows only one server encoding
4194+
per database. It is therefore not possible for JSON to conform rigidly
4195+
to the specification unless the server encoding is UTF-8. Attempts to
4196+
directly include characters which cannot be represented in the server
4197+
encoding will fail; conversely, characters which can be represented in
4198+
the server encoding but not in UTF-8 will be allowed.
4199+
<literal>\uXXXX</literal> escapes are allowed regardless of the server
4200+
encoding, and are checked only for syntactic correctness.
4201+
</para>
4202+
</sect1>
4203+
41724204
&array;
41734205

41744206
&rowtypes;

src/backend/commands/explain.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ ExplainResultDesc(ExplainStmt *stmt)
242242
{
243243
TupleDesc tupdesc;
244244
ListCell *lc;
245-
bool xml = false;
245+
Oid result_type = TEXTOID;
246246

247247
/* Check for XML format option */
248248
foreach(lc, stmt->options)
@@ -253,15 +253,20 @@ ExplainResultDesc(ExplainStmt *stmt)
253253
{
254254
char *p = defGetString(opt);
255255

256-
xml = (strcmp(p, "xml") == 0);
256+
if (strcmp(p, "xml") == 0)
257+
result_type = XMLOID;
258+
else if (strcmp(p, "json") == 0)
259+
result_type = JSONOID;
260+
else
261+
result_type = TEXTOID;
257262
/* don't "break", as ExplainQuery will use the last value */
258263
}
259264
}
260265

261266
/* Need a tuple descriptor representing a single TEXT or XML column */
262267
tupdesc = CreateTemplateTupleDesc(1, false);
263268
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "QUERY PLAN",
264-
xml ? XMLOID : TEXTOID, -1, 0);
269+
result_type, -1, 0);
265270
return tupdesc;
266271
}
267272

src/backend/utils/adt/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ endif
1818
OBJS = acl.o arrayfuncs.o array_userfuncs.o arrayutils.o bool.o \
1919
cash.o char.o date.o datetime.o datum.o domains.o \
2020
enum.o float.o format_type.o \
21-
geo_ops.o geo_selfuncs.o int.o int8.o like.o lockfuncs.o \
21+
geo_ops.o geo_selfuncs.o int.o int8.o json.o like.o lockfuncs.o \
2222
misc.o nabstime.o name.o numeric.o numutils.o \
2323
oid.o oracle_compat.o pseudotypes.o rangetypes.o rangetypes_gist.o \
2424
rowtypes.o regexp.o regproc.o ruleutils.o selfuncs.o \

0 commit comments

Comments
 (0)