Skip to content

Commit beb4699

Browse files
committed
Move jsonapi.c and jsonapi.h to src/common.
To make this work, (1) makeJsonLexContextCstringLen now takes the encoding to be used as an argument; (2) check_stack_depth() is made to do nothing in frontend code, and (3) elog(ERROR, ...) is changed to pg_log_fatal + exit in frontend code. Mark Dilger, reviewed and slightly revised by me. Discussion: http://postgr.es/m/CA+TgmoYfOXhd27MUDGioVh6QtpD0C1K-f6ObSA10AWiHBAL5bA@mail.gmail.com
1 parent dc78866 commit beb4699

File tree

12 files changed

+41
-20
lines changed

12 files changed

+41
-20
lines changed

contrib/hstore/hstore_io.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
#include "access/htup_details.h"
99
#include "catalog/pg_type.h"
10+
#include "common/jsonapi.h"
1011
#include "funcapi.h"
1112
#include "hstore.h"
1213
#include "lib/stringinfo.h"
1314
#include "libpq/pqformat.h"
1415
#include "utils/builtins.h"
1516
#include "utils/json.h"
16-
#include "utils/jsonapi.h"
1717
#include "utils/jsonb.h"
1818
#include "utils/lsyscache.h"
1919
#include "utils/memutils.h"

src/backend/tsearch/to_tsany.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
*/
1414
#include "postgres.h"
1515

16+
#include "common/jsonapi.h"
1617
#include "tsearch/ts_cache.h"
1718
#include "tsearch/ts_utils.h"
1819
#include "utils/builtins.h"
19-
#include "utils/jsonapi.h"
2020
#include "utils/jsonfuncs.h"
2121

2222

src/backend/tsearch/wparser.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
#include "catalog/namespace.h"
1717
#include "catalog/pg_type.h"
1818
#include "commands/defrem.h"
19+
#include "common/jsonapi.h"
1920
#include "funcapi.h"
2021
#include "tsearch/ts_cache.h"
2122
#include "tsearch/ts_utils.h"
2223
#include "utils/builtins.h"
23-
#include "utils/jsonapi.h"
2424
#include "utils/jsonfuncs.h"
2525
#include "utils/varlena.h"
2626

src/backend/utils/adt/Makefile

-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ OBJS = \
4444
int.o \
4545
int8.o \
4646
json.o \
47-
jsonapi.o \
4847
jsonb.o \
4948
jsonb_gin.o \
5049
jsonb_op.o \

src/backend/utils/adt/json.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ json_recv(PG_FUNCTION_ARGS)
127127
str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
128128

129129
/* Validate it. */
130-
lex = makeJsonLexContextCstringLen(str, nbytes, false);
130+
lex = makeJsonLexContextCstringLen(str, nbytes, GetDatabaseEncoding(), false);
131131
pg_parse_json_or_ereport(lex, &nullSemAction);
132132

133133
PG_RETURN_TEXT_P(cstring_to_text_with_len(str, nbytes));

src/backend/utils/adt/jsonb.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ jsonb_from_cstring(char *json, int len)
261261

262262
memset(&state, 0, sizeof(state));
263263
memset(&sem, 0, sizeof(sem));
264-
lex = makeJsonLexContextCstringLen(json, len, true);
264+
lex = makeJsonLexContextCstringLen(json, len, GetDatabaseEncoding(), true);
265265

266266
sem.semstate = (void *) &state;
267267

src/backend/utils/adt/jsonb_util.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515

1616
#include "catalog/pg_collation.h"
1717
#include "catalog/pg_type.h"
18+
#include "common/jsonapi.h"
1819
#include "miscadmin.h"
1920
#include "utils/builtins.h"
2021
#include "utils/datetime.h"
2122
#include "utils/hashutils.h"
2223
#include "utils/json.h"
23-
#include "utils/jsonapi.h"
2424
#include "utils/jsonb.h"
2525
#include "utils/memutils.h"
2626
#include "utils/varlena.h"

src/backend/utils/adt/jsonfuncs.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "access/htup_details.h"
2020
#include "catalog/pg_type.h"
21+
#include "common/jsonapi.h"
2122
#include "fmgr.h"
2223
#include "funcapi.h"
2324
#include "lib/stringinfo.h"
@@ -27,7 +28,6 @@
2728
#include "utils/builtins.h"
2829
#include "utils/hsearch.h"
2930
#include "utils/json.h"
30-
#include "utils/jsonapi.h"
3131
#include "utils/jsonb.h"
3232
#include "utils/jsonfuncs.h"
3333
#include "utils/lsyscache.h"
@@ -514,6 +514,7 @@ makeJsonLexContext(text *json, bool need_escapes)
514514
{
515515
return makeJsonLexContextCstringLen(VARDATA_ANY(json),
516516
VARSIZE_ANY_EXHDR(json),
517+
GetDatabaseEncoding(),
517518
need_escapes);
518519
}
519520

@@ -2605,7 +2606,7 @@ populate_array_json(PopulateArrayContext *ctx, char *json, int len)
26052606
PopulateArrayState state;
26062607
JsonSemAction sem;
26072608

2608-
state.lex = makeJsonLexContextCstringLen(json, len, true);
2609+
state.lex = makeJsonLexContextCstringLen(json, len, GetDatabaseEncoding(), true);
26092610
state.ctx = ctx;
26102611

26112612
memset(&sem, 0, sizeof(sem));
@@ -3448,7 +3449,7 @@ get_json_object_as_hash(char *json, int len, const char *funcname)
34483449
HASHCTL ctl;
34493450
HTAB *tab;
34503451
JHashState *state;
3451-
JsonLexContext *lex = makeJsonLexContextCstringLen(json, len, true);
3452+
JsonLexContext *lex = makeJsonLexContextCstringLen(json, len, GetDatabaseEncoding(), true);
34523453
JsonSemAction *sem;
34533454

34543455
memset(&ctl, 0, sizeof(ctl));

src/common/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ OBJS_COMMON = \
5656
f2s.o \
5757
file_perm.o \
5858
ip.o \
59+
jsonapi.o \
5960
keywords.o \
6061
kwlookup.o \
6162
link-canary.o \

src/backend/utils/adt/jsonapi.c renamed to src/common/jsonapi.c

+27-9
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,32 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* src/backend/utils/adt/jsonapi.c
10+
* src/common/jsonapi.c
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
14+
#ifndef FRONTEND
1415
#include "postgres.h"
16+
#else
17+
#include "postgres_fe.h"
18+
#endif
1519

20+
#include "common/jsonapi.h"
1621
#include "mb/pg_wchar.h"
22+
23+
#ifdef FRONTEND
24+
#include "common/logging.h"
25+
#else
1726
#include "miscadmin.h"
18-
#include "utils/jsonapi.h"
27+
#endif
28+
29+
#ifdef FRONTEND
30+
#define check_stack_depth()
31+
#define json_log_and_abort(...) \
32+
do { pg_log_fatal(__VA_ARGS__); exit(1); } while(0)
33+
#else
34+
#define json_log_and_abort(...) elog(ERROR, __VA_ARGS__)
35+
#endif
1936

2037
/*
2138
* The context of the parser is maintained by the recursive descent
@@ -135,13 +152,14 @@ IsValidJsonNumber(const char *str, int len)
135152
* if really required.
136153
*/
137154
JsonLexContext *
138-
makeJsonLexContextCstringLen(char *json, int len, bool need_escapes)
155+
makeJsonLexContextCstringLen(char *json, int len, int encoding, bool need_escapes)
139156
{
140157
JsonLexContext *lex = palloc0(sizeof(JsonLexContext));
141158

142159
lex->input = lex->token_terminator = lex->line_start = json;
143160
lex->line_number = 1;
144161
lex->input_length = len;
162+
lex->input_encoding = encoding;
145163
if (need_escapes)
146164
lex->strval = makeStringInfo();
147165
return lex;
@@ -720,7 +738,7 @@ json_lex_string(JsonLexContext *lex)
720738
ch = (ch * 16) + (*s - 'A') + 10;
721739
else
722740
{
723-
lex->token_terminator = s + pg_mblen(s);
741+
lex->token_terminator = s + pg_encoding_mblen(lex->input_encoding, s);
724742
return JSON_UNICODE_ESCAPE_FORMAT;
725743
}
726744
}
@@ -759,7 +777,7 @@ json_lex_string(JsonLexContext *lex)
759777
/* We can't allow this, since our TEXT type doesn't */
760778
return JSON_UNICODE_CODE_POINT_ZERO;
761779
}
762-
else if (GetDatabaseEncoding() == PG_UTF8)
780+
else if (lex->input_encoding == PG_UTF8)
763781
{
764782
unicode_to_utf8(ch, (unsigned char *) utf8str);
765783
utf8len = pg_utf_mblen((unsigned char *) utf8str);
@@ -809,7 +827,7 @@ json_lex_string(JsonLexContext *lex)
809827
default:
810828
/* Not a valid string escape, so signal error. */
811829
lex->token_start = s;
812-
lex->token_terminator = s + pg_mblen(s);
830+
lex->token_terminator = s + pg_encoding_mblen(lex->input_encoding, s);
813831
return JSON_ESCAPING_INVALID;
814832
}
815833
}
@@ -823,7 +841,7 @@ json_lex_string(JsonLexContext *lex)
823841
* shown it's not a performance win.
824842
*/
825843
lex->token_start = s;
826-
lex->token_terminator = s + pg_mblen(s);
844+
lex->token_terminator = s + pg_encoding_mblen(lex->input_encoding, s);
827845
return JSON_ESCAPING_INVALID;
828846
}
829847

@@ -1010,7 +1028,7 @@ report_parse_error(JsonParseContext ctx, JsonLexContext *lex)
10101028
* unhandled enum values. But this needs to be here anyway to cover the
10111029
* possibility of an incorrect input.
10121030
*/
1013-
elog(ERROR, "unexpected json parse state: %d", (int) ctx);
1031+
json_log_and_abort("unexpected json parse state: %d", (int) ctx);
10141032
return JSON_SUCCESS; /* silence stupider compilers */
10151033
}
10161034

@@ -1077,7 +1095,7 @@ json_errdetail(JsonParseErrorType error, JsonLexContext *lex)
10771095
* unhandled enum values. But this needs to be here anyway to cover the
10781096
* possibility of an incorrect input.
10791097
*/
1080-
elog(ERROR, "unexpected json parse error type: %d", (int) error);
1098+
json_log_and_abort("unexpected json parse error type: %d", (int) error);
10811099
return NULL; /* silence stupider compilers */
10821100
}
10831101

src/include/utils/jsonapi.h renamed to src/include/common/jsonapi.h

+2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ typedef struct JsonLexContext
7373
{
7474
char *input;
7575
int input_length;
76+
int input_encoding;
7677
char *token_start;
7778
char *token_terminator;
7879
char *prev_token_terminator;
@@ -149,6 +150,7 @@ extern JsonParseErrorType json_count_array_elements(JsonLexContext *lex,
149150
*/
150151
extern JsonLexContext *makeJsonLexContextCstringLen(char *json,
151152
int len,
153+
int encoding,
152154
bool need_escapes);
153155

154156
/* lex one token */

src/include/utils/jsonfuncs.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#ifndef JSONFUNCS_H
1515
#define JSONFUNCS_H
1616

17-
#include "utils/jsonapi.h"
17+
#include "common/jsonapi.h"
1818
#include "utils/jsonb.h"
1919

2020
/*

0 commit comments

Comments
 (0)