#Serializing a `Vec` as aJSON array without `serde_json`
44 messages · Page 1 of 1 (latest)
if you assume some things you can probably get away with not using serde at all
it feels very weird to have a single crate for this one functionality in my opinion.
what do you mean?
you have a vec of strings right
yes
debug print it then percent escape it
serde doesnt give you ser/deserialisers
Yeah serde is way overkill here
but for the /search endpoint's facets parameter has this as its example:
[["categories:forge"],["versions:1.17.1"],["project_type:mod"],["license:mit"]]
format!("{:?}",vec).replace('"',"%22"), very naively
so its not always going to be a vec of strings, as seen in this case
how many enpoints do you need to call
It's either use serde and a format crate or don't use serde, pick your poison
is this always a list of one item string lists
Like sure you could reimplement serde_json's logic yourself, but don't
no it can be of any size. this is also valid:
[["versions:1.16.5", "versions:1.17.1"]]
they are just search criteria right
could you give me a crate? i havent heard of a thing existing
do you need to support all of the search criteria
yes they are just search criteria
serde and serde_json
serde_json is the format crate
ohhhhhh i see
serde is the glue
serde_json is the thing that understands Json
serde is basically useless by itself
not everything, but some of the criteria
See the formats section for a big list of crates https://serde.rs/
these ones:
- project_type
- categories (loaders are lumped in with categories in search)
- versions
- client_side
- server_side
and yes they are controlled by the user
are they always all included
maybe create a structure that can represent every possible criteria
like an enum of the criteria?
so at the end i have to use serde_json, right?
no just make a method which converts that struct into a string
ive resorted to this impl
impl<T> Debug for ModrinthVec<T>
where
T: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.inner)
}
}
though i can probably just do something like this:
impl<T> Serialize for ModrinthVec<T>
where
Vec<T>: Debug,
{
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&format!("{:?}", self.inner))
}
}
and remove the Debug impl, im getting this error from my tests:
tried to serialize a value before serializing key
this won't do what you want. this will format! to something like "[[\"versions:1.16.5\"]]" and then serialize that string into json as "\"[[\\\\"versions:1.16.5\\\"]]\""
but why use serde if you dont use serde_json
what does your ModrinthVec look like