Transform JSON into a namespace

This commit is contained in:
Ingvar Stepanyan 2019-04-13 02:13:47 +01:00
parent 75c2971ab9
commit 62b7d3e3c4
2 changed files with 57 additions and 68 deletions

View File

@ -3066,67 +3066,68 @@ pub mod WebAssembly {
}
}
// JSON
#[wasm_bindgen]
extern "C" {
/// The `JSON` object contains methods for parsing [JavaScript Object
/// Notation (JSON)](https://json.org/) and converting values to JSON. It
/// can't be called or constructed, and aside from its two method
/// properties, it has no interesting functionality of its own.
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub type JSON;
/// The `JSON` object contains methods for parsing [JavaScript Object
/// Notation (JSON)](https://json.org/) and converting values to JSON. It
/// can't be called or constructed, and aside from its two method
/// properties, it has no interesting functionality of its own.
#[allow(non_snake_case)]
pub mod JSON {
use super::*;
/// The `JSON.parse()` method parses a JSON string, constructing the
/// JavaScript value or object described by the string.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)
#[wasm_bindgen(catch, static_method_of = JSON)]
pub fn parse(text: &str) -> Result<JsValue, JsValue>;
// JSON
#[wasm_bindgen]
extern "C" {
/// The `JSON.parse()` method parses a JSON string, constructing the
/// JavaScript value or object described by the string.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)
#[wasm_bindgen(catch, js_namespace = JSON)]
pub fn parse(text: &str) -> Result<JsValue, JsValue>;
/// The `JSON.stringify()` method converts a JavaScript value to a JSON string.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
#[wasm_bindgen(catch, static_method_of = JSON)]
pub fn stringify(obj: &JsValue) -> Result<JsString, JsValue>;
/// The `JSON.stringify()` method converts a JavaScript value to a JSON string.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
#[wasm_bindgen(catch, js_namespace = JSON)]
pub fn stringify(obj: &JsValue) -> Result<JsString, JsValue>;
/// The `JSON.stringify()` method converts a JavaScript value to a JSON string.
///
/// The `replacer` argument is a function that alters the behavior of the stringification
/// process, or an array of String and Number objects that serve as a whitelist
/// for selecting/filtering the properties of the value object to be included
/// in the JSON string. If this value is null or not provided, all properties
/// of the object are included in the resulting JSON string.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
#[wasm_bindgen(catch, static_method_of = JSON, js_name = stringify)]
pub fn stringify_with_replacer(obj: &JsValue, replacer: &JsValue) -> Result<JsString, JsValue>;
/// The `JSON.stringify()` method converts a JavaScript value to a JSON string.
///
/// The `replacer` argument is a function that alters the behavior of the stringification
/// process, or an array of String and Number objects that serve as a whitelist
/// for selecting/filtering the properties of the value object to be included
/// in the JSON string. If this value is null or not provided, all properties
/// of the object are included in the resulting JSON string.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
#[wasm_bindgen(catch, js_namespace = JSON, js_name = stringify)]
pub fn stringify_with_replacer(obj: &JsValue, replacer: &JsValue) -> Result<JsString, JsValue>;
/// The `JSON.stringify()` method converts a JavaScript value to a JSON string.
///
/// The `replacer` argument is a function that alters the behavior of the stringification
/// process, or an array of String and Number objects that serve as a whitelist
/// for selecting/filtering the properties of the value object to be included
/// in the JSON string. If this value is null or not provided, all properties
/// of the object are included in the resulting JSON string.
///
/// The `space` argument is a String or Number object that's used to insert white space into
/// the output JSON string for readability purposes. If this is a Number, it
/// indicates the number of space characters to use as white space; this number
/// is capped at 10 (if it is greater, the value is just 10). Values less than
/// 1 indicate that no space should be used. If this is a String, the string
/// (or the first 10 characters of the string, if it's longer than that) is
/// used as white space. If this parameter is not provided (or is null), no
/// white space is used.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
#[wasm_bindgen(catch, static_method_of = JSON, js_name = stringify)]
pub fn stringify_with_replacer_and_space(
obj: &JsValue,
replacer: &JsValue,
space: &JsValue,
) -> Result<JsString, JsValue>;
/// The `JSON.stringify()` method converts a JavaScript value to a JSON string.
///
/// The `replacer` argument is a function that alters the behavior of the stringification
/// process, or an array of String and Number objects that serve as a whitelist
/// for selecting/filtering the properties of the value object to be included
/// in the JSON string. If this value is null or not provided, all properties
/// of the object are included in the resulting JSON string.
///
/// The `space` argument is a String or Number object that's used to insert white space into
/// the output JSON string for readability purposes. If this is a Number, it
/// indicates the number of space characters to use as white space; this number
/// is capped at 10 (if it is greater, the value is just 10). Values less than
/// 1 indicate that no space should be used. If this is a String, the string
/// (or the first 10 characters of the string, if it's longer than that) is
/// used as white space. If this parameter is not provided (or is null), no
/// white space is used.
///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
#[wasm_bindgen(catch, js_namespace = JSON, js_name = stringify)]
pub fn stringify_with_replacer_and_space(
obj: &JsValue,
replacer: &JsValue,
space: &JsValue,
) -> Result<JsString, JsValue>;
}
}
// JsString

View File

@ -198,15 +198,3 @@ fn stringify_with_replacer_and_space_error() {
let err_msg: String = From::from(err.message());
assert!(err_msg.contains("rust really rocks"));
}
#[wasm_bindgen_test]
fn json_extends() {
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(js_name = JSON)]
static json: JSON;
}
assert!(json.is_instance_of::<Object>());
let _: &Object = json.as_ref();
}