mirror of
https://github.com/oatpp/oatpp.git
synced 2025-04-12 18:50:22 +08:00
Refactor error handling in oatpp::encoding::Hex and oatpp::parser::json::Utils. More API Docs.
This commit is contained in:
parent
9b7289c653
commit
3b64520ff5
src/oatpp
encoding
parser/json
test/oatpp/parser/json/mapping
@ -27,8 +27,6 @@
|
||||
|
||||
namespace oatpp { namespace encoding {
|
||||
|
||||
const char* const Hex::ERROR_UNKNOWN_SYMBOL = "UNKNOWN_SYMBOL";
|
||||
|
||||
const v_char8 Hex::A_D[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
|
||||
/*
|
||||
const v_word16 Hex::A_W16[] = {
|
||||
@ -103,7 +101,7 @@ void Hex::writeWord32(v_word32 value, p_char8 buffer){
|
||||
writeWord16(value, buffer + 4);
|
||||
}
|
||||
|
||||
const char* Hex::readWord16(p_char8 buffer, v_word16& value) {
|
||||
v_int32 Hex::readWord16(p_char8 buffer, v_word16& value) {
|
||||
value = 0;
|
||||
for(v_int32 i = 0; i < 4; i++){
|
||||
v_char8 a = buffer[i];
|
||||
@ -117,10 +115,10 @@ const char* Hex::readWord16(p_char8 buffer, v_word16& value) {
|
||||
return ERROR_UNKNOWN_SYMBOL;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
const char* Hex::readWord32(p_char8 buffer, v_word32& value) {
|
||||
v_int32 Hex::readWord32(p_char8 buffer, v_word32& value) {
|
||||
value = 0;
|
||||
for(v_int32 i = 0; i < 8; i++){
|
||||
v_char8 a = buffer[i];
|
||||
@ -134,7 +132,7 @@ const char* Hex::readWord32(p_char8 buffer, v_word32& value) {
|
||||
return ERROR_UNKNOWN_SYMBOL;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
}}
|
||||
|
@ -31,20 +31,50 @@
|
||||
#include "oatpp/core/base/Environment.hpp"
|
||||
|
||||
namespace oatpp { namespace encoding {
|
||||
|
||||
|
||||
/**
|
||||
* Utility class for hex string encoding/decoding .
|
||||
*/
|
||||
class Hex {
|
||||
public:
|
||||
static const v_char8 A_D[];
|
||||
static const v_word16 A_W16[];
|
||||
public:
|
||||
static const char* const ERROR_UNKNOWN_SYMBOL;
|
||||
/**
|
||||
* Unknown symbol error.
|
||||
*/
|
||||
static constexpr v_int32 ERROR_UNKNOWN_SYMBOL = 1;
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Write value as hex string to buffer.
|
||||
* @param value - value to write.
|
||||
* @param buffer - buffer for resultant string.
|
||||
*/
|
||||
static void writeWord16(v_word16 value, p_char8 buffer);
|
||||
|
||||
/**
|
||||
* Write value as hex string to buffer.
|
||||
* @param value - value to write.
|
||||
* @param buffer - buffer for resultant string.
|
||||
*/
|
||||
static void writeWord32(v_word32 value, p_char8 buffer);
|
||||
|
||||
static const char* readWord16(p_char8 buffer, v_word16& value);
|
||||
static const char* readWord32(p_char8 buffer, v_word32& value);
|
||||
|
||||
/**
|
||||
* Parse 4-char hex string to int16.
|
||||
* @param buffer - buffer containing string to parse.
|
||||
* @param value - out parameter. Resultant value.
|
||||
* @return - 0 on success. Negative value on failure.
|
||||
*/
|
||||
static v_int32 readWord16(p_char8 buffer, v_word16& value);
|
||||
|
||||
/**
|
||||
* Parse 8-char hex string to int32.
|
||||
* @param buffer - buffer containing string to parse.
|
||||
* @param value - out parameter. Resultant value.
|
||||
* @return - 0 on success. Negative value on failure.
|
||||
*/
|
||||
static v_int32 readWord32(p_char8 buffer, v_word32& value);
|
||||
|
||||
};
|
||||
|
||||
|
@ -29,10 +29,6 @@
|
||||
|
||||
namespace oatpp { namespace parser { namespace json{
|
||||
|
||||
const char* const Utils::ERROR_INVALID_ESCAPED_CHAR = "ERROR_INVALID_ESCAPED_CHAR";
|
||||
const char* const Utils::ERROR_INVALID_SURROGATE_PAIR = "ERROR_INVALID_SURROGATE_PAIR";
|
||||
const char* const Utils::ERROR_PARSER_QUOTE_EXPECTED = "'\"' - EXPECTED";
|
||||
|
||||
v_int32 Utils::calcEscapedStringSize(p_char8 data, v_int32 size, v_int32& safeSize) {
|
||||
v_int32 result = 0;
|
||||
v_int32 i = 0;
|
||||
@ -77,8 +73,8 @@ v_int32 Utils::calcEscapedStringSize(p_char8 data, v_int32 size, v_int32& safeSi
|
||||
return result;
|
||||
}
|
||||
|
||||
v_int32 Utils::calcUnescapedStringSize(p_char8 data, v_int32 size, const char* & error, v_int32& errorPosition) {
|
||||
error = nullptr;
|
||||
v_int32 Utils::calcUnescapedStringSize(p_char8 data, v_int32 size, v_int32& errorCode, v_int32& errorPosition) {
|
||||
errorCode = 0;
|
||||
v_int32 result = 0;
|
||||
v_int32 i = 0;
|
||||
|
||||
@ -87,7 +83,7 @@ v_int32 Utils::calcUnescapedStringSize(p_char8 data, v_int32 size, const char* &
|
||||
if(a == '\\'){
|
||||
|
||||
if(i + 1 == size){
|
||||
error = ERROR_INVALID_ESCAPED_CHAR;
|
||||
errorCode = ERROR_CODE_INVALID_ESCAPED_CHAR;
|
||||
errorPosition = i;
|
||||
return 0;
|
||||
}
|
||||
@ -100,20 +96,20 @@ v_int32 Utils::calcUnescapedStringSize(p_char8 data, v_int32 size, const char* &
|
||||
} else if(b == 'u'){
|
||||
|
||||
if(i + 6 > size){
|
||||
error = ERROR_INVALID_ESCAPED_CHAR;
|
||||
errorCode = ERROR_CODE_INVALID_ESCAPED_CHAR;
|
||||
errorPosition = i;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(data[i + 2] == '+') { // not JSON standard case
|
||||
if(i + 11 > size){
|
||||
error = ERROR_INVALID_ESCAPED_CHAR;
|
||||
errorCode = ERROR_CODE_INVALID_ESCAPED_CHAR;
|
||||
errorPosition = i;
|
||||
return 0;
|
||||
}
|
||||
v_word32 code;
|
||||
error = encoding::Hex::readWord32(&data[i + 3], code);
|
||||
if(error != nullptr){
|
||||
errorCode = encoding::Hex::readWord32(&data[i + 3], code);
|
||||
if(errorCode != 0){
|
||||
errorPosition = i + 3;
|
||||
return 0;
|
||||
}
|
||||
@ -121,21 +117,21 @@ v_int32 Utils::calcUnescapedStringSize(p_char8 data, v_int32 size, const char* &
|
||||
result += encoding::Unicode::getUtf8CharSequenceLengthForCode(code);
|
||||
} else {
|
||||
v_word16 code;
|
||||
error = encoding::Hex::readWord16(&data[i + 2], code);
|
||||
if(error != nullptr){
|
||||
errorCode = encoding::Hex::readWord16(&data[i + 2], code);
|
||||
if(errorCode != 0){
|
||||
errorPosition = i + 2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(code >= 0xD800 && code <= 0xDBFF){
|
||||
if(i + 12 > size){
|
||||
error = ERROR_INVALID_SURROGATE_PAIR;
|
||||
errorCode = ERROR_CODE_INVALID_SURROGATE_PAIR;
|
||||
errorPosition = i;
|
||||
return 0;
|
||||
}
|
||||
v_word16 low;
|
||||
error = encoding::Hex::readWord16(&data[i + 8], low);
|
||||
if(error != nullptr){
|
||||
errorCode = encoding::Hex::readWord16(&data[i + 8], low);
|
||||
if(errorCode != 0){
|
||||
errorPosition = i + 8;
|
||||
return 0;
|
||||
}
|
||||
@ -145,7 +141,7 @@ v_int32 Utils::calcUnescapedStringSize(p_char8 data, v_int32 size, const char* &
|
||||
i += 12;
|
||||
result += encoding::Unicode::getUtf8CharSequenceLengthForCode(bigCode);
|
||||
} else {
|
||||
error = ERROR_INVALID_SURROGATE_PAIR;
|
||||
errorCode = ERROR_CODE_INVALID_SURROGATE_PAIR;
|
||||
errorPosition = i;
|
||||
return 0;
|
||||
}
|
||||
@ -157,7 +153,7 @@ v_int32 Utils::calcUnescapedStringSize(p_char8 data, v_int32 size, const char* &
|
||||
}
|
||||
|
||||
} else {
|
||||
error = ERROR_INVALID_ESCAPED_CHAR;
|
||||
errorCode = ERROR_CODE_INVALID_ESCAPED_CHAR;
|
||||
errorPosition = i;
|
||||
return 0;
|
||||
}
|
||||
@ -322,10 +318,10 @@ void Utils::unescapeStringToBuffer(p_char8 data, v_int32 size, p_char8 resultDat
|
||||
|
||||
}
|
||||
|
||||
oatpp::String Utils::unescapeString(p_char8 data, v_int32 size, const char* & error, v_int32& errorPosition) {
|
||||
oatpp::String Utils::unescapeString(p_char8 data, v_int32 size, v_int32& errorCode, v_int32& errorPosition) {
|
||||
|
||||
v_int32 unescapedSize = calcUnescapedStringSize(data, size, error, errorPosition);
|
||||
if(error != nullptr){
|
||||
v_int32 unescapedSize = calcUnescapedStringSize(data, size, errorCode, errorPosition);
|
||||
if(errorCode != 0){
|
||||
return nullptr;
|
||||
}
|
||||
auto result = String(unescapedSize);
|
||||
@ -338,10 +334,10 @@ oatpp::String Utils::unescapeString(p_char8 data, v_int32 size, const char* & er
|
||||
|
||||
}
|
||||
|
||||
std::string Utils::unescapeStringToStdString(p_char8 data, v_int32 size, const char* & error, v_int32& errorPosition){
|
||||
std::string Utils::unescapeStringToStdString(p_char8 data, v_int32 size, v_int32& errorCode, v_int32& errorPosition){
|
||||
|
||||
v_int32 unescapedSize = calcUnescapedStringSize(data, size, error, errorPosition);
|
||||
if(error != nullptr){
|
||||
v_int32 unescapedSize = calcUnescapedStringSize(data, size, errorCode, errorPosition);
|
||||
if(errorCode != 0){
|
||||
return "";
|
||||
}
|
||||
std::string result;
|
||||
@ -394,11 +390,11 @@ oatpp::String Utils::parseString(ParsingCaret& caret) {
|
||||
|
||||
v_int32 pos = caret.getPosition();
|
||||
|
||||
const char* error;
|
||||
v_int32 errorCode;
|
||||
v_int32 errorPosition;
|
||||
auto result = unescapeString(data, size, error, errorPosition);
|
||||
if(error != nullptr){
|
||||
caret.setError("[oatpp::parser::json::Utils::parseString()]: Error. Call to unescapeString() failed");
|
||||
auto result = unescapeString(data, size, errorCode, errorPosition);
|
||||
if(errorCode != 0){
|
||||
caret.setError("[oatpp::parser::json::Utils::parseString()]: Error. Call to unescapeString() failed", errorCode);
|
||||
caret.setPosition(pos + errorPosition);
|
||||
} else {
|
||||
caret.setPosition(pos + size + 1);
|
||||
@ -421,11 +417,11 @@ std::string Utils::parseStringToStdString(ParsingCaret& caret){
|
||||
|
||||
v_int32 pos = caret.getPosition();
|
||||
|
||||
const char* error;
|
||||
v_int32 errorCode;
|
||||
v_int32 errorPosition;
|
||||
const std::string& result = unescapeStringToStdString(data, size, error, errorPosition);
|
||||
if(error != nullptr){
|
||||
caret.setError("[oatpp::parser::json::Utils::parseStringToStdString()]: Error. Call to unescapeStringToStdString() failed");
|
||||
const std::string& result = unescapeStringToStdString(data, size, errorCode, errorPosition);
|
||||
if(errorCode != 0){
|
||||
caret.setError("[oatpp::parser::json::Utils::parseStringToStdString()]: Error. Call to unescapeStringToStdString() failed", errorCode);
|
||||
caret.setPosition(pos + errorPosition);
|
||||
} else {
|
||||
caret.setPosition(pos + size + 1);
|
||||
|
@ -31,38 +31,27 @@
|
||||
#include <string>
|
||||
|
||||
namespace oatpp { namespace parser { namespace json{
|
||||
|
||||
|
||||
/**
|
||||
* Utility class for json serializer/deserializer.
|
||||
* Used by &id:oatpp::parser::json::mapping::Serializer;, &id:oatpp::parser::json::mapping::Deserializer;.
|
||||
*/
|
||||
class Utils {
|
||||
public:
|
||||
|
||||
/**
|
||||
* ERROR_INVALID_ESCAPED_CHAR = "ERROR_INVALID_ESCAPED_CHAR"
|
||||
*/
|
||||
static const char* const ERROR_INVALID_ESCAPED_CHAR;
|
||||
|
||||
/**
|
||||
* ERROR_INVALID_SURROGATE_PAIR = "ERROR_INVALID_SURROGATE_PAIR"
|
||||
*/
|
||||
static const char* const ERROR_INVALID_SURROGATE_PAIR;
|
||||
|
||||
/**
|
||||
* ERROR_PARSER_QUOTE_EXPECTED = "'\"' - EXPECTED"
|
||||
*/
|
||||
static const char* const ERROR_PARSER_QUOTE_EXPECTED;
|
||||
|
||||
/**
|
||||
* ERROR_CODE_INVALID_ESCAPED_CHAR = 1
|
||||
* ERROR_CODE_INVALID_ESCAPED_CHAR
|
||||
*/
|
||||
static constexpr v_int32 ERROR_CODE_INVALID_ESCAPED_CHAR = 1;
|
||||
|
||||
/**
|
||||
* ERROR_CODE_INVALID_SURROGATE_PAIR = 2
|
||||
* ERROR_CODE_INVALID_SURROGATE_PAIR
|
||||
*/
|
||||
static constexpr v_int32 ERROR_CODE_INVALID_SURROGATE_PAIR = 2;
|
||||
|
||||
/**
|
||||
* "'\"' - EXPECTED"
|
||||
* ERROR_CODE_PARSER_QUOTE_EXPECTED = 3
|
||||
* '\\' - EXPECTED"
|
||||
* ERROR_CODE_PARSER_QUOTE_EXPECTED
|
||||
*/
|
||||
static constexpr v_int32 ERROR_CODE_PARSER_QUOTE_EXPECTED = 3;
|
||||
|
||||
@ -72,19 +61,65 @@ public:
|
||||
private:
|
||||
static v_int32 escapeUtf8Char(p_char8 sequence, p_char8 buffer);
|
||||
static v_int32 calcEscapedStringSize(p_char8 data, v_int32 size, v_int32& safeSize);
|
||||
static v_int32 calcUnescapedStringSize(p_char8 data, v_int32 size, const char* & error, v_int32& errorPosition);
|
||||
static v_int32 calcUnescapedStringSize(p_char8 data, v_int32 size, v_int32& errorCode, v_int32& errorPosition);
|
||||
static void unescapeStringToBuffer(p_char8 data, v_int32 size, p_char8 resultData);
|
||||
static p_char8 preparseString(ParsingCaret& caret, v_int32& size);
|
||||
public:
|
||||
|
||||
/**
|
||||
* if(copyAsOwnData == false && escapedString == initialString) then result string will point to initial data
|
||||
* Escape string as for json standard. <br>
|
||||
* *Note:* if(copyAsOwnData == false && escapedString == initialString) then result string will point to initial data.
|
||||
* @param data - pointer to string to escape.
|
||||
* @param size - data size.
|
||||
* @param copyAsOwnData - see &id:oatpp::base::StrBuffer::StrBuffer;.
|
||||
* @return - &id:oatpp::String;.
|
||||
*/
|
||||
static String escapeString(p_char8 data, v_int32 size, bool copyAsOwnData = true);
|
||||
static String unescapeString(p_char8 data, v_int32 size, const char* & error, v_int32& errorPosition);
|
||||
static std::string unescapeStringToStdString(p_char8 data, v_int32 size,
|
||||
const char* & error, v_int32& errorPosition);
|
||||
|
||||
|
||||
/**
|
||||
* Unescape string as for json standard.
|
||||
* @param data - pointer to string to unescape.
|
||||
* @param size - data size.
|
||||
* @param errorCode - out parameter. Error code <br>
|
||||
* *One of:*<br>
|
||||
* <ul>
|
||||
* <li>&l:Utils::ERROR_CODE_INVALID_ESCAPED_CHAR;</li>
|
||||
* <li>&l:Utils::ERROR_CODE_INVALID_SURROGATE_PAIR;</li>
|
||||
* <li>&l:Utils::ERROR_CODE_PARSER_QUOTE_EXPECTED;</li>
|
||||
* </ul>
|
||||
* @param errorPosition - out parameter. Error position in data.
|
||||
* @return - &id:oatpp::String;.
|
||||
*/
|
||||
static String unescapeString(p_char8 data, v_int32 size, v_int32& errorCode, v_int32& errorPosition);
|
||||
|
||||
/**
|
||||
* Same as &l:Utils::unescapeString (); but return `std::string`.
|
||||
* @param data - pointer to string to unescape.
|
||||
* @param size - data size.
|
||||
* @param errorCode - out parameter. Error code <br>
|
||||
* *One of:*<br>
|
||||
* <ul>
|
||||
* <li>&l:Utils::ERROR_CODE_INVALID_ESCAPED_CHAR;</li>
|
||||
* <li>&l:Utils::ERROR_CODE_INVALID_SURROGATE_PAIR;</li>
|
||||
* <li>&l:Utils::ERROR_CODE_PARSER_QUOTE_EXPECTED;</li>
|
||||
* </ul>
|
||||
* @param errorPosition - out parameter. Error position in data.
|
||||
* @return - &id:oatpp::String;.
|
||||
*/
|
||||
static std::string unescapeStringToStdString(p_char8 data, v_int32 size, v_int32& errorCode, v_int32& errorPosition);
|
||||
|
||||
/**
|
||||
* Parse string enclosed in `"<string>"`.
|
||||
* @param caret - &id:oatpp::parser::Caret;.
|
||||
* @return - &id:oatpp::String;.
|
||||
*/
|
||||
static String parseString(ParsingCaret& caret);
|
||||
|
||||
/**
|
||||
* Parse string enclosed in `"<string>"`.
|
||||
* @param caret - &id:oatpp::parser::Caret;.
|
||||
* @return - `std::string`.
|
||||
*/
|
||||
static std::string parseStringToStdString(ParsingCaret& caret);
|
||||
|
||||
};
|
||||
|
@ -36,7 +36,11 @@
|
||||
#include "oatpp/core/Types.hpp"
|
||||
|
||||
namespace oatpp { namespace parser { namespace json { namespace mapping {
|
||||
|
||||
|
||||
/**
|
||||
* Json deserializer.
|
||||
* Deserializes json to oatpp DTO object. See [Data Transfer Object(DTO) component](https://oatpp.io/docs/components/dto/).
|
||||
*/
|
||||
class Deserializer {
|
||||
public:
|
||||
typedef oatpp::data::mapping::type::Type Type;
|
||||
@ -58,17 +62,31 @@ private:
|
||||
typedef oatpp::data::mapping::type::ListMap<String, AbstractObjectWrapper> AbstractListMap;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Deserializer config.
|
||||
*/
|
||||
class Config : public oatpp::base::Countable {
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
Config()
|
||||
{}
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Create shared Config.
|
||||
* @return - `std::shared_ptr` to Config.
|
||||
*/
|
||||
static std::shared_ptr<Config> createShared(){
|
||||
return std::make_shared<Config>();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Do not fail if unknown field is found in json.
|
||||
* "unknown field" is the one which is not present in DTO object class.
|
||||
*/
|
||||
bool allowUnknownFields = true;
|
||||
|
||||
};
|
||||
@ -151,7 +169,14 @@ private:
|
||||
const std::shared_ptr<Config>& config);
|
||||
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Deserialize json to oatpp DTO object.
|
||||
* @param caret - &id:oatpp::parser::Caret;.
|
||||
* @param config - &l:Deserializer::Config;.
|
||||
* @param type - &id:oatpp::data::mapping::type::Type;.
|
||||
* @return - &id:oatpp::data::mapping::type::AbstractObjectWrapper; containing deserialized object.
|
||||
*/
|
||||
static AbstractObjectWrapper deserialize(oatpp::parser::Caret& caret,
|
||||
const std::shared_ptr<Config>& config,
|
||||
const Type* const type) {
|
||||
|
@ -24,3 +24,28 @@
|
||||
|
||||
#include "ObjectMapper.hpp"
|
||||
|
||||
namespace oatpp { namespace parser { namespace json { namespace mapping {
|
||||
|
||||
ObjectMapper::ObjectMapper(const std::shared_ptr<Serializer::Config>& pSerializerConfig,
|
||||
const std::shared_ptr<Deserializer::Config>& pDeserializerConfig)
|
||||
: oatpp::data::mapping::ObjectMapper(getMapperInfo())
|
||||
, serializerConfig(pSerializerConfig)
|
||||
, deserializerConfig(pDeserializerConfig)
|
||||
{}
|
||||
|
||||
std::shared_ptr<ObjectMapper> ObjectMapper::createShared(const std::shared_ptr<Serializer::Config>& serializerConfig,
|
||||
const std::shared_ptr<Deserializer::Config>& deserializerConfig){
|
||||
return std::make_shared<ObjectMapper>(serializerConfig, deserializerConfig);
|
||||
}
|
||||
|
||||
void ObjectMapper::write(const std::shared_ptr<oatpp::data::stream::OutputStream>& stream,
|
||||
const oatpp::data::mapping::type::AbstractObjectWrapper& variant) const {
|
||||
Serializer::serialize(stream, variant, serializerConfig);
|
||||
}
|
||||
|
||||
oatpp::data::mapping::type::AbstractObjectWrapper ObjectMapper::read(oatpp::parser::Caret& caret,
|
||||
const oatpp::data::mapping::type::Type* const type) const {
|
||||
return Deserializer::deserialize(caret, deserializerConfig, type);
|
||||
}
|
||||
|
||||
}}}}
|
@ -31,7 +31,12 @@
|
||||
#include "oatpp/core/data/mapping/ObjectMapper.hpp"
|
||||
|
||||
namespace oatpp { namespace parser { namespace json { namespace mapping {
|
||||
|
||||
|
||||
/**
|
||||
* Json ObjectMapper. Serialized/Deserializes oatpp DTO objects to/from JSON.
|
||||
* See [Data Transfer Object(DTO) component](https://oatpp.io/docs/components/dto/). <br>
|
||||
* Extends &id:oatpp::base::Countable;, &id:oatpp::data::mapping::ObjectMapper;.
|
||||
*/
|
||||
class ObjectMapper : public oatpp::base::Countable, public oatpp::data::mapping::ObjectMapper {
|
||||
private:
|
||||
static Info& getMapperInfo() {
|
||||
@ -39,32 +44,50 @@ private:
|
||||
return info;
|
||||
}
|
||||
public:
|
||||
ObjectMapper(const std::shared_ptr<Serializer::Config>& pSerializerConfig,
|
||||
const std::shared_ptr<Deserializer::Config>& pDeserializerConfig)
|
||||
: oatpp::data::mapping::ObjectMapper(getMapperInfo())
|
||||
, serializerConfig(pSerializerConfig)
|
||||
, deserializerConfig(pDeserializerConfig)
|
||||
{}
|
||||
/**
|
||||
* Constructor.
|
||||
* @param pSerializerConfig - &id:oatpp::parser::json::mapping::Serializer::Config;.
|
||||
* @param pDeserializerConfig - &id:oatpp::parser::json::mapping::Deserializer::Config;.
|
||||
*/
|
||||
ObjectMapper(const std::shared_ptr<Serializer::Config>& pSerializerConfig = Serializer::Config::createShared(),
|
||||
const std::shared_ptr<Deserializer::Config>& pDeserializerConfig = Deserializer::Config::createShared());
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Create shared ObjectMapper.
|
||||
* @param serializerConfig - &id:oatpp::parser::json::mapping::Serializer::Config;.
|
||||
* @param deserializerConfig - &id:oatpp::parser::json::mapping::Deserializer::Config;.
|
||||
* @return - `std::shared_ptr` to ObjectMapper.
|
||||
*/
|
||||
static std::shared_ptr<ObjectMapper>
|
||||
createShared(const std::shared_ptr<Serializer::Config>& serializerConfig = Serializer::Config::createShared(),
|
||||
const std::shared_ptr<Deserializer::Config>& deserializerConfig = Deserializer::Config::createShared()){
|
||||
return std::make_shared<ObjectMapper>(serializerConfig, deserializerConfig);
|
||||
}
|
||||
|
||||
const std::shared_ptr<Deserializer::Config>& deserializerConfig = Deserializer::Config::createShared());
|
||||
|
||||
/**
|
||||
* Implementation of &id:oatpp::data::mapping::ObjectMapper::write;.
|
||||
* @param stream - stream to write serializerd data to &id:oatpp::data::stream::OutputStream;.
|
||||
* @param variant - object to serialize &id:oatpp::data::mapping::type::AbstractObjectWrapper;.
|
||||
*/
|
||||
void write(const std::shared_ptr<oatpp::data::stream::OutputStream>& stream,
|
||||
const oatpp::data::mapping::type::AbstractObjectWrapper& variant) const override {
|
||||
Serializer::serialize(stream, variant, serializerConfig);
|
||||
}
|
||||
|
||||
oatpp::data::mapping::type::AbstractObjectWrapper
|
||||
read(oatpp::parser::Caret& caret,
|
||||
const oatpp::data::mapping::type::Type* const type) const override {
|
||||
return Deserializer::deserialize(caret, deserializerConfig, type);
|
||||
}
|
||||
|
||||
const oatpp::data::mapping::type::AbstractObjectWrapper& variant) const override;
|
||||
|
||||
/**
|
||||
* Implementation of &id:oatpp::data::mapping::ObjectMapper::read;.
|
||||
* @param caret - &id:oatpp::parser::Caret;.
|
||||
* @param type - type of resultant object &id:oatpp::data::mapping::type::Type;.
|
||||
* @return - &id:oatpp::data::mapping::type::AbstractObjectWrapper; holding resultant object.
|
||||
*/
|
||||
oatpp::data::mapping::type::AbstractObjectWrapper read(oatpp::parser::Caret& caret,
|
||||
const oatpp::data::mapping::type::Type* const type) const override;
|
||||
|
||||
/**
|
||||
* Serializer config.
|
||||
*/
|
||||
std::shared_ptr<Serializer::Config> serializerConfig;
|
||||
|
||||
/**
|
||||
* Deserializer config.
|
||||
*/
|
||||
std::shared_ptr<Deserializer::Config> deserializerConfig;
|
||||
|
||||
};
|
||||
|
@ -38,7 +38,11 @@
|
||||
#include "oatpp/core/Types.hpp"
|
||||
|
||||
namespace oatpp { namespace parser { namespace json { namespace mapping {
|
||||
|
||||
|
||||
/**
|
||||
* Json Serializer.
|
||||
* Serializes oatpp DTO object to json. See [Data Transfer Object(DTO) component](https://oatpp.io/docs/components/dto/).
|
||||
*/
|
||||
class Serializer {
|
||||
public:
|
||||
typedef oatpp::data::mapping::type::Type Type;
|
||||
@ -56,18 +60,37 @@ public:
|
||||
typedef oatpp::data::mapping::type::ListMap<String, AbstractObjectWrapper> AbstractFieldsMap;
|
||||
|
||||
public:
|
||||
|
||||
/**
|
||||
* Serializer config.
|
||||
*/
|
||||
class Config : public oatpp::base::Countable {
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
Config()
|
||||
{}
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Create shared config.
|
||||
* @return - `std::shared_ptr` to Config.
|
||||
*/
|
||||
static std::shared_ptr<Config> createShared(){
|
||||
return std::make_shared<Config>();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Include fields with value == nullptr into serialized json.
|
||||
*/
|
||||
bool includeNullFields = true;
|
||||
|
||||
/**
|
||||
* If `true` - insert string `"<unknown-type>"` in json field value in case unknown field found.
|
||||
* Fail if `false`.
|
||||
* Known types for this serializer are:<br>
|
||||
* (String, Int8, Int16, Int32, Int64, Float32, Float64, Boolean, DTOs, List, Fields).
|
||||
*/
|
||||
bool throwOnUnknownTypes = true;
|
||||
|
||||
};
|
||||
@ -93,7 +116,16 @@ private:
|
||||
static void writeValue(oatpp::data::stream::OutputStream* stream, const AbstractObjectWrapper& polymorph, const std::shared_ptr<Config>& config);
|
||||
|
||||
public:
|
||||
|
||||
|
||||
/**
|
||||
* Serialize DTO object to stream.
|
||||
* @param stream - stream to write serialized object to. &id:oatpp::data::stream::OutputStream;. <br>
|
||||
* **WARNING** Serializer currently does't check call results of &id:oatpp::data::stream::OutputStream::write;.
|
||||
* Meaning that Serializer does not ensures that all data has been transferred successfully. <br>
|
||||
* **USE** output stream with guarantee that all data being successfully written. Like &id:oatpp::data::stream::ChunkedBuffer;.
|
||||
* @param polymorph - DTO object to serialize.
|
||||
* @param config - &l:Serializer::Config;.
|
||||
*/
|
||||
static void serialize(const std::shared_ptr<oatpp::data::stream::OutputStream>& stream,
|
||||
const oatpp::data::mapping::type::AbstractObjectWrapper& polymorph,
|
||||
const std::shared_ptr<Config>& config){
|
||||
|
@ -164,8 +164,7 @@ void DTOMapperTest::onRun(){
|
||||
OATPP_LOGD(TAG, "...");
|
||||
OATPP_LOGD(TAG, "...");
|
||||
OATPP_LOGD(TAG, "...");
|
||||
|
||||
auto config = oatpp::parser::json::mapping::Deserializer::Config::createShared();
|
||||
|
||||
oatpp::parser::Caret caret(result);
|
||||
auto obj = mapper->readFromCaret<Test>(caret);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user