JSON.h (2171B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- 2 * vim: set ts=8 sts=2 et sw=2 tw=80: 3 * This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef builtin_JSON_h 8 #define builtin_JSON_h 9 10 #include "mozilla/Range.h" 11 12 #include "NamespaceImports.h" 13 14 #include "js/RootingAPI.h" 15 16 namespace js { 17 18 class StringBuilder; 19 20 extern const JSClass JSONClass; 21 22 enum class StringifyBehavior { 23 // Attempt an optimistic fast path if possible, bailing back to the slow path 24 // if anything is encountered that could invalidate the fast path results per 25 // spec. Default behavior for non-DEBUG builds. 26 Normal, 27 28 // Handle a subset of functionality when called by JS::ToJSONMaybeSafely. 29 // Different restrictions than the fast path described for Normal. See the 30 // Stringify() comment below for details. 31 RestrictedSafe, 32 33 // If the fast path fails, throw an exception instead of falling back to the 34 // slow path. Useful for testing that something that should be handled by the 35 // fast path actually is. 36 FastOnly, 37 38 // Do not attempt the fast path. Useful for timing comparisons. 39 SlowOnly, 40 41 // Attempt to run both the fast and slow paths and compare the results, 42 // crashing on any discrepancy. For correctness testing only. Default behavior 43 // when DEBUG is defined. 44 Compare 45 }; 46 47 /** 48 * If stringifyBehavior is RestrictedSafe, Stringify will attempt to assert the 49 * API requirements of JS::ToJSONMaybeSafely as it traverses the graph, and will 50 * not try to invoke .toJSON on things as it goes. 51 */ 52 extern bool Stringify(JSContext* cx, js::MutableHandleValue vp, 53 JSObject* replacer, const Value& space, StringBuilder& sb, 54 StringifyBehavior stringifyBehavior); 55 56 template <typename CharT> 57 extern bool ParseJSONWithReviver(JSContext* cx, 58 const mozilla::Range<const CharT> chars, 59 HandleValue reviver, MutableHandleValue vp); 60 61 } // namespace js 62 63 #endif /* builtin_JSON_h */