diff --git a/src/json_utils.h b/src/json_utils.h new file mode 100644 index 0000000000000000000000000000000000000000..c70fad5cefcfde078cf23de7b1244d92b01660ee --- /dev/null +++ b/src/json_utils.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2004-2024 Savoir-faire Linux Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ +#pragma once + +#include <string_view> +#include <json/json.h> +#include <logger.h> + +namespace jami { + +extern const Json::CharReaderBuilder rbuilder; + +inline bool parseJson(std::string_view jsonStr, Json::Value& json) { + std::string err; + auto reader = std::unique_ptr<Json::CharReader>(rbuilder.newCharReader()); + if (!reader->parse(jsonStr.data(), + jsonStr.data() + jsonStr.size(), + &json, + &err)) { + JAMI_WARNING("Can't parse JSON: {}\n{}", err, jsonStr); + return false; + } + return true; +} + +} diff --git a/src/string_utils.cpp b/src/string_utils.cpp index fb11b0f44fa8b80622530ddb9d91f979f724c7a0..e1830e263cc9275cdb6630d75ca0d2e3786147f2 100644 --- a/src/string_utils.cpp +++ b/src/string_utils.cpp @@ -39,8 +39,19 @@ #include <ciso646> // fix windows compiler bug +#include "json_utils.h" + namespace jami { +Json::CharReaderBuilder getJsonReaderBuilder() +{ + Json::CharReaderBuilder builder; + builder["collectComments"] = false; + return builder; +} + +const Json::CharReaderBuilder rbuilder = getJsonReaderBuilder(); + std::string_view userAgent() {