mirror of
https://github.com/fluencelabs/jsonpath
synced 2025-03-31 14:01:05 +00:00
Selector's "select_to" function is deprecated
This commit is contained in:
parent
e2a6b13c9a
commit
135d3c319b
12
src/lib.rs
12
src/lib.rs
@ -217,7 +217,7 @@ pub fn compile<'a>(path: &'a str) -> impl FnMut(&Value) -> result::Result<Value,
|
|||||||
move |json| {
|
move |json| {
|
||||||
let s: &mut Selector = selector.borrow_mut();
|
let s: &mut Selector = selector.borrow_mut();
|
||||||
let _ = s.value(&json);
|
let _ = s.value(&json);
|
||||||
s.select_to_value()
|
s.select_as_value()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -261,7 +261,7 @@ pub fn selector<'a>(json: &Value) -> impl FnMut(&'a str) -> result::Result<Value
|
|||||||
let mut selector = Box::new(selector);
|
let mut selector = Box::new(selector);
|
||||||
move |path: &'a str| {
|
move |path: &'a str| {
|
||||||
let s: &mut Selector = selector.borrow_mut();
|
let s: &mut Selector = selector.borrow_mut();
|
||||||
s.path(path)?.select_to_value()
|
s.path(path)?.select_as_value()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -312,7 +312,7 @@ pub fn selector_as<T: serde::de::DeserializeOwned>(json: &Value) -> impl FnMut(&
|
|||||||
let mut selector = Selector::new();
|
let mut selector = Selector::new();
|
||||||
let _ = selector.value(json.into());
|
let _ = selector.value(json.into());
|
||||||
move |path: &str| {
|
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<Value,
|
|||||||
/// ```
|
/// ```
|
||||||
pub fn select(json: &Value, path: &str) -> result::Result<Value, String> {
|
pub fn select(json: &Value, path: &str) -> result::Result<Value, String> {
|
||||||
let mut selector = Selector::new();
|
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")]
|
#[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<String, String> {
|
|||||||
Selector::new()
|
Selector::new()
|
||||||
.path(path)?
|
.path(path)?
|
||||||
.value_from_str(json)?
|
.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`.
|
/// 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<T: serde::de::DeserializeOwned>(json: &str, path: &str) -> resu
|
|||||||
Selector::new()
|
Selector::new()
|
||||||
.path(path)?
|
.path(path)?
|
||||||
.value_from_str(json)?
|
.value_from_str(json)?
|
||||||
.select_to()
|
.select_as()
|
||||||
}
|
}
|
@ -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<String, String> {
|
pub fn select_to_str(&self) -> result::Result<String, String> {
|
||||||
|
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<Value, String> {
|
||||||
|
self.select_as_value()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[deprecated(since = "0.1.13", note = "Please use the select_as function instead")]
|
||||||
|
pub fn select_to<T: serde::de::DeserializeOwned>(&self) -> result::Result<T, String> {
|
||||||
|
self.select_as()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn select_as_str(&self) -> result::Result<String, String> {
|
||||||
serde_json::to_string(self.select()?.deref()).map_err(|e| e.to_string())
|
serde_json::to_string(self.select()?.deref()).map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select_to_value(&self) -> result::Result<Value, String> {
|
pub fn select_as_value(&self) -> result::Result<Value, String> {
|
||||||
Ok((&self.select()?).into())
|
Ok((&self.select()?).into())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select_to<T: serde::de::DeserializeOwned>(&self) -> result::Result<T, String> {
|
pub fn select_as<T: serde::de::DeserializeOwned>(&self) -> result::Result<T, String> {
|
||||||
T::deserialize(self.select()?.deref()).map_err(|e| e.to_string())
|
T::deserialize(self.select()?.deref()).map_err(|e| e.to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,6 +14,8 @@ It is Webassembly version of [jsonpath_lib](https://github.com/freestrings/jsonp
|
|||||||
|
|
||||||
### jsonpath.Selector
|
### jsonpath.Selector
|
||||||
|
|
||||||
|
> Selector's selectTo function is deprecated since 0.1.3
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
let jsonObj = {
|
let jsonObj = {
|
||||||
"school": {
|
"school": {
|
||||||
@ -31,19 +33,19 @@ let jsonObj = {
|
|||||||
let selector = new jsonpath.Selector().value(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}];
|
let resultObj = [{"name": "친구3", "age": 30}];
|
||||||
console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj));
|
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}];
|
let resultObj = [{"name": "친구1", "age": 20}, {"name": "친구2", "age": 20}];
|
||||||
console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj));
|
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}];
|
let resultObj = [{"name": "친구5", "age": 20}];
|
||||||
console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj));
|
console.log(JSON.stringify(jsonObj) === JSON.stringify(resultObj));
|
||||||
}
|
}
|
||||||
|
@ -55,12 +55,12 @@ fn into_serde_json<D>(js_value: &JsValue) -> Result<D, String>
|
|||||||
if js_value.is_string() {
|
if js_value.is_string() {
|
||||||
match serde_json::from_str(js_value.as_string().unwrap().as_str()) {
|
match serde_json::from_str(js_value.as_string().unwrap().as_str()) {
|
||||||
Ok(json) => Ok(json),
|
Ok(json) => Ok(json),
|
||||||
Err(e) => Err(format!("{:?}", e))
|
Err(e) => Err(e.to_string())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match js_value.into_serde() {
|
match js_value.into_serde() {
|
||||||
Ok(json) => Ok(json),
|
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)]
|
#[wasm_bindgen(catch, js_name = selectToStr)]
|
||||||
pub fn select_to_str(&mut self) -> result::Result<JsValue, JsValue> {
|
pub fn select_to_str(&mut self) -> result::Result<JsValue, JsValue> {
|
||||||
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<JsValue, JsValue> {
|
||||||
|
let json_str = self.selector.select_as_str()?;
|
||||||
Ok(JsValue::from_str(&json_str))
|
Ok(JsValue::from_str(&json_str))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[wasm_bindgen(catch, js_name = selectTo)]
|
#[wasm_bindgen(catch, js_name = selectTo)]
|
||||||
pub fn select_to(&mut self) -> result::Result<JsValue, JsValue> {
|
pub fn select_to(&mut self) -> result::Result<JsValue, JsValue> {
|
||||||
let ref_value = self.selector.select_to::<RefValue>()
|
self.select_as()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[wasm_bindgen(catch, js_name = selectAs)]
|
||||||
|
pub fn select_as(&mut self) -> result::Result<JsValue, JsValue> {
|
||||||
|
let ref_value = self.selector.select_as::<RefValue>()
|
||||||
.map_err(|e| JsValue::from_str(&e))?;
|
.map_err(|e| JsValue::from_str(&e))?;
|
||||||
Ok(JsValue::from_serde(&ref_value)
|
Ok(JsValue::from_serde(&ref_value)
|
||||||
.map_err(|e| JsValue::from_str(&format!("{:?}", e)))?)
|
.map_err(|e| JsValue::from_str(&format!("{:?}", e)))?)
|
||||||
|
@ -113,6 +113,6 @@ fn selector_struct() {
|
|||||||
let mut selector = jsonpath::Selector::new();
|
let mut selector = jsonpath::Selector::new();
|
||||||
selector.path("$..book[2]").unwrap();
|
selector.path("$..book[2]").unwrap();
|
||||||
selector.value(JsValue::from_str(json_str())).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());
|
assert_eq!(json, target_json());
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user