Code Monkey home page Code Monkey logo

Comments (2)

lganzzzo avatar lganzzzo commented on June 27, 2024 1

Hello @bakeunbin ,

oatpp::Any is what you need.

Here is a simple test - to see how Any works:

  class TestDto : public oatpp::DTO {

    DTO_INIT(TestDto, DTO)

    DTO_FIELD (Any, wdir);
    DTO_FIELD (Any, visib);

  };

...

  void printFields(const oatpp::Object<TestDto>& body) {

    if(body->wdir) {
      if(body->wdir.getStoredType() == oatpp::String::Class::getType()) {
        auto wdir = body->wdir.retrieve<oatpp::String>();
        OATPP_LOGD("TEST", "wdir(string)='%s'", wdir->c_str())
      } else if(body->wdir.getStoredType() == oatpp::UInt64::Class::getType()) {
        auto wdir = body->wdir.retrieve<oatpp::UInt64>();
        OATPP_LOGD("TEST", "wdir(int)=%llu", *wdir)
      } else {
        OATPP_LOGD("TEST", "wdir type is '%s'", body->wdir.getStoredType()->classId.name)
      }
    }

    if(body->visib) {
      if(body->visib.getStoredType() == oatpp::String::Class::getType()) {
        auto visib = body->visib.retrieve<oatpp::String>();
        OATPP_LOGD("TEST", "visib(string)='%s'", visib->c_str())
      } else if(body->visib.getStoredType() == oatpp::UInt64::Class::getType()) {
        auto visib = body->visib.retrieve<oatpp::UInt64>();
        OATPP_LOGD("TEST", "visib(int)=%llu", *visib)
      }else {
        OATPP_LOGD("TEST", "visib type is '%s'", body->visib.getStoredType()->classId.name)
      }
    }

  }

And the test:

 oatpp::parser::json::mapping::ObjectMapper mapper;

  {
    oatpp::String json = R"({"wdir": "VRB", "visib": "10+"})";
    auto body = mapper.readFromString<oatpp::Object<TestDto>>(json);
    printFields(body);
  }

  {
    oatpp::String json = R"({"wdir": 100, "visib": 100})";
    auto body = mapper.readFromString<oatpp::Object<TestDto>>(json);
    printFields(body);
  }

  {
    oatpp::String json = R"({"wdir": 100.1, "visib": 100.1})";
    auto body = mapper.readFromString<oatpp::Object<TestDto>>(json);
    printFields(body);
  }

And the output (oatpp 1.3.0 tag "1.3.0-latest")

 D |2024-05-28 09:46:53 1716878813520526| TEST:wdir(string)='VRB'
 D |2024-05-28 09:46:53 1716878813520532| TEST:visib(string)='10+'
 D |2024-05-28 09:46:53 1716878813520543| TEST:wdir(int)=100
 D |2024-05-28 09:46:53 1716878813520547| TEST:visib(int)=100
 D |2024-05-28 09:46:53 1716878813520557| TEST:wdir type is 'Float64'
 D |2024-05-28 09:46:53 1716878813520561| TEST:visib type is 'Float64'

from oatpp.

bakeunbin avatar bakeunbin commented on June 27, 2024

Thank you, @lganzzzo. I didn't realize it uses 64 bit variable by default; oatpp::Any::retrieve works perfectly fine now.

from oatpp.

Related Issues (20)

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.