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,28 +3066,28 @@ pub mod WebAssembly {
} }
} }
// JSON /// The `JSON` object contains methods for parsing [JavaScript Object
#[wasm_bindgen] /// Notation (JSON)](https://json.org/) and converting values to JSON. It
extern "C" { /// can't be called or constructed, and aside from its two method
/// The `JSON` object contains methods for parsing [JavaScript Object /// properties, it has no interesting functionality of its own.
/// Notation (JSON)](https://json.org/) and converting values to JSON. It #[allow(non_snake_case)]
/// can't be called or constructed, and aside from its two method pub mod JSON {
/// properties, it has no interesting functionality of its own. use super::*;
#[wasm_bindgen(extends = Object)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub type JSON;
// JSON
#[wasm_bindgen]
extern "C" {
/// The `JSON.parse()` method parses a JSON string, constructing the /// The `JSON.parse()` method parses a JSON string, constructing the
/// JavaScript value or object described by the string. /// JavaScript value or object described by the string.
/// ///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse) /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse)
#[wasm_bindgen(catch, static_method_of = JSON)] #[wasm_bindgen(catch, js_namespace = JSON)]
pub fn parse(text: &str) -> Result<JsValue, JsValue>; pub fn parse(text: &str) -> Result<JsValue, JsValue>;
/// The `JSON.stringify()` method converts a JavaScript value to a JSON string. /// 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) /// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
#[wasm_bindgen(catch, static_method_of = JSON)] #[wasm_bindgen(catch, js_namespace = JSON)]
pub fn stringify(obj: &JsValue) -> Result<JsString, JsValue>; pub fn stringify(obj: &JsValue) -> Result<JsString, JsValue>;
/// The `JSON.stringify()` method converts a JavaScript value to a JSON string. /// The `JSON.stringify()` method converts a JavaScript value to a JSON string.
@ -3099,7 +3099,7 @@ extern "C" {
/// of the object are included in the resulting JSON string. /// 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) /// [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)] #[wasm_bindgen(catch, js_namespace = JSON, js_name = stringify)]
pub fn stringify_with_replacer(obj: &JsValue, replacer: &JsValue) -> Result<JsString, JsValue>; 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 `JSON.stringify()` method converts a JavaScript value to a JSON string.
@ -3120,13 +3120,14 @@ extern "C" {
/// white space is used. /// white space is used.
/// ///
/// [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify) /// [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)] #[wasm_bindgen(catch, js_namespace = JSON, js_name = stringify)]
pub fn stringify_with_replacer_and_space( pub fn stringify_with_replacer_and_space(
obj: &JsValue, obj: &JsValue,
replacer: &JsValue, replacer: &JsValue,
space: &JsValue, space: &JsValue,
) -> Result<JsString, JsValue>; ) -> Result<JsString, JsValue>;
}
} }
// JsString // JsString

View File

@ -198,15 +198,3 @@ fn stringify_with_replacer_and_space_error() {
let err_msg: String = From::from(err.message()); let err_msg: String = From::from(err.message());
assert!(err_msg.contains("rust really rocks")); 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();
}