icinga2/lib/base/json.cpp

497 lines
9.1 KiB
C++
Raw Normal View History

/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2014-10-26 19:59:49 +01:00
#include "base/json.hpp"
#include "base/debug.hpp"
2018-08-07 13:55:41 +02:00
#include "base/namespace.hpp"
2014-10-26 19:59:49 +01:00
#include "base/dictionary.hpp"
#include "base/array.hpp"
#include "base/objectlock.hpp"
#include "base/convert.hpp"
#include "base/utility.hpp"
2019-03-15 09:16:41 +01:00
#include <bitset>
#include <boost/exception_ptr.hpp>
2019-03-15 09:16:41 +01:00
#include <cstdint>
#include <json.hpp>
2014-10-26 19:59:49 +01:00
#include <stack>
#include <utility>
2019-03-15 09:16:41 +01:00
#include <vector>
2014-10-26 19:59:49 +01:00
using namespace icinga;
class JsonSax : public nlohmann::json_sax<nlohmann::json>
{
public:
bool null() override;
bool boolean(bool val) override;
bool number_integer(number_integer_t val) override;
bool number_unsigned(number_unsigned_t val) override;
bool number_float(number_float_t val, const string_t& s) override;
bool string(string_t& val) override;
bool start_object(std::size_t elements) override;
bool key(string_t& val) override;
bool end_object() override;
bool start_array(std::size_t elements) override;
bool end_array() override;
bool parse_error(std::size_t position, const std::string& last_token, const nlohmann::detail::exception& ex) override;
Value GetResult();
private:
Value m_Root;
2019-03-15 11:53:56 +01:00
std::stack<std::pair<Dictionary*, Array*>> m_CurrentSubtree;
String m_CurrentKey;
void FillCurrentTarget(Value value);
};
2019-03-15 09:16:41 +01:00
const char l_Null[] = "null";
const char l_False[] = "false";
const char l_True[] = "true";
const char l_Indent[] = " ";
2014-10-26 19:59:49 +01:00
2019-03-15 09:16:41 +01:00
// https://github.com/nlohmann/json/issues/1512
template<bool prettyPrint>
class JsonEncoder
{
public:
void Null();
void Boolean(bool value);
void NumberFloat(double value);
void Strng(String value);
void StartObject();
void Key(String value);
void EndObject();
void StartArray();
void EndArray();
String GetResult();
private:
std::vector<char> m_Result;
String m_CurrentKey;
std::stack<std::bitset<2>> m_CurrentSubtree;
void AppendChar(char c);
template<class Iterator>
void AppendChars(Iterator begin, Iterator end);
void AppendJson(nlohmann::json json);
2019-03-15 09:16:41 +01:00
void BeforeItem();
void FinishContainer(char terminator);
};
template<bool prettyPrint>
void Encode(JsonEncoder<prettyPrint>& stateMachine, const Value& value);
template<bool prettyPrint>
inline
void EncodeNamespace(JsonEncoder<prettyPrint>& stateMachine, const Namespace::Ptr& ns)
2018-08-07 13:55:41 +02:00
{
2019-03-15 09:16:41 +01:00
stateMachine.StartObject();
2018-08-07 13:55:41 +02:00
ObjectLock olock(ns);
for (const Namespace::Pair& kv : ns) {
stateMachine.Key(Utility::ValidateUTF8(kv.first));
2019-03-15 09:16:41 +01:00
Encode(stateMachine, kv.second->Get());
2018-08-07 13:55:41 +02:00
}
2019-03-15 09:16:41 +01:00
stateMachine.EndObject();
2018-08-07 13:55:41 +02:00
}
2019-03-15 09:16:41 +01:00
template<bool prettyPrint>
inline
void EncodeDictionary(JsonEncoder<prettyPrint>& stateMachine, const Dictionary::Ptr& dict)
2014-10-26 19:59:49 +01:00
{
2019-03-15 09:16:41 +01:00
stateMachine.StartObject();
2014-10-26 19:59:49 +01:00
ObjectLock olock(dict);
for (const Dictionary::Pair& kv : dict) {
stateMachine.Key(Utility::ValidateUTF8(kv.first));
2019-03-15 09:16:41 +01:00
Encode(stateMachine, kv.second);
2014-10-26 19:59:49 +01:00
}
2019-03-15 09:16:41 +01:00
stateMachine.EndObject();
2014-10-26 19:59:49 +01:00
}
2019-03-15 09:16:41 +01:00
template<bool prettyPrint>
inline
void EncodeArray(JsonEncoder<prettyPrint>& stateMachine, const Array::Ptr& arr)
2014-10-26 19:59:49 +01:00
{
2019-03-15 09:16:41 +01:00
stateMachine.StartArray();
2014-10-26 19:59:49 +01:00
ObjectLock olock(arr);
for (const Value& value : arr) {
2019-03-15 09:16:41 +01:00
Encode(stateMachine, value);
2014-10-26 19:59:49 +01:00
}
2019-03-15 09:16:41 +01:00
stateMachine.EndArray();
2014-10-26 19:59:49 +01:00
}
2019-03-15 09:16:41 +01:00
template<bool prettyPrint>
void Encode(JsonEncoder<prettyPrint>& stateMachine, const Value& value)
2014-10-26 19:59:49 +01:00
{
switch (value.GetType()) {
case ValueNumber:
2019-03-15 09:16:41 +01:00
stateMachine.NumberFloat(value.Get<double>());
break;
2019-03-15 09:16:41 +01:00
case ValueBoolean:
stateMachine.Boolean(value.ToBool());
2014-10-26 19:59:49 +01:00
break;
2019-03-15 09:16:41 +01:00
case ValueString:
stateMachine.Strng(Utility::ValidateUTF8(value.Get<String>()));
2014-10-26 19:59:49 +01:00
break;
2019-03-15 09:16:41 +01:00
2014-10-26 19:59:49 +01:00
case ValueObject:
{
const Object::Ptr& obj = value.Get<Object::Ptr>();
2018-08-07 13:55:41 +02:00
2019-03-15 09:16:41 +01:00
{
Namespace::Ptr ns = dynamic_pointer_cast<Namespace>(obj);
if (ns) {
EncodeNamespace(stateMachine, ns);
break;
}
2018-08-07 13:55:41 +02:00
}
2019-03-15 09:16:41 +01:00
{
Dictionary::Ptr dict = dynamic_pointer_cast<Dictionary>(obj);
if (dict) {
EncodeDictionary(stateMachine, dict);
break;
}
}
Array::Ptr arr = dynamic_pointer_cast<Array>(obj);
if (arr) {
2019-03-15 09:16:41 +01:00
EncodeArray(stateMachine, arr);
break;
}
}
2019-03-15 09:16:41 +01:00
stateMachine.Null();
2014-10-26 19:59:49 +01:00
break;
2019-03-15 09:16:41 +01:00
case ValueEmpty:
stateMachine.Null();
2014-10-26 19:59:49 +01:00
break;
2019-03-15 09:16:41 +01:00
2014-10-26 19:59:49 +01:00
default:
VERIFY(!"Invalid variant type.");
}
}
String icinga::JsonEncode(const Value& value, bool pretty_print)
2014-10-26 19:59:49 +01:00
{
2019-03-15 09:16:41 +01:00
if (pretty_print) {
JsonEncoder<true> stateMachine;
2014-10-26 19:59:49 +01:00
2019-03-15 09:16:41 +01:00
Encode(stateMachine, value);
2019-03-15 09:16:41 +01:00
return stateMachine.GetResult();
} else {
JsonEncoder<false> stateMachine;
2014-10-26 19:59:49 +01:00
2019-03-15 09:16:41 +01:00
Encode(stateMachine, value);
2014-10-26 19:59:49 +01:00
2019-03-15 09:16:41 +01:00
return stateMachine.GetResult();
}
2014-10-26 19:59:49 +01:00
}
Value icinga::JsonDecode(const String& data)
2014-10-26 19:59:49 +01:00
{
String sanitized (Utility::ValidateUTF8(data));
JsonSax stateMachine;
2014-10-26 19:59:49 +01:00
nlohmann::json::sax_parse(sanitized.Begin(), sanitized.End(), &stateMachine);
2014-10-26 19:59:49 +01:00
return stateMachine.GetResult();
}
2014-10-26 19:59:49 +01:00
inline
bool JsonSax::null()
{
FillCurrentTarget(Value());
2014-10-26 19:59:49 +01:00
return true;
}
2014-10-26 19:59:49 +01:00
inline
bool JsonSax::boolean(bool val)
{
FillCurrentTarget(val);
2014-10-26 19:59:49 +01:00
return true;
}
2014-10-26 19:59:49 +01:00
inline
bool JsonSax::number_integer(JsonSax::number_integer_t val)
2014-10-26 19:59:49 +01:00
{
FillCurrentTarget((double)val);
2014-10-26 19:59:49 +01:00
return true;
2014-10-26 19:59:49 +01:00
}
inline
bool JsonSax::number_unsigned(JsonSax::number_unsigned_t val)
2014-10-26 19:59:49 +01:00
{
FillCurrentTarget((double)val);
2014-10-26 19:59:49 +01:00
return true;
2014-10-26 19:59:49 +01:00
}
inline
bool JsonSax::number_float(JsonSax::number_float_t val, const JsonSax::string_t&)
2014-10-26 19:59:49 +01:00
{
FillCurrentTarget((double)val);
2014-10-26 19:59:49 +01:00
return true;
2014-10-26 19:59:49 +01:00
}
inline
bool JsonSax::string(JsonSax::string_t& val)
2014-10-26 19:59:49 +01:00
{
FillCurrentTarget(String(std::move(val)));
2014-10-26 19:59:49 +01:00
return true;
2014-10-26 19:59:49 +01:00
}
inline
bool JsonSax::start_object(std::size_t)
2014-10-26 19:59:49 +01:00
{
auto object (new Dictionary());
2014-10-26 19:59:49 +01:00
FillCurrentTarget(object);
2019-03-15 11:53:56 +01:00
m_CurrentSubtree.push({object, nullptr});
2014-10-26 19:59:49 +01:00
return true;
2014-10-26 19:59:49 +01:00
}
inline
bool JsonSax::key(JsonSax::string_t& val)
2014-10-26 19:59:49 +01:00
{
m_CurrentKey = String(std::move(val));
2014-10-26 19:59:49 +01:00
return true;
2014-10-26 19:59:49 +01:00
}
inline
bool JsonSax::end_object()
2014-10-26 19:59:49 +01:00
{
m_CurrentSubtree.pop();
m_CurrentKey = String();
2017-12-13 12:54:14 +01:00
return true;
2014-10-26 19:59:49 +01:00
}
inline
bool JsonSax::start_array(std::size_t)
2014-10-26 19:59:49 +01:00
{
auto array (new Array());
2014-10-26 19:59:49 +01:00
FillCurrentTarget(array);
2014-10-26 19:59:49 +01:00
2019-03-15 11:53:56 +01:00
m_CurrentSubtree.push({nullptr, array});
2014-10-26 19:59:49 +01:00
return true;
}
2014-10-26 19:59:49 +01:00
inline
bool JsonSax::end_array()
{
m_CurrentSubtree.pop();
2014-10-26 19:59:49 +01:00
return true;
}
2014-10-26 19:59:49 +01:00
inline
bool JsonSax::parse_error(std::size_t, const std::string&, const nlohmann::detail::exception& ex)
{
throw std::invalid_argument(ex.what());
}
inline
Value JsonSax::GetResult()
{
return m_Root;
}
2014-10-26 19:59:49 +01:00
inline
void JsonSax::FillCurrentTarget(Value value)
{
if (m_CurrentSubtree.empty()) {
m_Root = value;
} else {
auto& node (m_CurrentSubtree.top());
2014-10-26 19:59:49 +01:00
2019-03-15 11:53:56 +01:00
if (node.first) {
node.first->Set(m_CurrentKey, value);
} else {
2019-03-15 11:53:56 +01:00
node.second->Add(value);
}
}
2014-10-26 19:59:49 +01:00
}
2019-03-15 09:16:41 +01:00
template<bool prettyPrint>
inline
void JsonEncoder<prettyPrint>::Null()
{
BeforeItem();
AppendChars((const char*)l_Null, (const char*)l_Null + 4);
}
template<bool prettyPrint>
inline
void JsonEncoder<prettyPrint>::Boolean(bool value)
{
BeforeItem();
if (value) {
AppendChars((const char*)l_True, (const char*)l_True + 4);
} else {
AppendChars((const char*)l_False, (const char*)l_False + 5);
}
}
template<bool prettyPrint>
inline
void JsonEncoder<prettyPrint>::NumberFloat(double value)
{
BeforeItem();
AppendJson(value);
}
template<bool prettyPrint>
inline
void JsonEncoder<prettyPrint>::Strng(String value)
{
BeforeItem();
AppendJson(std::move(value));
}
template<bool prettyPrint>
inline
void JsonEncoder<prettyPrint>::StartObject()
{
BeforeItem();
AppendChar('{');
m_CurrentSubtree.push(2);
}
template<bool prettyPrint>
inline
void JsonEncoder<prettyPrint>::Key(String value)
{
m_CurrentKey = std::move(value);
}
template<bool prettyPrint>
inline
void JsonEncoder<prettyPrint>::EndObject()
{
FinishContainer('}');
}
template<bool prettyPrint>
inline
void JsonEncoder<prettyPrint>::StartArray()
{
BeforeItem();
AppendChar('[');
m_CurrentSubtree.push(0);
}
template<bool prettyPrint>
inline
void JsonEncoder<prettyPrint>::EndArray()
{
FinishContainer(']');
}
template<bool prettyPrint>
inline
String JsonEncoder<prettyPrint>::GetResult()
{
return String(m_Result.begin(), m_Result.end());
}
template<bool prettyPrint>
inline
void JsonEncoder<prettyPrint>::AppendChar(char c)
{
m_Result.emplace_back(c);
}
template<bool prettyPrint>
template<class Iterator>
inline
void JsonEncoder<prettyPrint>::AppendChars(Iterator begin, Iterator end)
{
m_Result.insert(m_Result.end(), begin, end);
}
template<bool prettyPrint>
inline
void JsonEncoder<prettyPrint>::AppendJson(nlohmann::json json)
{
nlohmann::detail::serializer<nlohmann::json>(nlohmann::detail::output_adapter<char>(m_Result), ' ').dump(std::move(json), prettyPrint, true, 0);
}
template<bool prettyPrint>
inline
void JsonEncoder<prettyPrint>::BeforeItem()
{
if (!m_CurrentSubtree.empty()) {
auto& node (m_CurrentSubtree.top());
if (node[0]) {
AppendChar(',');
} else {
node[0] = true;
}
if (prettyPrint) {
AppendChar('\n');
for (auto i (m_CurrentSubtree.size()); i; --i) {
AppendChars((const char*)l_Indent, (const char*)l_Indent + 4);
}
}
if (node[1]) {
AppendJson(std::move(m_CurrentKey));
AppendChar(':');
if (prettyPrint) {
AppendChar(' ');
}
}
}
}
template<bool prettyPrint>
inline
void JsonEncoder<prettyPrint>::FinishContainer(char terminator)
{
if (prettyPrint && m_CurrentSubtree.top()[0]) {
AppendChar('\n');
for (auto i (m_CurrentSubtree.size() - 1u); i; --i) {
AppendChars((const char*)l_Indent, (const char*)l_Indent + 4);
}
}
AppendChar(terminator);
m_CurrentSubtree.pop();
}