JsonValue ========= .. cpp:class:: pandas::JsonValue pandas C++ class. Example ------- .. code-block:: cpp #include using namespace pandas; // Use JsonValue JsonValue obj; // ... operations ... Constructors ------------ .. list-table:: :widths: 55 25 20 :header-rows: 1 * - Signature - Location - Example * - ``JsonValue() = default`` - json.h:44 - :ref:`View ` * - ``JsonValue(const JsonValue&) = default`` - json.h:45 - :ref:`View ` * - ``JsonValue(JsonValue&&) noexcept = default`` - json.h:46 - :ref:`View ` Type Checking ------------- .. list-table:: :widths: 40 20 15 25 :header-rows: 1 * - Signature - Return Type - Location - Example * - ``bool is_array() const { return std::holds_alternative(v)`` - bool - json.h:63 - * - ``bool is_bool() const { return std::holds_alternative(v)`` - bool - json.h:59 - * - ``bool is_double() const { return std::holds_alternative(v)`` - bool - json.h:61 - * - ``bool is_int() const { return std::holds_alternative(v)`` - bool - json.h:60 - :ref:`View ` * - ``bool is_null() const { return std::holds_alternative(v)`` - bool - json.h:58 - :ref:`View ` * - ``bool is_object() const { return std::holds_alternative(v)`` - bool - json.h:64 - :ref:`View ` * - ``bool is_string() const { return std::holds_alternative(v)`` - bool - json.h:62 - Code Examples ------------- The following examples are extracted from the test suite. .. _example-jsonvalue-jsonvalue-0: .. dropdown:: JsonValue (pd_test_5_all.cpp:81168) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 81158 :emphasize-lines: 11 void case_1_default_is_null(int& local_fail) { std::cout << "-- case_1_default_is_null\n"; JsonValue v; pandas_tests::check(v.is_null(), "case_1_default_is_null.is_null()", local_fail); } void case_2_bool_true_false(int& local_fail) { std::cout << "-- case_2_bool_true_false\n"; check_str("case_2_bool_true_false.true_str()", "True", [] { return PIO::json_value_to_string(JsonValue(true)); }, local_fail); check_str("case_2_bool_true_false.false_str()", "False", [] { return PIO::json_value_to_string(JsonValue(false)); }, local_fail); } void case_3_int64_range(int& local_fail) { std::cout << "-- case_3_int64_range\n"; check_str("case_3_int64_range.zero()", "0", [] { return PIO::json_value_to_string(JsonValue(int64_t{0})); }, local_fail); check_str("case_3_int64_range.neg()", "-12345", .. _example-jsonvalue-jsonvalue-1: .. dropdown:: JsonValue (pd_test_5_all.cpp:81168) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 81158 :emphasize-lines: 11 void case_1_default_is_null(int& local_fail) { std::cout << "-- case_1_default_is_null\n"; JsonValue v; pandas_tests::check(v.is_null(), "case_1_default_is_null.is_null()", local_fail); } void case_2_bool_true_false(int& local_fail) { std::cout << "-- case_2_bool_true_false\n"; check_str("case_2_bool_true_false.true_str()", "True", [] { return PIO::json_value_to_string(JsonValue(true)); }, local_fail); check_str("case_2_bool_true_false.false_str()", "False", [] { return PIO::json_value_to_string(JsonValue(false)); }, local_fail); } void case_3_int64_range(int& local_fail) { std::cout << "-- case_3_int64_range\n"; check_str("case_3_int64_range.zero()", "0", [] { return PIO::json_value_to_string(JsonValue(int64_t{0})); }, local_fail); check_str("case_3_int64_range.neg()", "-12345", .. _example-jsonvalue-jsonvalue-2: .. dropdown:: JsonValue (pd_test_5_all.cpp:81168) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 81158 :emphasize-lines: 11 void case_1_default_is_null(int& local_fail) { std::cout << "-- case_1_default_is_null\n"; JsonValue v; pandas_tests::check(v.is_null(), "case_1_default_is_null.is_null()", local_fail); } void case_2_bool_true_false(int& local_fail) { std::cout << "-- case_2_bool_true_false\n"; check_str("case_2_bool_true_false.true_str()", "True", [] { return PIO::json_value_to_string(JsonValue(true)); }, local_fail); check_str("case_2_bool_true_false.false_str()", "False", [] { return PIO::json_value_to_string(JsonValue(false)); }, local_fail); } void case_3_int64_range(int& local_fail) { std::cout << "-- case_3_int64_range\n"; check_str("case_3_int64_range.zero()", "0", [] { return PIO::json_value_to_string(JsonValue(int64_t{0})); }, local_fail); check_str("case_3_int64_range.neg()", "-12345", .. _example-jsonvalue-is_int-3: .. dropdown:: is_int (pd_test_5_all.cpp:81116) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 81106 :emphasize-lines: 11 static void check_lookup_int(const std::string& label, const JsonObject& root, const std::vector& path, int64_t expected_int, int& local_fail) { bool ok = false; std::string err; try { const JsonValue* hit = PIO::lookup_path(root, path); if (hit && hit->is_int() && std::get(hit->v) == expected_int) { ok = true; } } catch (const std::exception& e) { err = std::string("THREW: ") + e.what(); } pandas_tests::check(ok, label, local_fail); if (!ok && !err.empty()) std::cout << " " << err << "\n"; } static void check_lookup_object(const std::string& label, .. _example-jsonvalue-is_null-4: .. dropdown:: is_null (pd_test_5_all.cpp:71470) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 71460 :emphasize-lines: 11 bool ok = (got == expected); pandas_tests::check(ok, label, local_fail); if (!ok) { std::cout << " expected: [" << expected << "]\n" << " got: [" << got << "]\n"; } return ok; } static numpy::object_ obj_none() { return numpy::object_(); // default-constructed -> is_null() == true } static numpy::object_ obj_nan() { return numpy::object_(std::numeric_limits::quiet_NaN()); } static numpy::object_ obj_int(int64_t v) { return numpy::object_(v); } .. _example-jsonvalue-is_object-5: .. dropdown:: is_object (pd_test_3_all.cpp:626) :class-title: example-dropdown .. code-block:: cpp :linenos: :lineno-start: 616 :emphasize-lines: 11 throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_numeric"); } if (!int_dtype.is_integer()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should be integer" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_integer"); } if (int_dtype.is_floating()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be floating" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_floating"); } if (int_dtype.is_object()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : int should not be object" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: int is_object"); } // Test with float index pandas::IndexDtype float_dtype; if (!float_dtype.is_numeric()) { std::cout << " [FAIL] : in pd_test_3_all_index_dtype_checks() : float should be numeric" << std::endl; throw std::runtime_error("pd_test_3_all_index_dtype_checks failed: float is_numeric"); }