autojson  0.1
A JSON parser base on the automaton provided by json.org
 All Classes Files Functions Typedefs Enumerations Enumerator Pages
ObjectReactor.hh
1 /*
2  autojson: A JSON parser base on the automaton provided by json.org
3  Copyright (C) 2014 Wan Wai Ho
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  as published by the Free Software Foundation version 2
8  of the License.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  02110-1301, USA.
19 */
20 
21 #ifndef OBJECTREACTOR_HH_INCLUDED
22 #define OBJECTREACTOR_HH_INCLUDED
23 
24 #include "Reactor.hh"
25 
26 #include <map>
27 #include <memory>
28 #include <vector>
29 
30 namespace ajs {
31 
34 template <typename DestType>
35 class ObjectReactor : public Reactor
36 {
37 public:
38  ObjectReactor() : m_next(nullptr)
39  {
40  }
41 
42  ObjectReactor(const ObjectReactor& rhs) : m_next(rhs.m_next)
43  {
44  for (const auto& p : rhs.m_actions)
45  m_actions.insert(std::make_pair(p.first, ReactorPtr(p.second->Clone())));
46  }
47 
49  m_actions(std::move(rhs.m_actions)),
50  m_next(rhs.m_next)
51  {
52  }
53 
54  template <typename T>
55  ObjectReactor& Map(const std::string& key, T DestType::*member)
56  {
57  m_actions.insert(
58  std::make_pair(key, ReactorPtr(new SaveToMember<DestType,T>(member))));
59  return *this;
60  }
61 
62  ObjectReactor* Clone() const override
63  {
64  return new ObjectReactor(*this);
65  }
66 
67  ParseState On(ParseState& s, JSON_event event, const char *data, std::size_t len) override
68  {
69  if (event == JSON_object_key)
70  {
71  auto i = m_actions.find(std::string(data,len)) ;
72  m_next = (i != m_actions.end() ? i->second.get() : nullptr);
73  }
74  else if (m_next != nullptr)
75  {
76  switch (event)
77  {
78  case JSON_string:
79  case JSON_null:
80  case JSON_true:
81  case JSON_false:
82  case JSON_number:
83  m_next->On(s, event, data, len);
84  break;
85 
86  case JSON_object_start:
87  default:
88  break;
89  }
90  // only use once
91  m_next = nullptr;
92  }
93  return s;
94  }
95 
96 private :
97  typedef std::unique_ptr<Reactor> ReactorPtr;
98 
99  std::map<std::string, ReactorPtr> m_actions;
100  Reactor *m_next;
101 };
102 
103 } // end of namespace
104 
105 #endif
Literal value "false".
Definition: JSON_checker.h:36
Definition: JSON_checker.h:30
Definition: Reactor.hh:49
Literal value "null".
Definition: JSON_checker.h:34
Start of a JSON object.
Definition: JSON_checker.h:25
Definition: JSON_checker.h:32
Literal value "true".
Definition: JSON_checker.h:35
JSON_event
Definition: JSON_checker.h:21
Definition: Reactor.hh:42
Definition: JSON_checker.h:26
Definition: ObjectReactor.hh:35
Definition: Reactor.hh:35