From 322f8932476eda944c7d3ac65eafde12c69b2ae9 Mon Sep 17 00:00:00 2001 From: Loïc Collignon Date: Tue, 5 Jun 2018 10:29:47 +0200 Subject: Rewrite of the High Level API using the new HAL model MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The new HAL model need the High Level API to be rewritten. This is the first version of this rewrite, still in progress but should work. Change-Id: I5c94cf39d84cefae6b7a179c09d95e645673e8d4 Signed-off-by: Loïc Collignon --- ahl-binding/jsonc_utils.hpp | 90 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 ahl-binding/jsonc_utils.hpp (limited to 'ahl-binding/jsonc_utils.hpp') diff --git a/ahl-binding/jsonc_utils.hpp b/ahl-binding/jsonc_utils.hpp new file mode 100644 index 0000000..02e113b --- /dev/null +++ b/ahl-binding/jsonc_utils.hpp @@ -0,0 +1,90 @@ +#pragma once + +/* + * Copyright (C) 2018 "IoT.bzh" + * Author Loïc Collignon + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include + +template +inline T& jcast(T& v, json_object* o) +{ + v << o; + return v; +} + +template +inline T& jcast_array(T& v, json_object* o) +{ + auto sz = json_object_array_length(o); + for(auto i = 0; i < sz ; ++i) + { + typename T::value_type item; + jcast(item, json_object_array_get_idx(o, i)); + v.push_back(item); + } + return v; +} + +template<> +inline bool& jcast(bool& v, json_object* o) +{ + v = (json_object_get_boolean(o) == TRUE); + return v; +} + +template<> +inline double& jcast(double& v, json_object* o) +{ + v = json_object_get_double(o); + return v; +} + +template<> +inline int32_t& jcast(int32_t& v, json_object* o) +{ + v = json_object_get_int(o); + return v; +} + +template<> +inline int64_t& jcast(int64_t& v, json_object* o) +{ + v = json_object_get_int(o); + return v; +} + +template<> +inline std::string& jcast(std::string& v, json_object* o) +{ + const char* tmp = json_object_get_string(o); + v = tmp ? tmp : ""; + return v; +} + +template +inline T& jcast(T& v, json_object* o, std::string field) +{ + return jcast(v, json_object_object_get(o, field.c_str())); +} + +template +inline T& jcast_array(T& v, json_object* o, std::string field) +{ + return jcast_array(v, json_object_object_get(o, field.c_str())); +} -- cgit 1.2.3-korg