diff --git a/src/lib.rs b/src/lib.rs index 8911dea..6fa94e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -217,7 +217,7 @@ pub fn compile<'a>(path: &'a str) -> impl FnMut(&Value) -> result::Result(json: &Value) -> impl FnMut(&'a str) -> result::Result(json: &Value) -> impl FnMut(& let mut selector = Selector::new(); let _ = selector.value(json.into()); move |path: &str| { - selector.path(path)?.select_to() + selector.path(path)?.select_as() } } @@ -349,7 +349,7 @@ pub fn reader<'a>(json: &Value) -> impl FnMut(&'a str) -> result::Result result::Result { let mut selector = Selector::new(); - selector.path(path)?.value(json.into())?.select_to_value() + selector.path(path)?.value(json.into())?.select_as_value() } #[deprecated(since = "0.1.4", note = "Please use the select function instead")] @@ -389,7 +389,7 @@ pub fn select_as_str(json: &str, path: &str) -> result::Result { Selector::new() .path(path)? .value_from_str(json)? - .select_to_str() + .select_as_str() } /// This function compile a jsonpath everytime and it convert `&str` to `jsonpath's RefValue` everytime and then it return a deserialized-instance of type `T`. @@ -434,5 +434,5 @@ pub fn select_as(json: &str, path: &str) -> resu Selector::new() .path(path)? .value_from_str(json)? - .select_to() + .select_as() } \ No newline at end of file diff --git a/src/select/mod.rs b/src/select/mod.rs index f924e4b..10403fb 100644 --- a/src/select/mod.rs +++ b/src/select/mod.rs @@ -130,15 +130,30 @@ impl Selector { } } + #[deprecated(since = "0.1.13", note = "Please use the select_as_str function instead")] pub fn select_to_str(&self) -> result::Result { + self.select_as_str() + } + + #[deprecated(since = "0.1.13", note = "Please use the select_as_value function instead")] + pub fn select_to_value(&self) -> result::Result { + self.select_as_value() + } + + #[deprecated(since = "0.1.13", note = "Please use the select_as function instead")] + pub fn select_to(&self) -> result::Result { + self.select_as() + } + + pub fn select_as_str(&self) -> result::Result { serde_json::to_string(self.select()?.deref()).map_err(|e| e.to_string()) } - pub fn select_to_value(&self) -> result::Result { + pub fn select_as_value(&self) -> result::Result { Ok((&self.select()?).into()) } - pub fn select_to(&self) -> result::Result { + pub fn select_as(&self) -> result::Result { T::deserialize(self.select()?.deref()).map_err(|e| e.to_string()) } diff --git a/wasm/README.md b/wasm/README.md index 9186334..e5b684c 100644 --- a/wasm/README.md +++ b/wasm/README.md @@ -14,6 +14,8 @@ It is Webassembly version of [jsonpath_lib](https://github.com/freestrings/jsonp ### jsonpath.Selector +> Selector's selectTo function is deprecated since 0.1.3 + ```javascript let jsonObj = { "school": { @@ -31,19 +33,19 @@ let jsonObj = { let selector = new jsonpath.Selector().value(jsonObj); { - let jsonObj = selector.path('$..[?(@.age >= 30)]').selectTo(); + let jsonObj = selector.path('$..[?(@.age >= 30)]').selectAs(); let resultObj = [{"name": "친구3", "age": 30}]; console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj)); } { - let jsonObj = selector.path('$..[?(@.age == 20)]').selectTo(); + let jsonObj = selector.path('$..[?(@.age == 20)]').selectAs(); let resultObj = [{"name": "친구1", "age": 20}, {"name": "친구2", "age": 20}]; console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj)); } { - let jsonObj = selector.value({"friends": [ {"name": "친구5", "age": 20} ]}).selectTo(); + let jsonObj = selector.value({"friends": [ {"name": "친구5", "age": 20} ]}).selectAs(); let resultObj = [{"name": "친구5", "age": 20}]; console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj)); } diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index 0618ee2..9c7750b 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -55,12 +55,12 @@ fn into_serde_json(js_value: &JsValue) -> Result if js_value.is_string() { match serde_json::from_str(js_value.as_string().unwrap().as_str()) { Ok(json) => Ok(json), - Err(e) => Err(format!("{:?}", e)) + Err(e) => Err(e.to_string()) } } else { match js_value.into_serde() { Ok(json) => Ok(json), - Err(e) => Err(format!("{:?}", e)) + Err(e) => Err(e.to_string()) } } } @@ -203,13 +203,23 @@ impl Selector { #[wasm_bindgen(catch, js_name = selectToStr)] pub fn select_to_str(&mut self) -> result::Result { - let json_str = self.selector.select_to_str()?; + self.select_as_str() + } + + #[wasm_bindgen(catch, js_name = selectAsStr)] + pub fn select_as_str(&mut self) -> result::Result { + let json_str = self.selector.select_as_str()?; Ok(JsValue::from_str(&json_str)) } #[wasm_bindgen(catch, js_name = selectTo)] pub fn select_to(&mut self) -> result::Result { - let ref_value = self.selector.select_to::() + self.select_as() + } + + #[wasm_bindgen(catch, js_name = selectAs)] + pub fn select_as(&mut self) -> result::Result { + let ref_value = self.selector.select_as::() .map_err(|e| JsValue::from_str(&e))?; Ok(JsValue::from_serde(&ref_value) .map_err(|e| JsValue::from_str(&format!("{:?}", e)))?) diff --git a/wasm/tests/web.rs b/wasm/tests/web.rs index cf5fba9..349afbf 100644 --- a/wasm/tests/web.rs +++ b/wasm/tests/web.rs @@ -113,6 +113,6 @@ fn selector_struct() { let mut selector = jsonpath::Selector::new(); selector.path("$..book[2]").unwrap(); selector.value(JsValue::from_str(json_str())).unwrap(); - let json: Value = selector.select_to().unwrap().into_serde().unwrap(); + let json: Value = selector.select_as().unwrap().into_serde().unwrap(); assert_eq!(json, target_json()); } \ No newline at end of file