diff --git a/include/json/value.h b/include/json/value.h index d0af7116b..4d74b008b 100644 --- a/include/json/value.h +++ b/include/json/value.h @@ -399,6 +399,33 @@ class JSON_API Value { double asDouble() const; bool asBool() const; + template struct asLookupHelper; + +#define defAsLookupHelper(type, lookup) \ + template <> struct asLookupHelper { \ + static type as(const Value& val) { return val.lookup(); } \ + } + + defAsLookupHelper(const char*, asCString); + defAsLookupHelper(String, asString); +#ifdef JSON_USE_CPPTL + defAsLookupHelper(CppTL::ConstString, asConstString); +#endif + defAsLookupHelper(Int, asInt); + defAsLookupHelper(UInt, asUInt); +#if defined(JSON_HAS_INT64) + defAsLookupHelper(Int64, asInt64); + defAsLookupHelper(UInt64, asUInt64); +#endif // if defined(JSON_HAS_INT64) + // (U)LargestInt is a type alias of int or int64 and thus cannot be defined + defAsLookupHelper(float, asFloat); + defAsLookupHelper(double, asDouble); + defAsLookupHelper(bool, asBool); + +#undef defAsLookupHelper + + template T as() const { return asLookupHelper::as(*this); } + bool isNull() const; bool isBool() const; bool isInt() const; @@ -412,6 +439,26 @@ class JSON_API Value { bool isArray() const; bool isObject() const; + template struct isLookupHelper; + +#define defIsLookupHelper(type, lookup) \ + template <> struct isLookupHelper { \ + static bool is(const Value& val) { return val.lookup(); } \ + } + + defIsLookupHelper(bool, isBool); + defIsLookupHelper(Int, isInt); + defIsLookupHelper(Int64, isInt64); + defIsLookupHelper(UInt, isUInt); + defIsLookupHelper(UInt64, isUInt64); + defIsLookupHelper(double, isDouble); + defIsLookupHelper(const char*, isString); + defIsLookupHelper(String, isString); + +#undef defIsLookupHelper + + template bool is() const { return isLookupHelper::is(*this); } + bool isConvertibleTo(ValueType other) const; /// Number of values in array or object diff --git a/src/test_lib_json/main.cpp b/src/test_lib_json/main.cpp index 1d636d8a0..b5f0e604b 100644 --- a/src/test_lib_json/main.cpp +++ b/src/test_lib_json/main.cpp @@ -3361,6 +3361,117 @@ int main(int argc, const char* argv[]) { return runner.runCommandLine(argc, argv); } +struct TemplatedAs : JsonTest::TestCase {}; + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorCString) { + Json::Value json = "hello world"; + JSONTEST_ASSERT_STRING_EQUAL(json.asCString(), json.as()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorString) { + Json::Value json = "hello world"; + JSONTEST_ASSERT_STRING_EQUAL(json.asString(), json.as()); +} + +#ifdef JSON_USE_CPPTL +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorConstString) { + Json::Value json = "hello world"; + JSONTEST_ASSERT_STRING_EQUAL(json.asConstString(), + json.as()); +} +#endif + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorInt) { + Json::Value json = Json::Int(64); + JSONTEST_ASSERT_EQUAL(json.asInt(), json.as()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorUInt) { + Json::Value json = Json::UInt(64); + JSONTEST_ASSERT_EQUAL(json.asUInt(), json.as()); +} + +#if defined(JSON_HAS_INT64) +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorInt64) { + Json::Value json = Json::Int64(64); + JSONTEST_ASSERT_EQUAL(json.asUInt64(), json.as()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorUInt64) { + Json::Value json = Json::UInt64(64); + JSONTEST_ASSERT_EQUAL(json.asUInt64(), json.as()); +} +#endif // if defined(JSON_HAS_INT64) + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorLargestInt) { + Json::Value json = Json::LargestInt(64); + JSONTEST_ASSERT_EQUAL(json.asLargestInt(), json.as()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorLargestUInt) { + Json::Value json = Json::LargestUInt(64); + JSONTEST_ASSERT_EQUAL(json.asLargestUInt(), json.as()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorFloat) { + Json::Value json = float(69.69); + JSONTEST_ASSERT_EQUAL(json.asFloat(), json.as()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorDouble) { + Json::Value json = double(69.69); + JSONTEST_ASSERT_EQUAL(json.asDouble(), json.as()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedAs, equalBehaviorBool) { + Json::Value jsonTrue = true; + Json::Value jsonFalse = false; + JSONTEST_ASSERT_EQUAL(jsonTrue.asBool(), jsonTrue.as()); + JSONTEST_ASSERT_EQUAL(jsonFalse.asBool(), jsonFalse.as()); +} + +struct TemplatedIs : JsonTest::TestCase {}; + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsBool) { + Json::Value json = true; + JSONTEST_ASSERT_EQUAL(json.isBool(), json.is()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsInt) { + Json::Value json = 142; + JSONTEST_ASSERT_EQUAL(json.isInt(), json.is()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsInt64) { + Json::Value json = 142; + JSONTEST_ASSERT_EQUAL(json.isInt64(), json.is()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsUInt) { + Json::Value json = 142; + JSONTEST_ASSERT_EQUAL(json.isUInt(), json.is()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsUInt64) { + Json::Value json = 142; + JSONTEST_ASSERT_EQUAL(json.isUInt64(), json.is()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsDouble) { + Json::Value json = 40.63; + JSONTEST_ASSERT_EQUAL(json.isDouble(), json.is()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsCString) { + Json::Value json = "hello world"; + JSONTEST_ASSERT_EQUAL(json.isString(), json.is()); +} + +JSONTEST_FIXTURE_LOCAL(TemplatedIs, equalBehaviorIsString) { + Json::Value json = "hello world"; + JSONTEST_ASSERT_EQUAL(json.isString(), json.is()); +} + #if defined(__GNUC__) #pragma GCC diagnostic pop #endif