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

Modify code to use a generic underline json value #71

Open
MeirShpilraien opened this issue Jul 4, 2021 · 6 comments
Open

Modify code to use a generic underline json value #71

MeirShpilraien opened this issue Jul 4, 2021 · 6 comments

Comments

@MeirShpilraien
Copy link
Contributor

Today the code assumes the underline json structure is serde_json::Value. We have a situation where this is not the case and we need to work with a custom structure. We still want to use this library with our structure. I was wondering if you will accept a PR that changes the code to work with any value that implement a defined trait, this way it can be use with serde_json::Value but with other structures as well. If you agree we can use this issue to define the details and I will be happy to contribute the PR.

@freestrings
Copy link
Owner

I have a question. What do you mean by custom struct?

@MeirShpilraien
Copy link
Contributor Author

I mean that the json structure (in memory) is not a serde_json::Value. Instead, we use our own structure (specifically in our use-case it's even C implemented). Imagine for example that you want some extra metadata per node in the json structure, you can not use serde_json::Value but it is easy to create your own struct and just implement Serialize and Deserialize to be able to use it with serde_json for serialization and deserialization. But now I also want to be able to perform a jsonpath query on this struct and this is why I open this issue :).

I imagine we can define a trait such that each object that wants to be jsonpath "compatible" will need to implement this trait (for sure the implementation for serde_json::Value will come built-in). The trait can define functions like IterValues for array iteration or IterItems for object iteration, it will define a function to get the type of the json object and for sure get primitive values (strings, number, boolean, ...).

For existing users, the API will stay exactly the same, but it will open a door for more users to be able to use this library.

WDYT?

@freestrings
Copy link
Owner

freestrings commented Jul 8, 2021

I didn't understand exactly. Can I understand as below?

let v = TestStruct { ... };
let result = jsonpath::select(&v, "$.name").unwrap();

@MeirShpilraien
Copy link
Contributor Author

Yes, exactly.

@freestrings
Copy link
Owner

That's a good idea. Thanks in advance for the PR. 👍

@mattmart3
Copy link

mattmart3 commented Dec 6, 2021

Maybe this trait could be useful to add (and retrieve) other metadata to the json node structure, such as the reference to the parent node or to the key string of the node. For instance I submitted a PR (#84) that implements the parent operator by the mean of a HashMap to relate each node to its parent. Of course by adding the parent reference to a more generic Node struct the implementation would be much more clean and efficient. Similarly, to implement the key operator (issue #69), one could add the key string reference to each node.
What do you think?

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

No branches or pull requests

3 participants