From: Gunnar Beutner Date: Mon, 9 Feb 2015 07:12:42 +0000 (+0100) Subject: Implement the Json.encode and Json.decode methods X-Git-Tag: v2.3.0~266 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bb12704a3102f5f8cf7befeee844ad98dac25890;p=icinga2 Implement the Json.encode and Json.decode methods fixes #8393 --- diff --git a/doc/16-library-reference.md b/doc/16-library-reference.md index f14829f05..2130eab07 100644 --- a/doc/16-library-reference.md +++ b/doc/16-library-reference.md @@ -223,6 +223,26 @@ Signature: Returns the tangent of `x`. +## Json object + +The global `Json` object can be used to encode and decode JSON. + +### Json.encode + +Signature: + + function encode(x); + +Encodes an arbitrary value into JSON. + +### Json.decode + +Signature: + + function decode(x); + +Decodes a JSON string. + ## Number type ### Number#to_string diff --git a/lib/base/CMakeLists.txt b/lib/base/CMakeLists.txt index e992febaf..ed60d2aa7 100644 --- a/lib/base/CMakeLists.txt +++ b/lib/base/CMakeLists.txt @@ -25,7 +25,7 @@ mkclass_target(sysloglogger.ti sysloglogger.thpp) set(base_SOURCES application.cpp application-version.cpp application.thpp array.cpp array-script.cpp boolean.cpp boolean-script.cpp console.cpp context.cpp convert.cpp debuginfo.cpp dictionary.cpp dictionary-script.cpp dynamicobject.cpp dynamicobject.thpp dynamictype.cpp - exception.cpp fifo.cpp filelogger.cpp filelogger.thpp initialize.cpp json.cpp logger.cpp logger.thpp math-script.cpp + exception.cpp fifo.cpp filelogger.cpp filelogger.thpp initialize.cpp json.cpp json-script.cpp logger.cpp logger.thpp math-script.cpp netstring.cpp networkstream.cpp number.cpp number-script.cpp object.cpp object-script.cpp primitivetype.cpp process.cpp ringbuffer.cpp scriptframe.cpp function.cpp function-script.cpp functionwrapper.cpp scriptglobal.cpp scriptutils.cpp serializer.cpp socket.cpp stacktrace.cpp diff --git a/lib/base/json-script.cpp b/lib/base/json-script.cpp new file mode 100644 index 000000000..50e5fa7b5 --- /dev/null +++ b/lib/base/json-script.cpp @@ -0,0 +1,46 @@ +/****************************************************************************** + * Icinga 2 * + * Copyright (C) 2012-2015 Icinga Development Team (http://www.icinga.org) * + * * + * 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/dictionary.hpp" +#include "base/function.hpp" +#include "base/functionwrapper.hpp" +#include "base/scriptframe.hpp" +#include "base/initialize.hpp" +#include "base/json.hpp" + +using namespace icinga; + +static String JsonEncodeShim(const Value& value) +{ + return JsonEncode(value); +} + +static void InitializeJsonObj(void) +{ + Dictionary::Ptr jsonObj = new Dictionary(); + + /* Methods */ + jsonObj->Set("encode", new Function(WrapFunction(JsonEncodeShim))); + jsonObj->Set("decode", new Function(WrapFunction(JsonDecode))); + + ScriptGlobal::Set("Json", jsonObj); +} + +INITIALIZE_ONCE(InitializeJsonObj); + diff --git a/lib/config/config_parser.yy b/lib/config/config_parser.yy index 357bf40fc..7b17164b8 100644 --- a/lib/config/config_parser.yy +++ b/lib/config/config_parser.yy @@ -449,7 +449,7 @@ object: context->m_Assign.push(NULL); context->m_Ignore.push(NULL); } - object_declaration identifier rterm use_specifier rterm_scope_require_side_effect + object_declaration identifier optional_rterm use_specifier rterm_scope_require_side_effect { context->m_ObjectAssign.pop(); diff --git a/lib/config/configitembuilder.cpp b/lib/config/configitembuilder.cpp index 0ebe2b75f..542c56aab 100644 --- a/lib/config/configitembuilder.cpp +++ b/lib/config/configitembuilder.cpp @@ -89,12 +89,6 @@ ConfigItem::Ptr ConfigItemBuilder::Compile(void) BOOST_THROW_EXCEPTION(std::invalid_argument(msgbuf.str())); } - if (m_Name.IsEmpty()) { - std::ostringstream msgbuf; - msgbuf << "The name of an object may not be empty: " << m_DebugInfo; - BOOST_THROW_EXCEPTION(std::invalid_argument(msgbuf.str())); - } - std::vector exprs; Array::Ptr templateArray = new Array();