AlecTroemel / quickxml_to_serde

Convert between XML JSON using quickxml and serde
MIT License
24 stars 14 forks source link

How to pass json object as parameter to function #16

Closed mzfr closed 3 years ago

mzfr commented 3 years ago

I am new to rust so just wanted to ask:

        let text = std::fs::read_to_string(xmlpath).unwrap();
        let conf = Config::new_with_defaults();
        let json = xml_string_to_json(text.to_owned(), &conf);

This is how I read and changed the XML to json. And not I want to send the json value to another function.

Now when I make a new function like fn parser(jsondata: serde::Value) I get error saying cannot find type Value in crate serde. But the doc of quickxml_to_serde says it changes xml to serde::Value.

Can you please tell me what will be the right type for this?

rimutaka commented 3 years ago

@mzfr , I think Value should come from serde_json crate. See https://docs.serde.rs/serde_json/value/enum.Value.html.

mzfr commented 3 years ago

Yes I tried that as well

fn general_package_info(jsondata: serde_json::Value) {}

But with this I get the following error:

|
38 |         general_package_info(jsondata);
   |                              ^^^^^^^^ expected enum `Value`, found enum `Result`
   |
   = note: expected enum `Value`
              found enum `Result<Value, minidom::error::Error>`
mzfr commented 3 years ago

ok, I fixed that. Didn't had to include anything else in the cargo. Just had to unwrap it.

In the end the thing that worked for me was:

        let text = std::fs::read_to_string(xmlpath).unwrap();
        let conf = Config::new_with_defaults();
        let json = xml_string_to_json(text.to_owned(), &conf);
        another_function(json.unwrap())

And in the another_function we can accept serde_json::Value

fn another_function(jsondata: serde_json::Value)