tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

JSON.h (7425B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 /*
      7 * JSON serialization and deserialization operations.
      8 */
      9 
     10 #ifndef js_JSON_h
     11 #define js_JSON_h
     12 
     13 #include <stdint.h>  // uint32_t
     14 
     15 #include "jstypes.h"  // JS_PUBLIC_API
     16 
     17 #include "js/TypeDecls.h"
     18 
     19 using JSONWriteCallback = bool (*)(const char16_t* buf, uint32_t len,
     20                                   void* data);
     21 
     22 /**
     23 * Performs the JSON.stringify operation, as specified by ECMAScript, except
     24 * writing stringified data by exactly one call of |callback|, passing |data| as
     25 * argument.
     26 *
     27 * In cases where JSON.stringify would return undefined, this function calls
     28 * |callback| with the string "null".
     29 *
     30 * If a length hint is passed, space will be reserved for at least that many
     31 * characters.
     32 */
     33 extern JS_PUBLIC_API bool JS_Stringify(JSContext* cx,
     34                                       JS::MutableHandle<JS::Value> value,
     35                                       JS::Handle<JSObject*> replacer,
     36                                       JS::Handle<JS::Value> space,
     37                                       JSONWriteCallback callback, void* data);
     38 extern JS_PUBLIC_API bool JS_StringifyWithLengthHint(
     39    JSContext* cx, JS::MutableHandle<JS::Value> value,
     40    JS::Handle<JSObject*> replacer, JS::Handle<JS::Value> space,
     41    JSONWriteCallback callback, void* data, size_t lengthHint);
     42 
     43 namespace JS {
     44 
     45 /**
     46 * An API akin to JS_Stringify but with the goal of not having observable
     47 * side-effects when the stringification is performed.  This means it does not
     48 * allow a replacer or a custom space and has the following constraints on its
     49 * input:
     50 *
     51 * 1) The input must be a plain object or array, not an abitrary value.
     52 * 2) Every value in the graph reached by the algorithm starting with this
     53 *    object must be one of the following: null, undefined, a string (NOT a
     54 *    string object!), a boolean, a finite number (i.e. no NaN or Infinity or
     55 *    -Infinity), a plain object with no accessor properties, or an Array with
     56 *    no holes.
     57 *
     58 * The actual behavior differs from JS_Stringify only in asserting the above and
     59 * NOT attempting to get the "toJSON" property from things, since that could
     60 * clearly have side-effects.
     61 */
     62 extern JS_PUBLIC_API bool ToJSONMaybeSafely(JSContext* cx,
     63                                            JS::Handle<JSObject*> input,
     64                                            JSONWriteCallback callback,
     65                                            void* data);
     66 
     67 /**
     68 * Performs the JSON.stringify operation, as specified by ECMAScript, except
     69 * writing stringified data by one call of |callback|, passing |data| as
     70 * argument.
     71 *
     72 * In cases where JSON.stringify would return undefined, this function does not
     73 * call |callback| at all.
     74 */
     75 extern JS_PUBLIC_API bool ToJSON(JSContext* cx, Handle<Value> value,
     76                                 Handle<JSObject*> replacer,
     77                                 Handle<Value> space,
     78                                 JSONWriteCallback callback, void* data);
     79 
     80 } /* namespace JS */
     81 
     82 /**
     83 * Performs the JSON.parse operation as specified by ECMAScript.
     84 */
     85 extern JS_PUBLIC_API bool JS_ParseJSON(JSContext* cx, const char16_t* chars,
     86                                       uint32_t len,
     87                                       JS::MutableHandle<JS::Value> vp);
     88 
     89 /**
     90 * Performs the JSON.parse operation as specified by ECMAScript.
     91 */
     92 extern JS_PUBLIC_API bool JS_ParseJSON(JSContext* cx, JS::Handle<JSString*> str,
     93                                       JS::MutableHandle<JS::Value> vp);
     94 
     95 /**
     96 * Performs the JSON.parse operation as specified by ECMAScript.
     97 */
     98 extern JS_PUBLIC_API bool JS_ParseJSON(JSContext* cx,
     99                                       const JS::Latin1Char* chars,
    100                                       uint32_t len,
    101                                       JS::MutableHandle<JS::Value> vp);
    102 
    103 /**
    104 * Performs the JSON.parse operation as specified by ECMAScript, using the
    105 * given |reviver| argument as the corresponding optional argument to that
    106 * function.
    107 */
    108 extern JS_PUBLIC_API bool JS_ParseJSONWithReviver(
    109    JSContext* cx, const char16_t* chars, uint32_t len,
    110    JS::Handle<JS::Value> reviver, JS::MutableHandle<JS::Value> vp);
    111 
    112 /**
    113 * Performs the JSON.parse operation as specified by ECMAScript, using the
    114 * given |reviver| argument as the corresponding optional argument to that
    115 * function.
    116 */
    117 extern JS_PUBLIC_API bool JS_ParseJSONWithReviver(
    118    JSContext* cx, JS::Handle<JSString*> str, JS::Handle<JS::Value> reviver,
    119    JS::MutableHandle<JS::Value> vp);
    120 
    121 namespace JS {
    122 
    123 /**
    124 * Returns true if the given text is valid JSON.
    125 */
    126 extern JS_PUBLIC_API bool IsValidJSON(const JS::Latin1Char* chars,
    127                                      uint32_t len);
    128 extern JS_PUBLIC_API bool IsValidJSON(const char16_t* chars, uint32_t len);
    129 
    130 /**
    131 * Handler with callbacks for JS::ParseJSONWithHandler.
    132 *
    133 * Each method is called during parsing the JSON string. If the method returns
    134 * true, the parsing keeps going.  If the method returns false, the parsing
    135 * stops and fails.
    136 *
    137 * The error method is called when syntax error happens while parsing the input.
    138 * This method is not called when handler's method returns false.
    139 */
    140 class JSONParseHandler {
    141 public:
    142  JSONParseHandler() {}
    143  virtual ~JSONParseHandler() {}
    144 
    145  // Called when '{' is found for an object.
    146  virtual bool startObject() = 0;
    147 
    148  // Called when a property name is found for an object.
    149  // The character type depends on the input type and also the content of the
    150  // property name. The consumer should implement both methods.
    151  virtual bool propertyName(const JS::Latin1Char* name, size_t length) = 0;
    152  virtual bool propertyName(const char16_t* name, size_t length) = 0;
    153 
    154  // Called when '}' is found for an object.
    155  virtual bool endObject() = 0;
    156 
    157  // Called when '[' is found for an array.
    158  virtual bool startArray() = 0;
    159 
    160  // Called when ']' is found for an array.
    161  virtual bool endArray() = 0;
    162 
    163  // Called when a string is found.
    164  // The character type depends on the input type and also the content of the
    165  // string. The consumer should implement both methods.
    166  virtual bool stringValue(const JS::Latin1Char* str, size_t length) = 0;
    167  virtual bool stringValue(const char16_t* str, size_t length) = 0;
    168 
    169  // Called when a number is found.
    170  virtual bool numberValue(double d) = 0;
    171 
    172  // Called when a boolean is found.
    173  virtual bool booleanValue(bool v) = 0;
    174 
    175  // Called when null is found.
    176  virtual bool nullValue() = 0;
    177 
    178  // Called when syntax error happens.
    179  virtual void error(const char* msg, uint32_t line, uint32_t column) = 0;
    180 };
    181 
    182 /**
    183 * Performs the JSON.parse operation as specified by ECMAScript, and call
    184 * callbacks defined by the handler.
    185 */
    186 extern JS_PUBLIC_API bool ParseJSONWithHandler(const JS::Latin1Char* chars,
    187                                               uint32_t len,
    188                                               JSONParseHandler* handler);
    189 extern JS_PUBLIC_API bool ParseJSONWithHandler(const char16_t* chars,
    190                                               uint32_t len,
    191                                               JSONParseHandler* handler);
    192 
    193 }  // namespace JS
    194 
    195 #endif /* js_JSON_h */