Coder Social home page Coder Social logo

NaN values about json_dto HOT 4 CLOSED

stiffstream avatar stiffstream commented on August 30, 2024
NaN values

from json_dto.

Comments (4)

eao197 avatar eao197 commented on August 30, 2024

Hi! Thanks for reporting that. I'll take a look.

from json_dto.

eao197 avatar eao197 commented on August 30, 2024

Values like "nan", "inf" and "-inf" are non-standard in JSON. They can be used as non-standard extensions and RapidJson handles them that way:

  • a flag rapidjson::kParseNanAndInfFlag should be passed to from_json function, for example:
const auto my_data = json_dto::from_json<MyData, rapidjson::kParseNanAndInfFlag>(str);
  • RapidJSON recognizes only values NaN, Inf, or Infinitiy. Not nan, Nan, inf or something else. Not "NaN" nor "nan", nor "Nan". It means that {"id":234, "test":"Nan"} can't be parsed by RapidJSON. Only in that form: {"id":234, "test":NaN}.

Because json_dto is just a thin wrapper around RapidJSON it's impossible to parse documents like {"id":234, "test":"Nan"} by json_dto.

However, json_dto can be extended by something like:

struct MyData {
  int id;
  double test;
  ...
  template<typename Io>
  void json_io(Io & io) {
     io & json_dto::mandatory("id", id)
        & json_dto::mandatory(double_extractor{}, "test", test);
  }
};

where double_extractor will be something like:

struct double_extractor {
  double read(const rapidjson::Value & from) const {
     if(from.IsNumber()) return from.GetDouble();
     if(from.IsString()) {
        const auto * str_v = from.GetString();
        if(0 == strcasecmp(str_v, "nan")) return std::numeric_limits<double>::quiet_NaN();
        ...
     }
     throw json_dto::ex_t{"value is not double"};
  }
  void write(double v, rapidjson::Value & to, rapidjson::MemoryPoolAllocator<> &) const {
    to.SetDouble(v);
  }
};

and you have to write such double_extractor by yourself.

from json_dto.

eao197 avatar eao197 commented on August 30, 2024

@qwertynat
Version 0.2.10 with support of custom Reader_Writer types (from an idea described above) is just released. Now you can try to handle non-standard values in JSON document via your Reader_Writer as described here.

from json_dto.

qwertynat avatar qwertynat commented on August 30, 2024

niceone! will have a play with it.

from json_dto.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.