Skip to content

Commit 39909d1

Browse files
committed
Add array_to_json and row_to_json functions.
Also move the escape_json function from explain.c to json.c where it seems to belong. Andrew Dunstan, Reviewd by Abhijit Menon-Sen.
1 parent 69e9768 commit 39909d1

File tree

7 files changed

+579
-48
lines changed

7 files changed

+579
-48
lines changed

doc/src/sgml/func.sgml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9617,6 +9617,65 @@ table2-mapping
96179617
</sect2>
96189618
</sect1>
96199619

9620+
<sect1 id="functions-json">
9621+
<title>JSON functions</title>
9622+
9623+
<indexterm zone="datatype-json">
9624+
<primary>JSON</primary>
9625+
<secondary>Functions and operators</secondary>
9626+
</indexterm>
9627+
9628+
<para>
9629+
This section descripbes the functions that are available for creating
9630+
JSON (see <xref linkend="datatype-json">) data.
9631+
</para>
9632+
9633+
<table id="functions-json-table">
9634+
<title>JSON Support Functions</title>
9635+
<tgroup cols="4">
9636+
<thead>
9637+
<row>
9638+
<entry>Function</entry>
9639+
<entry>Description</entry>
9640+
<entry>Example</entry>
9641+
<entry>Example Result</entry>
9642+
</row>
9643+
</thead>
9644+
<tbody>
9645+
<row>
9646+
<entry>
9647+
<indexterm>
9648+
<primary>array_to_json</primary>
9649+
</indexterm>
9650+
<literal>array_to_json(anyarray [, pretty_bool])</literal>
9651+
</entry>
9652+
<entry>
9653+
Returns the array as JSON. A Postgres multi-dimensional array
9654+
becomes a JSON array of arrays. Line feeds will be added between
9655+
dimension 1 elements if pretty_bool is true.
9656+
</entry>
9657+
<entry><literal>array_to_json('{{1,5},{99,100}}'::int[])</literal></entry>
9658+
<entry><literal>[[1,5],[99,100]]</literal></entry>
9659+
</row>
9660+
<row>
9661+
<entry>
9662+
<indexterm>
9663+
<primary>row_to_json</primary>
9664+
</indexterm>
9665+
<literal>row_to_json(record [, pretty_bool])</literal>
9666+
</entry>
9667+
<entry>
9668+
Returns the row as JSON. Line feeds will be added between level
9669+
1 elements if pretty_bool is true.
9670+
</entry>
9671+
<entry><literal>row_to_json(row(1,'foo'))</literal></entry>
9672+
<entry><literal>{"f1":1,"f2":"foo"}</literal></entry>
9673+
</row>
9674+
</tbody>
9675+
</tgroup>
9676+
</table>
9677+
9678+
</sect1>
96209679

96219680
<sect1 id="functions-sequence">
96229681
<title>Sequence Manipulation Functions</title>

src/backend/commands/explain.c

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "rewrite/rewriteHandler.h"
2525
#include "tcop/tcopprot.h"
2626
#include "utils/builtins.h"
27+
#include "utils/json.h"
2728
#include "utils/lsyscache.h"
2829
#include "utils/rel.h"
2930
#include "utils/snapmgr.h"
@@ -99,7 +100,6 @@ static void ExplainDummyGroup(const char *objtype, const char *labelname,
99100
static void ExplainXMLTag(const char *tagname, int flags, ExplainState *es);
100101
static void ExplainJSONLineEnding(ExplainState *es);
101102
static void ExplainYAMLLineStarting(ExplainState *es);
102-
static void escape_json(StringInfo buf, const char *str);
103103
static void escape_yaml(StringInfo buf, const char *str);
104104

105105

@@ -2318,51 +2318,6 @@ ExplainYAMLLineStarting(ExplainState *es)
23182318
}
23192319
}
23202320

2321-
/*
2322-
* Produce a JSON string literal, properly escaping characters in the text.
2323-
*/
2324-
static void
2325-
escape_json(StringInfo buf, const char *str)
2326-
{
2327-
const char *p;
2328-
2329-
appendStringInfoCharMacro(buf, '\"');
2330-
for (p = str; *p; p++)
2331-
{
2332-
switch (*p)
2333-
{
2334-
case '\b':
2335-
appendStringInfoString(buf, "\\b");
2336-
break;
2337-
case '\f':
2338-
appendStringInfoString(buf, "\\f");
2339-
break;
2340-
case '\n':
2341-
appendStringInfoString(buf, "\\n");
2342-
break;
2343-
case '\r':
2344-
appendStringInfoString(buf, "\\r");
2345-
break;
2346-
case '\t':
2347-
appendStringInfoString(buf, "\\t");
2348-
break;
2349-
case '"':
2350-
appendStringInfoString(buf, "\\\"");
2351-
break;
2352-
case '\\':
2353-
appendStringInfoString(buf, "\\\\");
2354-
break;
2355-
default:
2356-
if ((unsigned char) *p < ' ')
2357-
appendStringInfo(buf, "\\u%04x", (int) *p);
2358-
else
2359-
appendStringInfoCharMacro(buf, *p);
2360-
break;
2361-
}
2362-
}
2363-
appendStringInfoCharMacro(buf, '\"');
2364-
}
2365-
23662321
/*
23672322
* YAML is a superset of JSON; unfortuantely, the YAML quoting rules are
23682323
* ridiculously complicated -- as documented in sections 5.3 and 7.3.3 of

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy