2014-10-26 19:59:49 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-01-02 12:06:00 +01:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
2014-10-26 19:59:49 +01:00
|
|
|
* *
|
|
|
|
* 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 2 *
|
|
|
|
* 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, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "base/json.hpp"
|
|
|
|
#include "base/debug.hpp"
|
|
|
|
#include "base/dictionary.hpp"
|
|
|
|
#include "base/array.hpp"
|
|
|
|
#include "base/objectlock.hpp"
|
|
|
|
#include "base/convert.hpp"
|
2016-08-31 13:43:14 +02:00
|
|
|
#include <boost/exception_ptr.hpp>
|
2014-10-28 05:55:08 +01:00
|
|
|
#include <yajl/yajl_version.h>
|
2014-10-26 19:59:49 +01:00
|
|
|
#include <yajl/yajl_gen.h>
|
|
|
|
#include <yajl/yajl_parse.h>
|
|
|
|
#include <stack>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
static void Encode(yajl_gen handle, const Value& value);
|
|
|
|
|
2014-10-28 08:12:54 +01:00
|
|
|
#if YAJL_MAJOR < 2
|
2014-10-28 06:33:40 +01:00
|
|
|
typedef unsigned int yajl_size;
|
|
|
|
#else /* YAJL_MAJOR */
|
|
|
|
typedef size_t yajl_size;
|
|
|
|
#endif /* YAJL_MAJOR */
|
|
|
|
|
2014-10-26 19:59:49 +01:00
|
|
|
static void EncodeDictionary(yajl_gen handle, const Dictionary::Ptr& dict)
|
|
|
|
{
|
|
|
|
yajl_gen_map_open(handle);
|
|
|
|
|
|
|
|
ObjectLock olock(dict);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Dictionary::Pair& kv : dict) {
|
2014-10-26 19:59:49 +01:00
|
|
|
yajl_gen_string(handle, reinterpret_cast<const unsigned char *>(kv.first.CStr()), kv.first.GetLength());
|
|
|
|
Encode(handle, kv.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
yajl_gen_map_close(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void EncodeArray(yajl_gen handle, const Array::Ptr& arr)
|
|
|
|
{
|
|
|
|
yajl_gen_array_open(handle);
|
|
|
|
|
|
|
|
ObjectLock olock(arr);
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& value : arr) {
|
2014-10-26 19:59:49 +01:00
|
|
|
Encode(handle, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
yajl_gen_array_close(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void Encode(yajl_gen handle, const Value& value)
|
|
|
|
{
|
|
|
|
switch (value.GetType()) {
|
|
|
|
case ValueNumber:
|
2016-08-27 12:20:54 +02:00
|
|
|
if (yajl_gen_double(handle, value.Get<double>()) == yajl_gen_invalid_number)
|
2014-10-26 19:59:49 +01:00
|
|
|
yajl_gen_double(handle, 0);
|
|
|
|
|
2014-12-10 09:04:49 +01:00
|
|
|
break;
|
|
|
|
case ValueBoolean:
|
|
|
|
yajl_gen_bool(handle, value.ToBool());
|
|
|
|
|
2014-10-26 19:59:49 +01:00
|
|
|
break;
|
|
|
|
case ValueString:
|
2016-08-27 11:47:36 +02:00
|
|
|
yajl_gen_string(handle, reinterpret_cast<const unsigned char *>(value.Get<String>().CStr()), value.Get<String>().GetLength());
|
2014-10-26 19:59:49 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case ValueObject:
|
2018-01-11 07:29:34 +01:00
|
|
|
{
|
|
|
|
const Object::Ptr& obj = value.Get<Object::Ptr>();
|
|
|
|
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(obj);
|
|
|
|
|
|
|
|
if (dict) {
|
|
|
|
EncodeDictionary(handle, dict);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
Array::Ptr arr = dynamic_pointer_cast<Array>(obj);
|
|
|
|
|
|
|
|
if (arr) {
|
|
|
|
EncodeArray(handle, arr);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
yajl_gen_null(handle);
|
2014-10-26 19:59:49 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
case ValueEmpty:
|
|
|
|
yajl_gen_null(handle);
|
|
|
|
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
VERIFY(!"Invalid variant type.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-16 19:28:46 +01:00
|
|
|
String icinga::JsonEncode(const Value& value, bool pretty_print)
|
2014-10-26 19:59:49 +01:00
|
|
|
{
|
2014-10-28 05:55:08 +01:00
|
|
|
#if YAJL_MAJOR < 2
|
2014-12-16 19:28:46 +01:00
|
|
|
yajl_gen_config conf = { pretty_print, "" };
|
2017-12-14 15:37:20 +01:00
|
|
|
yajl_gen handle = yajl_gen_alloc(&conf, nullptr);
|
2014-10-28 05:55:08 +01:00
|
|
|
#else /* YAJL_MAJOR */
|
2017-12-14 15:37:20 +01:00
|
|
|
yajl_gen handle = yajl_gen_alloc(nullptr);
|
2014-12-16 19:28:46 +01:00
|
|
|
if (pretty_print)
|
|
|
|
yajl_gen_config(handle, yajl_gen_beautify, 1);
|
2014-10-28 05:55:08 +01:00
|
|
|
#endif /* YAJL_MAJOR */
|
|
|
|
|
2014-10-26 19:59:49 +01:00
|
|
|
Encode(handle, value);
|
|
|
|
|
|
|
|
const unsigned char *buf;
|
2014-10-28 06:33:40 +01:00
|
|
|
yajl_size len;
|
|
|
|
|
2014-10-26 19:59:49 +01:00
|
|
|
yajl_gen_get_buf(handle, &buf, &len);
|
|
|
|
|
|
|
|
String result = String(buf, buf + len);
|
|
|
|
|
|
|
|
yajl_gen_free(handle);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct JsonElement
|
|
|
|
{
|
|
|
|
String Key;
|
2018-01-04 09:43:49 +01:00
|
|
|
bool KeySet{false};
|
2014-10-27 05:16:58 +01:00
|
|
|
Value EValue;
|
2014-10-26 19:59:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
struct JsonContext
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Push(const Value& value)
|
|
|
|
{
|
|
|
|
JsonElement element;
|
2014-10-27 05:16:58 +01:00
|
|
|
element.EValue = value;
|
2014-10-26 19:59:49 +01:00
|
|
|
|
|
|
|
m_Stack.push(element);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
JsonElement Pop()
|
2014-10-26 19:59:49 +01:00
|
|
|
{
|
|
|
|
JsonElement value = m_Stack.top();
|
|
|
|
m_Stack.pop();
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddValue(const Value& value)
|
|
|
|
{
|
|
|
|
if (m_Stack.empty()) {
|
|
|
|
JsonElement element;
|
2014-10-27 05:16:58 +01:00
|
|
|
element.EValue = value;
|
2014-10-26 19:59:49 +01:00
|
|
|
m_Stack.push(element);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JsonElement& element = m_Stack.top();
|
|
|
|
|
2014-10-27 05:16:58 +01:00
|
|
|
if (element.EValue.IsObjectType<Dictionary>()) {
|
2014-10-26 19:59:49 +01:00
|
|
|
if (!element.KeySet) {
|
|
|
|
element.Key = value;
|
|
|
|
element.KeySet = true;
|
|
|
|
} else {
|
2014-10-27 05:16:58 +01:00
|
|
|
Dictionary::Ptr dict = element.EValue;
|
2014-10-26 19:59:49 +01:00
|
|
|
dict->Set(element.Key, value);
|
|
|
|
element.KeySet = false;
|
|
|
|
}
|
2014-10-27 05:16:58 +01:00
|
|
|
} else if (element.EValue.IsObjectType<Array>()) {
|
|
|
|
Array::Ptr arr = element.EValue;
|
2014-10-26 19:59:49 +01:00
|
|
|
arr->Add(value);
|
|
|
|
} else {
|
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Cannot add value to JSON element."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
Value GetValue() const
|
2014-10-26 19:59:49 +01:00
|
|
|
{
|
|
|
|
ASSERT(m_Stack.size() == 1);
|
2014-10-27 05:16:58 +01:00
|
|
|
return m_Stack.top().EValue;
|
2014-10-26 19:59:49 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void SaveException()
|
2014-10-26 19:59:49 +01:00
|
|
|
{
|
2016-08-31 13:43:14 +02:00
|
|
|
m_Exception = boost::current_exception();
|
2014-10-26 19:59:49 +01:00
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
void ThrowException() const
|
2014-10-26 19:59:49 +01:00
|
|
|
{
|
2014-10-27 09:28:32 +01:00
|
|
|
if (m_Exception)
|
2016-08-31 13:43:14 +02:00
|
|
|
boost::rethrow_exception(m_Exception);
|
2014-10-26 19:59:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::stack<JsonElement> m_Stack;
|
|
|
|
Value m_Key;
|
2016-08-31 13:43:14 +02:00
|
|
|
boost::exception_ptr m_Exception;
|
2014-10-26 19:59:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static int DecodeNull(void *ctx)
|
|
|
|
{
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *context = static_cast<JsonContext *>(ctx);
|
2014-10-26 19:59:49 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
context->AddValue(Empty);
|
|
|
|
} catch (...) {
|
|
|
|
context->SaveException();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int DecodeBoolean(void *ctx, int value)
|
|
|
|
{
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *context = static_cast<JsonContext *>(ctx);
|
2014-10-26 19:59:49 +01:00
|
|
|
|
|
|
|
try {
|
2014-12-10 09:04:49 +01:00
|
|
|
context->AddValue(static_cast<bool>(value));
|
2014-10-26 19:59:49 +01:00
|
|
|
} catch (...) {
|
|
|
|
context->SaveException();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-28 06:33:40 +01:00
|
|
|
static int DecodeNumber(void *ctx, const char *str, yajl_size len)
|
2014-10-26 19:59:49 +01:00
|
|
|
{
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *context = static_cast<JsonContext *>(ctx);
|
2014-10-26 19:59:49 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
String jstr = String(str, str + len);
|
|
|
|
context->AddValue(Convert::ToDouble(jstr));
|
|
|
|
} catch (...) {
|
|
|
|
context->SaveException();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-10-28 06:33:40 +01:00
|
|
|
static int DecodeString(void *ctx, const unsigned char *str, yajl_size len)
|
2014-10-26 19:59:49 +01:00
|
|
|
{
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *context = static_cast<JsonContext *>(ctx);
|
2014-10-26 19:59:49 +01:00
|
|
|
|
|
|
|
try {
|
2014-10-27 09:28:32 +01:00
|
|
|
context->AddValue(String(str, str + len));
|
2014-10-26 19:59:49 +01:00
|
|
|
} catch (...) {
|
|
|
|
context->SaveException();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int DecodeStartMap(void *ctx)
|
|
|
|
{
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *context = static_cast<JsonContext *>(ctx);
|
2014-10-26 19:59:49 +01:00
|
|
|
|
|
|
|
try {
|
2014-11-08 21:17:16 +01:00
|
|
|
context->Push(new Dictionary());
|
2014-10-26 19:59:49 +01:00
|
|
|
} catch (...) {
|
|
|
|
context->SaveException();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-03-02 12:44:15 +01:00
|
|
|
static int DecodeEndMapOrArray(void *ctx)
|
2014-10-26 19:59:49 +01:00
|
|
|
{
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *context = static_cast<JsonContext *>(ctx);
|
2014-10-26 19:59:49 +01:00
|
|
|
|
|
|
|
try {
|
2014-10-27 05:16:58 +01:00
|
|
|
context->AddValue(context->Pop().EValue);
|
2014-10-26 19:59:49 +01:00
|
|
|
} catch (...) {
|
|
|
|
context->SaveException();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int DecodeStartArray(void *ctx)
|
|
|
|
{
|
2018-01-04 09:07:03 +01:00
|
|
|
auto *context = static_cast<JsonContext *>(ctx);
|
2017-12-13 12:54:14 +01:00
|
|
|
|
2014-10-26 19:59:49 +01:00
|
|
|
try {
|
2014-11-08 21:17:16 +01:00
|
|
|
context->Push(new Array());
|
2014-10-26 19:59:49 +01:00
|
|
|
} catch (...) {
|
|
|
|
context->SaveException();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value icinga::JsonDecode(const String& data)
|
|
|
|
{
|
|
|
|
static const yajl_callbacks callbacks = {
|
|
|
|
DecodeNull,
|
|
|
|
DecodeBoolean,
|
2017-12-14 15:37:20 +01:00
|
|
|
nullptr,
|
|
|
|
nullptr,
|
2014-10-26 19:59:49 +01:00
|
|
|
DecodeNumber,
|
|
|
|
DecodeString,
|
|
|
|
DecodeStartMap,
|
|
|
|
DecodeString,
|
2015-03-02 12:44:15 +01:00
|
|
|
DecodeEndMapOrArray,
|
2014-10-26 19:59:49 +01:00
|
|
|
DecodeStartArray,
|
2015-03-02 12:44:15 +01:00
|
|
|
DecodeEndMapOrArray
|
2014-10-26 19:59:49 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
yajl_handle handle;
|
2014-10-28 05:55:08 +01:00
|
|
|
#if YAJL_MAJOR < 2
|
|
|
|
yajl_parser_config cfg = { 1, 0 };
|
|
|
|
#endif /* YAJL_MAJOR */
|
2014-10-26 19:59:49 +01:00
|
|
|
JsonContext context;
|
|
|
|
|
2014-10-28 05:55:08 +01:00
|
|
|
#if YAJL_MAJOR < 2
|
2017-12-14 15:37:20 +01:00
|
|
|
handle = yajl_alloc(&callbacks, &cfg, nullptr, &context);
|
2014-10-28 05:55:08 +01:00
|
|
|
#else /* YAJL_MAJOR */
|
2017-12-14 15:37:20 +01:00
|
|
|
handle = yajl_alloc(&callbacks, nullptr, &context);
|
2014-11-20 10:09:13 +01:00
|
|
|
yajl_config(handle, yajl_dont_validate_strings, 1);
|
2015-08-17 08:03:25 +02:00
|
|
|
yajl_config(handle, yajl_allow_comments, 1);
|
2014-10-28 05:55:08 +01:00
|
|
|
#endif /* YAJL_MAJOR */
|
2014-10-26 19:59:49 +01:00
|
|
|
|
|
|
|
yajl_parse(handle, reinterpret_cast<const unsigned char *>(data.CStr()), data.GetLength());
|
|
|
|
|
2014-10-28 05:55:08 +01:00
|
|
|
#if YAJL_MAJOR < 2
|
|
|
|
if (yajl_parse_complete(handle) != yajl_status_ok) {
|
|
|
|
#else /* YAJL_MAJOR */
|
2014-10-26 19:59:49 +01:00
|
|
|
if (yajl_complete_parse(handle) != yajl_status_ok) {
|
2014-10-28 05:55:08 +01:00
|
|
|
#endif /* YAJL_MAJOR */
|
2014-10-26 19:59:49 +01:00
|
|
|
unsigned char *internal_err_str = yajl_get_error(handle, 1, reinterpret_cast<const unsigned char *>(data.CStr()), data.GetLength());
|
|
|
|
String msg = reinterpret_cast<char *>(internal_err_str);
|
|
|
|
yajl_free_error(handle, internal_err_str);
|
|
|
|
|
|
|
|
yajl_free(handle);
|
|
|
|
|
2014-10-27 09:28:32 +01:00
|
|
|
/* throw saved exception (if there is one) */
|
|
|
|
context.ThrowException();
|
|
|
|
|
2014-10-26 19:59:49 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument(msg));
|
|
|
|
}
|
|
|
|
|
|
|
|
yajl_free(handle);
|
|
|
|
|
|
|
|
return context.GetValue();
|
|
|
|
}
|