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 */
137154JsonLexContext *
138- makeJsonLexContextCstringLen (char * json ,int len ,bool need_escapes )
155+ makeJsonLexContextCstringLen (char * json ,int len ,int encoding , bool need_escapes )
139156{
140157JsonLexContext * lex = palloc0 (sizeof (JsonLexContext ));
141158
142159lex -> input = lex -> token_terminator = lex -> line_start = json ;
143160lex -> line_number = 1 ;
144161lex -> input_length = len ;
162+ lex -> input_encoding = encoding ;
145163if (need_escapes )
146164lex -> strval = makeStringInfo ();
147165return lex ;
@@ -720,7 +738,7 @@ json_lex_string(JsonLexContext *lex)
720738ch = (ch * 16 )+ (* s - 'A' )+ 10 ;
721739else
722740{
723- lex -> token_terminator = s + pg_mblen ( s );
741+ lex -> token_terminator = s + pg_encoding_mblen ( lex -> input_encoding , s );
724742return 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 */
760778return JSON_UNICODE_CODE_POINT_ZERO ;
761779}
762- else if (GetDatabaseEncoding () == PG_UTF8 )
780+ else if (lex -> input_encoding == PG_UTF8 )
763781{
764782unicode_to_utf8 (ch , (unsignedchar * )utf8str );
765783utf8len = pg_utf_mblen ((unsignedchar * )utf8str );
@@ -809,7 +827,7 @@ json_lex_string(JsonLexContext *lex)
809827default :
810828/* Not a valid string escape, so signal error. */
811829lex -> token_start = s ;
812- lex -> token_terminator = s + pg_mblen ( s );
830+ lex -> token_terminator = s + pg_encoding_mblen ( lex -> input_encoding , s );
813831return JSON_ESCAPING_INVALID ;
814832}
815833}
@@ -823,7 +841,7 @@ json_lex_string(JsonLexContext *lex)
823841 * shown it's not a performance win.
824842 */
825843lex -> token_start = s ;
826- lex -> token_terminator = s + pg_mblen ( s );
844+ lex -> token_terminator = s + pg_encoding_mblen ( lex -> input_encoding , s );
827845return 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 );
10141032return 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 );
10811099return NULL ;/* silence stupider compilers */
10821100}
10831101