From 09ae8f1e0083d019752e47518b09d7f7d2f496ce Mon Sep 17 00:00:00 2001 From: "FeRD (Frank Dana)" Date: Fri, 27 Dec 2019 10:28:18 -0500 Subject: [PATCH] Add Json.cpp --- src/Json.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/Json.cpp diff --git a/src/Json.cpp b/src/Json.cpp new file mode 100644 index 00000000..0c83d9d6 --- /dev/null +++ b/src/Json.cpp @@ -0,0 +1,50 @@ +/** + * @file + * @brief Helper functions for Json parsing + * @author FeRD (Frank Dana) + * + * @ref License + */ + +/* LICENSE + * + * Copyright (c) 2008-2019 OpenShot Studios, LLC + * . This file is part of + * OpenShot Library (libopenshot), an open-source project dedicated to + * delivering high quality video editing and animation solutions to the + * world. For more information visit . + * + * OpenShot Library (libopenshot) is free software: you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * OpenShot Library (libopenshot) 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 Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with OpenShot Library. If not, see . + */ + +#include "../include/Json.h" + +const Json::Value openshot::stringToJson(const std::string value) { + + // Parse JSON string into JSON objects + Json::Value root; + Json::CharReaderBuilder rbuilder; + Json::CharReader* reader(rbuilder.newCharReader()); + + std::string errors; + bool success = reader->parse( value.c_str(), value.c_str() + value.size(), + &root, &errors ); + delete reader; + + if (!success) + // Raise exception + throw openshot::InvalidJSON("JSON could not be parsed (or is invalid)"); + + return root; +}