Skip to content

Commit 466b4ea

Browse files
committed
Implemented as<T>() and is<T>() with accompanying tests
1 parent 2703c30 commit 466b4ea

File tree

2 files changed

+171
-0
lines changed

2 files changed

+171
-0
lines changed

include/json/value.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,40 @@ class JSON_API Value {
399399
double asDouble() const;
400400
bool asBool() const;
401401

402+
template <class T>
403+
struct asLookupHelper;
404+
405+
#define defAsLookupHelper(type, lookup) \
406+
template<> \
407+
struct asLookupHelper< type > { \
408+
static type as(const Value& val) { \
409+
return val. lookup (); \
410+
} \
411+
}
412+
413+
defAsLookupHelper(const char*, asCString);
414+
defAsLookupHelper(String, asString);
415+
#ifdef JSON_USE_CPPTL
416+
defAsLookupHelper(CppTL::ConstString, asConstString);
417+
#endif
418+
defAsLookupHelper(Int, asInt);
419+
defAsLookupHelper(UInt, asUInt);
420+
#if defined(JSON_HAS_INT64)
421+
defAsLookupHelper(Int64, asInt64);
422+
defAsLookupHelper(UInt64, asUInt64);
423+
#endif // if defined(JSON_HAS_INT64)
424+
// (U)LargestInt is a type alias of int or int64 and thus cannot be defined
425+
defAsLookupHelper(float, asFloat);
426+
defAsLookupHelper(double, asDouble);
427+
defAsLookupHelper(bool, asBool);
428+
429+
#undef defAsLookupHelper
430+
431+
template <class T>
432+
T as() const {
433+
return asLookupHelper<T>::as(*this);
434+
}
435+
402436
bool isNull() const;
403437
bool isBool() const;
404438
bool isInt() const;
@@ -412,6 +446,33 @@ class JSON_API Value {
412446
bool isArray() const;
413447
bool isObject() const;
414448

449+
template <class T>
450+
struct isLookupHelper;
451+
452+
#define defIsLookupHelper(type, lookup) \
453+
template<> \
454+
struct isLookupHelper< type > { \
455+
static bool is(const Value& val) { \
456+
return val. lookup (); \
457+
} \
458+
}
459+
460+
defIsLookupHelper(bool, isBool);
461+
defIsLookupHelper(Int, isInt);
462+
defIsLookupHelper(Int64, isInt64);
463+
defIsLookupHelper(UInt, isUInt);
464+
defIsLookupHelper(UInt64, isUInt64);
465+
defIsLookupHelper(double, isDouble);
466+
defIsLookupHelper(const char*, isString);
467+
defIsLookupHelper(String, isString);
468+
469+
#undef defIsLookupHelper
470+
471+
template <class T>
472+
bool is() const {
473+
return isLookupHelper<T>::is(*this);
474+
}
475+
415476
bool isConvertibleTo(ValueType other) const;
416477

417478
/// Number of values in array or object

src/test_lib_json/main.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3361,6 +3361,116 @@ int main(int argc, const char* argv[]) {
33613361
return runner.runCommandLine(argc, argv);
33623362
}
33633363

3364+
struct TemplatedAs : JsonTest::TestCase {};
3365+
3366+
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorCString) {
3367+
Json::Value json = "hello world";
3368+
JSONTEST_ASSERT_STRING_EQUAL(json.asCString(), json.as<const char*>());
3369+
}
3370+
3371+
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorString) {
3372+
Json::Value json = "hello world";
3373+
JSONTEST_ASSERT_STRING_EQUAL(json.asString(), json.as<Json::String>());
3374+
}
3375+
3376+
#ifdef JSON_USE_CPPTL
3377+
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorConstString) {
3378+
Json::Value json = "hello world";
3379+
JSONTEST_ASSERT_STRING_EQUAL(json.asConstString(), json.as<CppTL::ConstString>());
3380+
}
3381+
#endif
3382+
3383+
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorInt) {
3384+
Json::Value json = Json::Int(64);
3385+
JSONTEST_ASSERT_EQUAL(json.asInt(), json.as<Json::Int>());
3386+
}
3387+
3388+
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorUInt) {
3389+
Json::Value json = Json::UInt(64);
3390+
JSONTEST_ASSERT_EQUAL(json.asUInt(), json.as<Json::UInt>());
3391+
}
3392+
3393+
#if defined(JSON_HAS_INT64)
3394+
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorInt64) {
3395+
Json::Value json = Json::Int64(64);
3396+
JSONTEST_ASSERT_EQUAL(json.asUInt64(), json.as<Json::Int64>());
3397+
}
3398+
3399+
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorUInt64) {
3400+
Json::Value json = Json::UInt64(64);
3401+
JSONTEST_ASSERT_EQUAL(json.asUInt64(), json.as<Json::UInt64>());
3402+
}
3403+
#endif // if defined(JSON_HAS_INT64)
3404+
3405+
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorLargestInt) {
3406+
Json::Value json = Json::LargestInt(64);
3407+
JSONTEST_ASSERT_EQUAL(json.asLargestInt(), json.as<Json::LargestInt>());
3408+
}
3409+
3410+
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorLargestUInt) {
3411+
Json::Value json = Json::LargestUInt(64);
3412+
JSONTEST_ASSERT_EQUAL(json.asLargestUInt(), json.as<Json::LargestUInt>());
3413+
}
3414+
3415+
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorFloat) {
3416+
Json::Value json = float(69.69);
3417+
JSONTEST_ASSERT_EQUAL(json.asFloat(), json.as<float>());
3418+
}
3419+
3420+
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorDouble) {
3421+
Json::Value json = double(69.69);
3422+
JSONTEST_ASSERT_EQUAL(json.asDouble(), json.as<double>());
3423+
}
3424+
3425+
JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorBool) {
3426+
Json::Value jsonTrue = true;
3427+
Json::Value jsonFalse = false;
3428+
JSONTEST_ASSERT_EQUAL(jsonTrue.asBool(), jsonTrue.as<bool>());
3429+
JSONTEST_ASSERT_EQUAL(jsonFalse.asBool(), jsonFalse.as<bool>());
3430+
}
3431+
3432+
struct TemplatedIs : JsonTest::TestCase {};
3433+
3434+
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsBool) {
3435+
Json::Value json = true;
3436+
JSONTEST_ASSERT_EQUAL(json.isBool(), json.is<bool>());
3437+
}
3438+
3439+
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsInt) {
3440+
Json::Value json = 142;
3441+
JSONTEST_ASSERT_EQUAL(json.isInt(), json.is<Json::Int>());
3442+
}
3443+
3444+
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsInt64) {
3445+
Json::Value json = 142;
3446+
JSONTEST_ASSERT_EQUAL(json.isInt64(), json.is<Json::Int64>());
3447+
}
3448+
3449+
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsUInt) {
3450+
Json::Value json = 142;
3451+
JSONTEST_ASSERT_EQUAL(json.isUInt(), json.is<Json::UInt>());
3452+
}
3453+
3454+
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsUInt64) {
3455+
Json::Value json = 142;
3456+
JSONTEST_ASSERT_EQUAL(json.isUInt64(), json.is<Json::UInt64>());
3457+
}
3458+
3459+
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsDouble) {
3460+
Json::Value json = 40.63;
3461+
JSONTEST_ASSERT_EQUAL(json.isDouble(), json.is<double>());
3462+
}
3463+
3464+
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsCString) {
3465+
Json::Value json = "hello world";
3466+
JSONTEST_ASSERT_EQUAL(json.isString(), json.is<const char*>());
3467+
}
3468+
3469+
JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsString) {
3470+
Json::Value json = "hello world";
3471+
JSONTEST_ASSERT_EQUAL(json.isString(), json.is<Json::String>());
3472+
}
3473+
33643474
#if defined(__GNUC__)
33653475
#pragma GCC diagnostic pop
33663476
#endif

0 commit comments

Comments
 (0)