X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/8c6fa3fbaccc22127280bf77a48fab5a3ee0716e..46b0747592074017ff0ea4b33d4a7194235886e5:/libs/json/parser.cpp diff --git a/libs/json/parser.cpp b/libs/json/parser.cpp new file mode 100644 index 00000000..57115048 --- /dev/null +++ b/libs/json/parser.cpp @@ -0,0 +1,123 @@ +/* + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#include "stg/json_parser.h" + +#include + +using STG::JSON::Parser; +using STG::JSON::NodeParser; + +class Parser::Impl +{ + public: + Impl(NodeParser* topParser); + ~Impl() + { + yajl_free(m_handle); + } + + bool append(const char* data, size_t size) { return yajl_parse(m_handle, reinterpret_cast(data), size) == yajl_status_ok; } + bool last() { return yajl_complete_parse(m_handle) == yajl_status_ok; } + + static int parseNull(void* ctx) + { return runParser(ctx, &NodeParser::parseNull); } + static int parseBoolean(void* ctx, int value) + { return runParser(ctx, &NodeParser::parseBoolean, value != 0); } + static int parseNumber(void* ctx, const char* value, size_t size) + { return runParser(ctx, &NodeParser::parseNumber, std::string(value, size)); } + static int parseString(void* ctx, const unsigned char* value, size_t size) + { return runParser(ctx, &NodeParser::parseString, std::string(reinterpret_cast(value), size)); } + static int parseStartMap(void* ctx) + { return runParser(ctx, &NodeParser::parseStartMap); } + static int parseMapKey(void* ctx, const unsigned char* value, size_t size) + { return runParser(ctx, &NodeParser::parseMapKey, std::string(reinterpret_cast(value), size)); } + static int parseEndMap(void* ctx) + { return runParser(ctx, &NodeParser::parseEndMap); } + static int parseStartArray(void* ctx) + { return runParser(ctx, &NodeParser::parseStartArray); } + static int parseEndArray(void* ctx) + { return runParser(ctx, &NodeParser::parseEndArray); } + + private: + yajl_handle m_handle; + NodeParser* m_parser; + + static yajl_callbacks callbacks; + + static NodeParser& getParser(void* ctx) { return *static_cast(ctx)->m_parser; } + static bool runParser(void* ctx, NodeParser* (NodeParser::*func)()) + { + Impl& p = *static_cast(ctx); + NodeParser* next = (p.m_parser->*func)(); + if (next != NULL) + p.m_parser = next; + return next != NULL; + } + template + static bool runParser(void* ctx, NodeParser* (NodeParser::*func)(const T&), const T& value) + { + Impl& p = *static_cast(ctx); + NodeParser* next = (p.m_parser->*func)(value); + if (next != NULL) + p.m_parser = next; + return next != NULL; + } +}; + +yajl_callbacks Parser::Impl::callbacks = { + Parser::Impl::parseNull, + Parser::Impl::parseBoolean, + NULL, // parsing of integer is done using parseNumber + NULL, // parsing of double is done using parseNumber + Parser::Impl::parseNumber, + Parser::Impl::parseString, + Parser::Impl::parseStartMap, + Parser::Impl::parseMapKey, + Parser::Impl::parseEndMap, + Parser::Impl::parseStartArray, + Parser::Impl::parseEndArray +}; + +Parser::Impl::Impl(NodeParser* topParser) + : m_handle(yajl_alloc(&callbacks, NULL, this)), + m_parser(topParser) +{ + yajl_config(m_handle, yajl_allow_multiple_values, 1); +} + +Parser::Parser(NodeParser* topParser) + : m_impl(new Impl(topParser)) +{ +} + +Parser::~Parser() +{ +} + +bool Parser::append(const char* data, size_t size) +{ + return m_impl->append(data, size); +} + +bool Parser::last() +{ + return m_impl->last(); +}