Skip to content

glz::json_tdouble serialization, how to customize format? #1672

@acking-you

Description

@acking-you

Hello @stephenberry,I may have found another bug.

Descreibe my issue

You can see the code below:

int main() {
    glz::json_t v = {{"test", 1.00}, {"test2", 1}};
    std::cout << *glz::write_json(v);
    return 0;
}

Output is that:

{"test":1,"test2":1}

It seems that glz::json_t can't properly serialize the double type?

My current progress

I found that:

  1. double type will call this overload:
template <class T>
   requires std::convertible_to<T, val_t> && (!std::derived_from<std::decay_t<T>, json_t>)
json_t(T&& val)
{
   data = val;
}
  1. integer type will call this:
template <class T>
   requires std::convertible_to<T, double> && (!std::derived_from<std::decay_t<T>, json_t>) &&
            (!std::convertible_to<T, val_t>)
json_t(T&& val)
{
   data = static_cast<double>(val);
}
  1. double type in struct still not work:
struct vv {
    int t;
    double tt;
};

int main() {
    glz::json_t v = {{"test", 1.00}, {"test2", 1}};

    std::cout << *glz::write_json(vv{.t = 1, .tt = 1.0}) << std::endl;
    return 0;
}
  1. I found a issue Encoding of double variables that happen to be integral #1592 ,and this question has already been raised.

At last,I noticed that you @stephenberry gave the suggestion to use glz::custom to do this, but this should be difficult to do in std::variant, my current approach is to work around this via glz::raw_json, but this may not be a good solution?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions