#Serializing a `Vec` as aJSON array without `serde_json`

44 messages · Page 1 of 1 (latest)

naive sand
#

serde alone only really describes the API, you need a crate to actually implement it. Why don't you want to include serde_json?

shy igloo
#

if you assume some things you can probably get away with not using serde at all

shell cargo
#

it feels very weird to have a single crate for this one functionality in my opinion.

shy igloo
#

you have a vec of strings right

shell cargo
#

yes

shy igloo
#

debug print it then percent escape it

shy igloo
sturdy snow
#

Yeah serde is way overkill here

shell cargo
#

but for the /search endpoint's facets parameter has this as its example:

[["categories:forge"],["versions:1.17.1"],["project_type:mod"],["license:mit"]]
shy igloo
shell cargo
shy igloo
#

how many enpoints do you need to call

sturdy snow
shy igloo
sturdy snow
shell cargo
shy igloo
#

they are just search criteria right

shell cargo
shy igloo
#

do you need to support all of the search criteria

shell cargo
shy igloo
#

serde_json is the format crate

shell cargo
sturdy snow
#

serde is the glue

#

serde_json is the thing that understands Json

#

serde is basically useless by itself

shell cargo
shy igloo
#

which ones

#

and are they controlled by a user

#

or by your binary

sturdy snow
shell cargo
#

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
shy igloo
#

are they always all included

#

maybe create a structure that can represent every possible criteria

shell cargo
shy igloo
#

or a struct

#

depending on what criteria is possible

shell cargo
#

so at the end i have to use serde_json, right?

shy igloo
#

no just make a method which converts that struct into a string

shell cargo
#

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

crisp epoch
#

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\\\"]]\""

shy igloo
#

what does your ModrinthVec look like