Merge pull request #4676 from jmarshallnz/dont_set_scraper_on_tvshow_on_nfo
[vuplus_xbmc] / xbmc / utils / JSONVariantParser.h
1 #pragma once
2 /*
3  *      Copyright (C) 2005-2013 Team XBMC
4  *      http://xbmc.org
5  *
6  *  This Program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  *
11  *  This Program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with XBMC; see the file COPYING.  If not, see
18  *  <http://www.gnu.org/licenses/>.
19  *
20  */
21
22 #include "system.h"
23 #include "Variant.h"
24
25 #include <yajl/yajl_parse.h>
26 #include <yajl/yajl_gen.h>
27 #ifdef HAVE_YAJL_YAJL_VERSION_H
28 #include <yajl/yajl_version.h>
29 #endif
30
31 class IParseCallback
32 {
33 public:
34   virtual ~IParseCallback() { }
35
36   virtual void onParsed(CVariant *variant) = 0;
37 };
38
39 class CSimpleParseCallback : public IParseCallback
40 {
41 public:
42   virtual void onParsed(CVariant *variant) { m_parsed = *variant; }
43   CVariant &GetOutput() { return m_parsed; }
44
45 private:
46   CVariant m_parsed;
47 };
48
49 class CJSONVariantParser
50 {
51 public:
52   CJSONVariantParser(IParseCallback *callback);
53   ~CJSONVariantParser();
54
55   void push_buffer(const unsigned char *buffer, unsigned int length);
56
57   static CVariant Parse(const unsigned char *json, unsigned int length);
58
59 private:
60   static int ParseNull(void * ctx);
61   static int ParseBoolean(void * ctx, int boolean);
62 #if YAJL_MAJOR == 2
63   static int ParseInteger(void * ctx, long long integerVal);
64 #else
65   static int ParseInteger(void * ctx, long integerVal);
66 #endif
67   static int ParseDouble(void * ctx, double doubleVal);
68 #if YAJL_MAJOR == 2
69   static int ParseString(void * ctx, const unsigned char * stringVal, size_t stringLen);
70 #else
71   static int ParseString(void * ctx, const unsigned char * stringVal, unsigned int stringLen);
72 #endif
73   static int ParseMapStart(void * ctx);
74 #if YAJL_MAJOR == 2
75   static int ParseMapKey(void * ctx, const unsigned char * stringVal, size_t stringLen);
76 #else
77   static int ParseMapKey(void * ctx, const unsigned char * stringVal, unsigned int stringLen);
78 #endif
79   static int ParseMapEnd(void * ctx);
80   static int ParseArrayStart(void * ctx);
81   static int ParseArrayEnd(void * ctx);
82
83   void PushObject(CVariant variant);
84   void PopObject();
85
86   static yajl_callbacks callbacks;
87
88   IParseCallback *m_callback;
89   yajl_handle m_handler;
90
91   CVariant m_parsedObject;
92   std::vector<CVariant *> m_parse;
93   std::string m_key;
94
95   enum PARSE_STATUS
96   {
97     ParseArray = 1,
98     ParseObject = 2,
99     ParseVariable = 0
100   };
101   PARSE_STATUS m_status;
102 };