Skip to content

Commit

Permalink
add ignore type option
Browse files Browse the repository at this point in the history
  • Loading branch information
yksten committed Dec 4, 2021
1 parent fabe1ea commit 62fd407
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 34 deletions.
4 changes: 2 additions & 2 deletions example/exampleData.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace example {
};

struct data {
data() : e(ET2), b(true), i(-25), ui(253), i64(-847), ui64(3647457), f(253.28503), db(2535.78925) {}
data() : e(ET2), b(true), i(-25), ui(253), i64(-847), ui64(3647457), f(253.28503), db(2535.78925), str("1111") {}

EnumType e;
bool b;
Expand Down Expand Up @@ -56,7 +56,7 @@ namespace example {

template<typename T>
void serialize(T& t) {
SERIALIZE(t, e, i, ui, i64, ui64, f, db, str);
SERIALIZE(t, e, b, i, ui, i64, ui64, f, db, str);
}
};

Expand Down
20 changes: 10 additions & 10 deletions include/struct2x/json/GenericReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,19 @@ namespace custom {
return NULL;
}

inline uint32_t GetArraySize(const GenericValue* parent) {
uint32_t size = 0;
if (parent && parent->type == VALUE_ARRAY) {
for (const GenericValue* child = parent->child; child; child = child->next) {
++size;
}
}
return size;
}
// inline uint32_t GetArraySize(const GenericValue* parent) {
// uint32_t size = 0;
// if (parent && parent->type == VALUE_ARRAY) {
// for (const GenericValue* child = parent->child; child; child = child->next) {
// ++size;
// }
// }
// return size;
// }

inline uint32_t GetObjectSize(const GenericValue* parent) {
uint32_t size = 0;
if (parent && parent->type == VALUE_OBJECT) {
if (parent/* && parent->type == VALUE_OBJECT*/) {
for (const GenericValue* child = parent->child; child; child = child->next) {
++size;
}
Expand Down
64 changes: 42 additions & 22 deletions include/struct2x/json/decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ namespace struct2x {
}

class JSONDecoder {
bool _gnoreItemType;
custom::GenericReader _reader;
const custom::GenericValue* _cur;
public:
FORCEINLINE JSONDecoder(const char* str, uint32_t length) : _cur(_reader.Parse(str, length)) {
FORCEINLINE JSONDecoder(const char* str, uint32_t length, bool gnoreType = false) : _gnoreItemType(gnoreType), _cur(_reader.Parse(str, length)) {
assert(_cur);
}

Expand Down Expand Up @@ -133,7 +134,7 @@ namespace struct2x {
const custom::GenericValue* parent = _cur;
_cur = custom::GetObjectItem(_cur, name);
if (_cur) {
uint32_t size = custom::GetArraySize(_cur);
uint32_t size = custom::GetObjectSize(_cur);
if (size) {
value.resize(size);
}
Expand Down Expand Up @@ -170,70 +171,63 @@ namespace struct2x {

FORCEINLINE void decodeValue(const char* name, bool& value, bool* pHas) {
const custom::GenericValue* item = custom::GetObjectItem(_cur, name);
if (item && item->type == custom::VALUE_BOOL) {
if (item->valueSize == 4) {
value = true;
if (pHas) *pHas = true;
} else if (item->valueSize == 5) {
value = false;
if (pHas) *pHas = true;
} else {
assert(false);
}
if (item && checkItemType(*item, custom::VALUE_BOOL)) {
value = item2Bool(*item);
if (pHas) *pHas = true;
}
}

FORCEINLINE void decodeValue(const char* name, uint32_t& value, bool* pHas) {
const custom::GenericValue* item = custom::GetObjectItem(_cur, name);
if (item && item->type == custom::VALUE_NUMBER) {
if (item && checkItemType(*item, custom::VALUE_NUMBER)) {
value = (uint32_t)custom::GenericReader::convertUint(item->value, item->valueSize);
if (pHas) *pHas = true;
}
}

FORCEINLINE void decodeValue(const char* name, int32_t& value, bool* pHas) {
const custom::GenericValue* item = custom::GetObjectItem(_cur, name);
if (item && item->type == custom::VALUE_NUMBER) {
if (item && checkItemType(*item, custom::VALUE_NUMBER)) {
value = (int32_t)custom::GenericReader::convertInt(item->value, item->valueSize);
if (pHas) *pHas = true;
}
}

FORCEINLINE void decodeValue(const char* name, uint64_t& value, bool* pHas) {
const custom::GenericValue* item = custom::GetObjectItem(_cur, name);
if (item && item->type == custom::VALUE_NUMBER) {
if (item && checkItemType(*item, custom::VALUE_NUMBER)) {
value = custom::GenericReader::convertUint(item->value, item->valueSize);
if (pHas) *pHas = true;
}
}

FORCEINLINE void decodeValue(const char* name, int64_t& value, bool* pHas) {
const custom::GenericValue* item = custom::GetObjectItem(_cur, name);
if (item && item->type == custom::VALUE_NUMBER) {
if (item && checkItemType(*item, custom::VALUE_NUMBER)) {
value = custom::GenericReader::convertInt(item->value, item->valueSize);
if (pHas) *pHas = true;
}
}

FORCEINLINE void decodeValue(const char* name, float& value, bool* pHas) {
const custom::GenericValue* item = custom::GetObjectItem(_cur, name);
if (item && item->type == custom::VALUE_NUMBER) {
if (item && checkItemType(*item, custom::VALUE_NUMBER)) {
value = (float)custom::GenericReader::convertDouble(item->value, item->valueSize);
if (pHas) *pHas = true;
}
}

FORCEINLINE void decodeValue(const char* name, double& value, bool* pHas) {
const custom::GenericValue* item = custom::GetObjectItem(_cur, name);
if (item && item->type == custom::VALUE_NUMBER) {
if (item && checkItemType(*item, custom::VALUE_NUMBER)) {
value = custom::GenericReader::convertDouble(item->value, item->valueSize);
if (pHas) *pHas = true;
}
}

FORCEINLINE void decodeValue(const char* name, std::string& value, bool* pHas) {
const custom::GenericValue* item = custom::GetObjectItem(_cur, name);
if (item && item->type == custom::VALUE_STRING && item->value && item->valueSize) {
if (item && checkItemType(*item, custom::VALUE_STRING) && item->value && item->valueSize) {
value.clear();
bool result = parse_string(value, item->value, item->valueSize);
if (pHas) *pHas = true;
Expand All @@ -243,14 +237,40 @@ namespace struct2x {

FORCEINLINE void decodeValue(const char* name, std::vector<bool>& value, bool* pHas) {
const custom::GenericValue* item = custom::GetObjectItem(_cur, name);
if (item && item->type == custom::VALUE_ARRAY) {
for (const custom::GenericValue* child = item->child; child && child->type == custom::VALUE_BOOL; child = child->next) {
value.push_back((child->valueSize == 4));
if (item && checkItemType(*item, custom::VALUE_ARRAY)) {
for (const custom::GenericValue* child = item->child; child && checkItemType(*item, custom::VALUE_BOOL); child = child->next) {
value.push_back(item2Bool(*child));
if (pHas) *pHas = true;
}
}
}

inline bool checkItemType(const custom::GenericValue& item, const int type) const {
if (_gnoreItemType) {
return true;
}
return (item.type == type);
}

inline bool item2Bool(const custom::GenericValue& item) const {
if (item.type == custom::VALUE_BOOL) {
return (item.valueSize == 4);
} else if (_gnoreItemType) {
if (item.type == custom::VALUE_NUMBER) {
int value = custom::GenericReader::convertInt(item.value, item.valueSize);
return (value != 0);
} else if (item.type == custom::VALUE_STRING) {
std::string value;
if (parse_string(value, item.value, item.valueSize)) {
return (atoi(value.c_str()));
}
} else {
;
}
}
return false;
}

};

}
Expand Down

0 comments on commit 62fd407

Please sign in to comment.