JSON.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. use js_sys::*;
  2. use wasm_bindgen::prelude::*;
  3. use wasm_bindgen::JsCast;
  4. use wasm_bindgen_test::*;
  5. #[wasm_bindgen_test]
  6. fn parse_array() {
  7. let js_array = JSON::parse("[1, 2, 3]").unwrap();
  8. assert!(Array::is_array(&js_array));
  9. let array = Array::from(&js_array);
  10. assert_eq!(array.length(), 3);
  11. assert_eq!(array.pop(), 3);
  12. assert_eq!(array.pop(), 2);
  13. assert_eq!(array.pop(), 1);
  14. }
  15. #[wasm_bindgen_test]
  16. fn parse_object() {
  17. let js_object = JSON::parse("{\"x\": 5, \"y\": true, \"z\": [\"foo\", \"bar\"]}").unwrap();
  18. assert!(js_object.is_object());
  19. let obj = Object::from(js_object);
  20. let keys = Object::keys(&obj);
  21. assert_eq!(keys.length(), 3);
  22. assert_eq!(keys.pop().as_string().unwrap(), "z");
  23. assert_eq!(keys.pop().as_string().unwrap(), "y");
  24. assert_eq!(keys.pop().as_string().unwrap(), "x");
  25. let values = Object::values(&obj);
  26. assert_eq!(values.length(), 3);
  27. let z = values.pop();
  28. assert!(Array::is_array(&z));
  29. let z_array = Array::from(&z);
  30. assert_eq!(z_array.length(), 2);
  31. let y = values.pop();
  32. assert_eq!(y.as_bool(), Some(true));
  33. let x = values.pop();
  34. assert_eq!(x.as_f64().unwrap(), 5.0);
  35. }
  36. #[wasm_bindgen_test]
  37. fn parse_error() {
  38. let js_object = JSON::parse("invalid json");
  39. assert!(js_object.is_err());
  40. let err = js_object.unwrap_err();
  41. assert!(err.is_instance_of::<Error>());
  42. }
  43. #[wasm_bindgen_test]
  44. fn stringify() {
  45. let arr = Array::new();
  46. arr.push(&JsValue::from(1));
  47. arr.push(&JsValue::from(true));
  48. arr.push(&JsValue::from("hello"));
  49. let str1: String = JSON::stringify(&JsValue::from(arr)).unwrap().into();
  50. assert_eq!(str1, "[1,true,\"hello\"]");
  51. let obj = Object::new();
  52. Reflect::set(obj.as_ref(), &JsValue::from("foo"), &JsValue::from("bar")).unwrap();
  53. let str2: String = JSON::stringify(&JsValue::from(obj)).unwrap().into();
  54. assert_eq!(str2, "{\"foo\":\"bar\"}");
  55. }
  56. #[wasm_bindgen_test]
  57. fn stringify_error() {
  58. let func = Function::new_no_args("throw new Error(\"rust really rocks\")");
  59. let obj = Object::new();
  60. Reflect::set(obj.as_ref(), &JsValue::from("toJSON"), func.as_ref()).unwrap();
  61. let result = JSON::stringify(&JsValue::from(obj));
  62. assert!(result.is_err());
  63. let err_obj = result.unwrap_err();
  64. assert!(err_obj.is_instance_of::<Error>());
  65. let err: &Error = err_obj.dyn_ref().unwrap();
  66. let err_msg: String = From::from(err.message());
  67. assert!(err_msg.contains("rust really rocks"));
  68. }
  69. #[wasm_bindgen_test]
  70. fn stringify_with_replacer() {
  71. let obj = Object::new();
  72. Reflect::set(obj.as_ref(), &JsValue::from("foo"), &JsValue::from("bar")).unwrap();
  73. Reflect::set(
  74. obj.as_ref(),
  75. &JsValue::from("hello"),
  76. &JsValue::from("world"),
  77. )
  78. .unwrap();
  79. let replacer_array = Array::new();
  80. replacer_array.push(&JsValue::from("hello"));
  81. let output1: String =
  82. JSON::stringify_with_replacer(&JsValue::from(obj.clone()), &JsValue::from(replacer_array))
  83. .unwrap()
  84. .into();
  85. assert_eq!(output1, "{\"hello\":\"world\"}");
  86. let replacer_func =
  87. Function::new_with_args("key, value", "return key === 'hello' ? undefined : value");
  88. let output2: String =
  89. JSON::stringify_with_replacer(&JsValue::from(obj), &JsValue::from(replacer_func))
  90. .unwrap()
  91. .into();
  92. assert_eq!(output2, "{\"foo\":\"bar\"}");
  93. }
  94. #[wasm_bindgen_test]
  95. fn stringify_with_replacer_error() {
  96. let arr = Array::new();
  97. arr.push(&JsValue::from(1));
  98. arr.push(&JsValue::from(true));
  99. arr.push(&JsValue::from("hello"));
  100. let replacer = Function::new_no_args("throw new Error(\"rust really rocks\")");
  101. let result = JSON::stringify_with_replacer(&JsValue::from(arr), &JsValue::from(replacer));
  102. assert!(result.is_err());
  103. let err_obj = result.unwrap_err();
  104. assert!(err_obj.is_instance_of::<Error>());
  105. let err: &Error = err_obj.dyn_ref().unwrap();
  106. let err_msg: String = From::from(err.message());
  107. assert!(err_msg.contains("rust really rocks"));
  108. }
  109. #[wasm_bindgen_test]
  110. fn stringify_with_replacer_and_space() {
  111. let arr = Array::new();
  112. arr.push(&JsValue::from(1));
  113. arr.push(&JsValue::from(true));
  114. arr.push(&JsValue::from("hello"));
  115. let output1: String = JSON::stringify_with_replacer_and_space(
  116. &JsValue::from(arr),
  117. &JsValue::NULL,
  118. &JsValue::from(4),
  119. )
  120. .unwrap()
  121. .into();
  122. assert_eq!(output1, "[\n 1,\n true,\n \"hello\"\n]");
  123. let obj = Object::new();
  124. Reflect::set(obj.as_ref(), &JsValue::from("foo"), &JsValue::from("bar")).unwrap();
  125. Reflect::set(
  126. obj.as_ref(),
  127. &JsValue::from("hello"),
  128. &JsValue::from("world"),
  129. )
  130. .unwrap();
  131. let replacer_array = Array::new();
  132. replacer_array.push(&JsValue::from("hello"));
  133. let output2: String = JSON::stringify_with_replacer_and_space(
  134. &JsValue::from(obj.clone()),
  135. &JsValue::from(replacer_array),
  136. &JsValue::from(4),
  137. )
  138. .unwrap()
  139. .into();
  140. assert_eq!(output2, "{\n \"hello\": \"world\"\n}");
  141. let replacer_func =
  142. Function::new_with_args("key, value", "return key === 'hello' ? undefined : value");
  143. let output3: String = JSON::stringify_with_replacer_and_space(
  144. &JsValue::from(obj),
  145. &JsValue::from(replacer_func),
  146. &JsValue::from(4),
  147. )
  148. .unwrap()
  149. .into();
  150. assert_eq!(output3, "{\n \"foo\": \"bar\"\n}");
  151. }
  152. #[wasm_bindgen_test]
  153. fn stringify_with_replacer_and_space_error() {
  154. let arr = Array::new();
  155. arr.push(&JsValue::from(1));
  156. arr.push(&JsValue::from(true));
  157. arr.push(&JsValue::from("hello"));
  158. let replacer = Function::new_no_args("throw new Error(\"rust really rocks\")");
  159. let result = JSON::stringify_with_replacer_and_space(
  160. &JsValue::from(arr),
  161. &JsValue::from(replacer),
  162. &JsValue::from(4),
  163. );
  164. assert!(result.is_err());
  165. let err_obj = result.unwrap_err();
  166. assert!(err_obj.is_instance_of::<Error>());
  167. let err: &Error = err_obj.dyn_ref().unwrap();
  168. let err_msg: String = From::from(err.message());
  169. assert!(err_msg.contains("rust really rocks"));
  170. }