Releases: randydu/json5pp
Releases · randydu/json5pp
Supports VC2022
non-null value access, default value and callback
- adds type conversion template function: v.to<T>();
json5pp::value v(100);
CHECK(v.get<int>() == 100);
CHECK_THROWS(v.get<std::string>() == "100"); // throws std::bad_cast, auto-conversion is OFF
CHECK(v.to<std::string>() == "100"); //auto conversion is ON- adds implicit type conversion: (T)v;
json5pp::value v(100);
float f = v; // = v.get<float>();
CHECK(f == 100);- adds try_get(T&)/try_get(F f), get_or(def_val);
Try accessing non-null value.
json5cpp::value null;
int i {0};
CHECK_FALSE(null.try_get(i));
CHECK( i == 0); // i not touched.
///// try_get with callback //////
json5pp::value v(10);
bool ok;
ok = v.try_get<int>([](auto&& x){ std::cout << x ; }); // print out "10" in callback
CHECK(ok); //return true if non-null value and callback returns void.
ok = v.try_get<int>([&](auto&& x){
if( x > 100) return false; //only accept value <=100.
i = x;
return true; // valid
});
CHECK(ok); //returns true if non-null value read and callback returns true.
CHECK(i == 10);
///////// default value ////////
i = null.get_or(100);
CHECK( i == 100); // i falls back to default value.
json5cpp::value v(8);
i = v.get_or(100);
CHECK(i == 8); // i is changed.v3.0.0
- needs C++ 20, replace union with std::variant;
- flexible value operators; (==, !=, >, >=, <, <=);
value v(10); CHECK(v == 10); CHECK(v < 20 && v >= 8); value w(100); CHECK(v < w);
- flexible type-safe value extractors;
value v (88); auto i = v.get<int>(); auto i64 = v.get<int64_t>(); CHECK_THROWS(v.get<std::string>()); //int => string auto conversion OFF (by default) CHECK(v.get<std::string, true>() == "88); //auto-conversion ON long j; v.get(j); v >> j; v << 18.9; CHECK(v == 18.9); value null; CHECK(null.is_null()); CHECK(v.get<void*>() == nullptr);