Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function to convert serde_json::Value or serde_yaml::Value into regorus::Value #195

Closed
lquerel opened this issue Apr 5, 2024 · 4 comments · Fixed by #196
Closed

Function to convert serde_json::Value or serde_yaml::Value into regorus::Value #195

lquerel opened this issue Apr 5, 2024 · 4 comments · Fixed by #196

Comments

@lquerel
Copy link

lquerel commented Apr 5, 2024

This type of conversion will likely be very common. I don't think it's possible, with the current regorus API, to use serde-transcode (or a similar approach) to perform this conversion efficiently. It might be helpful to add a method to perform this type of transformation.

@anakrish
Copy link
Collaborator

anakrish commented Apr 5, 2024

Would serde_json::from_value and serde_yaml::from_value be sufficient?

use serde_json::json;

fn main() {
    let j:serde_json::Value = json!({
        "fingerprint": "0xF9BA143B95FF6D82",
        "location": "Menlo Park, CA",
        "Hello": [
            1,
            "Foo"
        ]
    });
    println!("{j:#?}");

    let yaml = "
        - 1
        - [0, 0, 0]
        - {x: 1.0, y: 2.0}
    ";
    let k : serde_yaml::Value = serde_yaml::from_str(&yaml).unwrap();
    println!("{k:#?}");

    // Convert serde_json::Value to regorus::Value, consuming it.
    let u: regorus::Value = serde_json::from_value(j).unwrap();
    println!("{u:#?}");

    // Convert serde_yaml::Value to regorus::Value, consuming it.
    let v: regorus::Value = serde_yaml::from_value(k).unwrap();
    println!("{v:#?}");
}

@lquerel
Copy link
Author

lquerel commented Apr 5, 2024

Sorry, and shame on me, I forgot the basics...

@lquerel lquerel closed this as completed Apr 5, 2024
@anakrish
Copy link
Collaborator

anakrish commented Apr 5, 2024

No worries. I'm thinking of adding a From<serde_json::Value> to regorus::Value since serde_json::from_value may not be that intuitive.

@lquerel
Copy link
Author

lquerel commented Apr 5, 2024

That will make things straightforward

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants