Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / json.h
1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is SpiderMonkey JSON.
15  *
16  * The Initial Developer of the Original Code is
17  * Mozilla Corporation.
18  * Portions created by the Initial Developer are Copyright (C) 1998-1999
19  * the Initial Developer. All Rights Reserved.
20  *
21  * Contributor(s):
22  *   Robert Sayre <sayrer@gmail.com>
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either of the GNU General Public License Version 2 or later (the "GPL"),
26  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 #ifndef json_h___
39 #define json_h___
40
41 #include "jsprvtd.h"
42 #include "jspubtd.h"
43 #include "jsvalue.h"
44 #include "jsvector.h"
45
46 #define JSON_MAX_DEPTH  2048
47 #define JSON_PARSER_BUFSIZE 1024
48
49 extern js::Class js_JSONClass;
50
51 extern JSObject *
52 js_InitJSONClass(JSContext *cx, JSObject *obj);
53
54 extern JSBool
55 js_Stringify(JSContext *cx, js::Value *vp, JSObject *replacer,
56              const js::Value &space, js::StringBuffer &sb);
57
58 extern JSBool js_TryJSON(JSContext *cx, js::Value *vp);
59
60 /* JSON parsing states; most permit leading whitespace. */
61 enum JSONParserState {
62     /* Start of string. */
63     JSON_PARSE_STATE_INIT,
64
65     /* JSON fully processed, expecting only trailing whitespace. */
66     JSON_PARSE_STATE_FINISHED,
67
68     /* Start of JSON value. */
69     JSON_PARSE_STATE_VALUE,
70
71     /* Start of first key/value pair in object, or at }. */
72     JSON_PARSE_STATE_OBJECT_INITIAL_PAIR,
73
74     /* Start of subsequent key/value pair in object, after delimiting comma. */
75     JSON_PARSE_STATE_OBJECT_PAIR,
76
77     /* At : in key/value pair in object. */
78     JSON_PARSE_STATE_OBJECT_IN_PAIR,
79
80     /* Immediately after key/value pair in object: at , or }. */
81     JSON_PARSE_STATE_OBJECT_AFTER_PAIR,
82
83     /* Start of first element of array or at ]. */
84     JSON_PARSE_STATE_ARRAY_INITIAL_VALUE,
85
86     /* Immediately after element in array: at , or ]. */
87     JSON_PARSE_STATE_ARRAY_AFTER_ELEMENT,
88
89
90     /* The following states allow no leading whitespace. */
91
92     /* Within string literal. */
93     JSON_PARSE_STATE_STRING,
94
95     /* At first character after \ in string literal. */
96     JSON_PARSE_STATE_STRING_ESCAPE,
97
98     /* Within numbers in \uXXXX in string literal. */
99     JSON_PARSE_STATE_STRING_HEX,
100
101     /* Within numeric literal. */
102     JSON_PARSE_STATE_NUMBER,
103
104     /* Handling keywords (only null/true/false pass validity post-check). */
105     JSON_PARSE_STATE_KEYWORD
106 };
107
108 struct JSONParser;
109
110 extern JSONParser *
111 js_BeginJSONParse(JSContext *cx, js::Value *rootVal, bool suppressErrors = false);
112
113 /* Aargh, Windows. */
114 #ifdef STRICT
115 #undef STRICT
116 #endif
117 #ifdef LEGACY
118 #undef LEGACY
119 #endif
120
121 /*
122  * The type of JSON decoding to perform.  Strict decoding is to-the-spec;
123  * legacy decoding accepts a few non-JSON syntaxes historically accepted by the
124  * implementation.  (Full description of these deviations is deliberately
125  * omitted.)  New users should use strict decoding rather than legacy decoding,
126  * as legacy decoding might be removed at a future time.
127  */
128 enum DecodingMode { STRICT, LEGACY };
129
130 extern JS_FRIEND_API(JSBool)
131 js_ConsumeJSONText(JSContext *cx, JSONParser *jp, const jschar *data, uint32 len,
132                    DecodingMode decodingMode = STRICT);
133
134 extern bool
135 js_FinishJSONParse(JSContext *cx, JSONParser *jp, const js::Value &reviver);
136
137 #endif /* json_h___ */