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

Suggestion to unify as_*() methods under one as_scalar() #82

Open
aldanor opened this issue May 10, 2024 · 1 comment
Open

Suggestion to unify as_*() methods under one as_scalar() #82

aldanor opened this issue May 10, 2024 · 1 comment

Comments

@aldanor
Copy link
Contributor

aldanor commented May 10, 2024

Suggestion:

// impl for bool, &str, i64, u64, Number
trait Scalar<V>: Sealed + Sized {
    fn from_json_value(value: &V) -> Option<Self>;
}

trait JsonValueTrait {
    fn as_scalar<T: Scalar<Self::ValueType>>(&self) -> Option<T> {
        T::from_json_value(self)
    }

    fn get_scalar<T: Scalar<Self::ValueType>, I: Index>(&self, index: I) -> Option<T>;

    fn pointer_scalar<T: Scalar<Self::ValueType>, P: ..>(&self, path: P) -> Option<T>;
}

This would make it easier to build more generic wrappers around this API without too much copy/paste for each particular type.


Note 1: would be nice to have it just as Scalar without it being generic, but it seems that Scalar<Self::ValueType> is inevitable? (because JsonValueTrait may return non-self in get() and pointer()?)


Note 2: for consistency, it might make sense to also have

    fn as_object(&self) -> Option<LazyObject>;
    fn as_array(&self) -> Option<LazyObject>;
@liuq19
Copy link
Collaborator

liuq19 commented May 14, 2024

we use as_xx API for two reasons:

  1. consistent with serde_json as possible
  2. the bool, &str, i64, u64, Number are mostly used, the concise names are more readable

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

No branches or pull requests

2 participants