2  *    This program is free software; you can redistribute it and/or modify
 
   3  *    it under the terms of the GNU General Public License as published by
 
   4  *    the Free Software Foundation; either version 2 of the License, or
 
   5  *    (at your option) any later version.
 
   7  *    This program is distributed in the hope that it will be useful,
 
   8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
   9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  10  *    GNU General Public License for more details.
 
  12  *    You should have received a copy of the GNU General Public License
 
  13  *    along with this program; if not, write to the Free Software
 
  14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
  18  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
 
  21 #include "stg/json_parser.h"
 
  23 #include <yajl/yajl_parse.h>
 
  25 using STG::JSON::Parser;
 
  26 using STG::JSON::NodeParser;
 
  31         Impl(NodeParser* topParser);
 
  37         bool append(const char* data, size_t size) { return yajl_parse(m_handle, reinterpret_cast<const unsigned char*>(data), size) != yajl_status_ok; }
 
  38         bool done() { return yajl_complete_parse(m_handle) != yajl_status_ok; }
 
  40         static int parseNull(void* ctx)
 
  41         { return runParser(ctx, &NodeParser::parseNull); }
 
  42         static int parseBoolean(void* ctx, int value)
 
  43         { return runParser(ctx, &NodeParser::parseBoolean, value != 0); }
 
  44         static int parseNumber(void* ctx, const char* value, size_t size)
 
  45         { return runParser(ctx, &NodeParser::parseNumber, std::string(value, size)); }
 
  46         static int parseString(void* ctx, const unsigned char* value, size_t size)
 
  47         { return runParser(ctx, &NodeParser::parseString, std::string(reinterpret_cast<const char*>(value), size)); }
 
  48         static int parseStartMap(void* ctx)
 
  49         { return runParser(ctx, &NodeParser::parseStartMap); }
 
  50         static int parseMapKey(void* ctx, const unsigned char* value, size_t size)
 
  51         { return runParser(ctx, &NodeParser::parseMapKey, std::string(reinterpret_cast<const char*>(value), size)); }
 
  52         static int parseEndMap(void* ctx)
 
  53         { return runParser(ctx, &NodeParser::parseEndMap); }
 
  54         static int parseStartArray(void* ctx)
 
  55         { return runParser(ctx, &NodeParser::parseStartArray); }
 
  56         static int parseEndArray(void* ctx)
 
  57         { return runParser(ctx, &NodeParser::parseEndArray); }
 
  63         static yajl_callbacks callbacks;
 
  65         static NodeParser& getParser(void* ctx) { return *static_cast<Impl*>(ctx)->m_parser; }
 
  66         static bool runParser(void* ctx, NodeParser* (NodeParser::*func)())
 
  68             Impl& p = *static_cast<Impl*>(ctx);
 
  69             NodeParser* next = (p.m_parser->*func)();
 
  75         static bool runParser(void* ctx, NodeParser* (NodeParser::*func)(const T&), const T& value)
 
  77             Impl& p = *static_cast<Impl*>(ctx);
 
  78             NodeParser* next = (p.m_parser->*func)(value);
 
  85 yajl_callbacks Parser::Impl::callbacks = {
 
  86     Parser::Impl::parseNull,
 
  87     Parser::Impl::parseBoolean,
 
  88     NULL, // parsing of integer is done using parseNumber
 
  89     NULL, // parsing of double is done using parseNumber
 
  90     Parser::Impl::parseNumber,
 
  91     Parser::Impl::parseString,
 
  92     Parser::Impl::parseStartMap,
 
  93     Parser::Impl::parseMapKey,
 
  94     Parser::Impl::parseEndMap,
 
  95     Parser::Impl::parseStartArray,
 
  96     Parser::Impl::parseEndArray
 
  99 Parser::Impl::Impl(NodeParser* topParser)
 
 100     : m_handle(yajl_alloc(&callbacks, NULL, this)),
 
 105 Parser::Parser(NodeParser* topParser)
 
 106     : m_impl(new Impl(topParser))
 
 114 bool Parser::append(const char* data, size_t size)
 
 116     return m_impl->append(data, size);
 
 121     return m_impl->done();