From a007a41b0deb780eb5e923c3af5fe71e0c1339df Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Tue, 22 Oct 2019 20:24:44 -0400 Subject: [PATCH] Add json.hpp. --- include/json.hpp | 139 +++++++++++++++++++++++++++++++++++++++++++++++ include/rack.hpp | 1 + 2 files changed, 140 insertions(+) create mode 100644 include/json.hpp diff --git a/include/json.hpp b/include/json.hpp new file mode 100644 index 00000000..f6491435 --- /dev/null +++ b/include/json.hpp @@ -0,0 +1,139 @@ +#pragma once +#include +#include + + +namespace rack { + + +/** JSON helpers */ +namespace json { + + +/** Checks that a JSON type can be cast to the C++ type */ +template +bool is(json_t* valueJ); +template <> +inline bool is(json_t* valueJ) { + return json_is_string(valueJ); +} +template <> +inline bool is(json_t* valueJ) { + return json_is_integer(valueJ); +} +template <> +inline bool is(json_t* valueJ) { + return json_is_integer(valueJ); +} +template <> +inline bool is(json_t* valueJ) { + return json_is_number(valueJ); +} +template <> +inline bool is(json_t* valueJ) { + return json_is_number(valueJ); +} +template <> +inline bool is(json_t* valueJ) { + return json_is_boolean(valueJ); +} + +/** Converts C++ types to the JSON type */ +template +json_t* to(T value); +template <> +inline json_t* to(const std::string& value) { + return json_stringn(value.c_str(), value.size()); +} +template <> +inline json_t* to(json_int_t value) { + return json_integer(value); +} +template <> +inline json_t* to(int value) { + return json_integer(value); +} +template <> +inline json_t* to(double value) { + return json_real(value); +} +template <> +inline json_t* to(float value) { + return json_real(value); +} +template <> +inline json_t* to(bool value) { + return json_boolean(value); +} + +/** Converts the JSON type to C++ types */ +template +T from(json_t* valueJ); +template <> +inline std::string from(json_t* valueJ) { + return std::string(json_string_value(valueJ), json_string_length(valueJ)); +} +template <> +inline json_int_t from(json_t* valueJ) { + return json_integer_value(valueJ); +} +template <> +inline int from(json_t* valueJ) { + return json_integer_value(valueJ); +} +template <> +inline double from(json_t* valueJ) { + return json_number_value(valueJ); +} +template <> +inline float from(json_t* valueJ) { + return json_number_value(valueJ); +} +template <> +inline bool from(json_t* valueJ) { + return json_boolean_value(valueJ); +} + +/** Helper template function for array functions */ +inline int multiplyDims() { + return 1; +} +template +int multiplyDims(int dim, Dims... dims) { + return dim * multiplyDims(dims...); +} + +/** Converts a C++ array to a JSON multidimensional array */ +template +json_t* toArray(const T* x) { + return to(*x); +} +template +json_t* toArray(const T* x, int dim, Dims... dims) { + int stride = multiplyDims(dims...); + json_t* arrayJ = json_array(); + for (int i = 0; i < dim; i++) { + json_array_insert_new(arrayJ, i, toArray(&x[i * stride], dims...)); + } + return arrayJ; +} + +/** Converts a JSON multidimensional array to a C++ array */ +template +void fromArray(json_t* arrayJ, T* x) { + *x = from(arrayJ); +} +template +void fromArray(json_t* arrayJ, T* x, int dim, Dims... dims) { + int stride = multiplyDims(dims...); + for (int i = 0; i < dim; i++) { + json_t* elJ = json_array_get(arrayJ, i); + if (elJ) { + fromArray(elJ, &x[i * stride], dims...); + } + } +} + + +} // namespace json +} // namespace rack diff --git a/include/rack.hpp b/include/rack.hpp index d590dbaa..f8f652cf 100644 --- a/include/rack.hpp +++ b/include/rack.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include