Skip to content

Conversion from valarray<double> to json fails to build #702

Closed

Description

@popizdeh

There seems to be a regression from 2.0.8 and 2.1.1. This no longer compiles.

std::valarray<double> v;
nlohmann::json j;
j["test"] = v;

P.S. Is there any chance we can get a release with std::vector fix released?

Activity

  1. theodelrieu commented on Aug 21, 2017

    @theodelrieu
    Contributor

    Oops, this is due to the has_iterator traits that check for a iterator type alias, which valarray doesn't provide. Sorry about that.

    namespace nlohmann
    {
    template <typename T>
    struct adl_serializer<std::valarray<T>>
    {
      static void to_json(json &j, std::valarray<T> const &v)
      {
        detail::external_constructor<detail::value_t::array>::construct(j, v);
      }
    
      static void from_json(json const &j, std::valarray<T> &v)
      {
        // checks omitted, not the best implem, never played with valarray myself
        auto arr = j.get<std::vector<T>>();
        v = std::valarray<T>(arr.data(), arr.size());
      }
    };
    }

    As for the bug fix release, I don't know what @nlohmann thinks about it.

  2. nlohmann commented on Aug 21, 2017

    @nlohmann
    Owner

    We need to fix this and add std::valarray support for the 3.x.x release. I did not plan to create a bug fix release for this.

  3. self-assigned this
    on Aug 22, 2017
  4. added this to the Release 3.0.0 milestone on Aug 22, 2017
  5. nlohmann commented on Aug 23, 2017

    @nlohmann
    Owner

    Fixed with 9fd031b.

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

Metadata

Metadata

Assignees

Labels

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions