Comments (2)
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.
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)
- LazyStringMapTemplate::get returns Nullptr wich is not in oatpp.io Documentation
- Please add support for CMake 3.16
- Cannot test if required integer query parameter is empty
- [Question] Is it possible during compilation to write out the generated oas-3.0.0.json file?
- How should I make my process send requests to the target when using OATPP HOT 4
- Stack corruption issue in ConnectionProvider.cpp
- Can not build project successfully
- Is it possible make oatpp::network::tcp::client::ConnectionProvider support bind client IP?
- specific async pattern implementation query HOT 4
- io_uring support request HOT 1
- Compilation error on Windows with MinGW
- Facing linker issue (undefined reference to `oatpp::base::Environment::getComponent) in windows using mingw gcc compiler
- auto objectMapper = oatpp::json::ObjectMapper::createShared(); HOT 2
- Rescheduling coroutines into different worker groups
- 无法启动程序” D:\zxlworkloatpp-starter-master\build\x64\Debug\ALL BUILD" 拒绝访问。 HOT 1
- What is the next plan for this project? HOT 2
- Why not ANSI C++?
- ENDPOINT with PATH as a list of String HOT 4
- oat build failed with Visual Studio 2015
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from oatpp.