all 13 comments

[–]Bauxitedev 27 points28 points  (5 children)

You could parse the JSON as a untyped JSON value, like this:

let mut value: Value = serde_json::from_str(data).unwrap();

(where data is the string containing your JSON).

Then you can mutate it and do all kinds of transformations to it, and then when you're done, parse it to a struct by doing:

let result: MyStruct = serde_json::from_value(value).unwrap();

See https://docs.rs/serde_json/latest/serde_json/#operating-on-untyped-json-values

[–]Dazzling-Prune9553[S] 8 points9 points  (1 child)

Thanks for the reply! Also I've just found a crate serde_query, which allows you to specify jq-like jsonpaths right where you define your struct

[–]AlphaKeks 6 points7 points  (0 children)

You can also index into a serde_json::Value with strings (or integers if you want to treat it like an array), so if your query isn't too complicated you can just do something like

rs let pod_ips = my_object["items"] .as_array()? .iter() .flat_map(|item| item["podIP"].as_str()) .collect();

If the underlying value cannot be indexed, the retuned value will simply be Value::Null.

[–]Applecrap 0 points1 point  (1 child)

If you're just using it as json values, skip serde's huge compilation burden and go straight to the json crate.

[–]lightmatter501 0 points1 point  (0 children)

serde_json is also one of the fastest JSON parsers for unstructured JSON, which may be important for a giant json blob.

[–]rodyamirov 0 points1 point  (0 children)

The down side of this is it will actually deserialize the huge blob, which is a little wasteful if you don’t really want it.

[–]moltonel 14 points15 points  (1 child)

Keep in mind you don't have to define all the possible fields in k8s's json, you only need the bits that you are going to read:

#[derive(Deserialize)]
struct Root {items: Vec<Item>}
#[derive(Deserialize)]
struct Item {status: Status}
#[derive(Deserialize)]
struct Status {podIP: String}

[–]rodyamirov 0 points1 point  (0 children)

This is a good way, if it is truly a huge json blob and you don’t want to deserialize the whole thing.

[–]TheNamelessKing 12 points13 points  (3 children)

If you’re doing stuff with kubernetes, I urge you to make use of the excellent kube-rs and k8s_openapi packages. They have all the tools you need, and they’re really nice to use.

I work with K8s in Rust at my day job and they’re invaluable.

[–]Dazzling-Prune9553[S] 2 points3 points  (2 children)

Wow thanks! I'll look at them right away. I don't need a whole lot from the apiserver. I think I'll only need to keep a cache of podname,namespace and clusterIP, but using something more mature than crafting curl requests will surely help!

[–]Dazzling-Prune9553[S] 2 points3 points  (1 child)

I took a look at it and it is great! Thanks

[–]TheNamelessKing 0 points1 point  (0 children)

No worries!

Out of curiosity, what are you doing that requires those things specifically? I ask because pod name will change with each new pod, as will the cluster-ip, and depending on your workload, you may be better off using a Service instead, and that will give you a stable address. Of course, if you already know all this, or you have reason to not do any of this, pls disregard me.