Skip to content

Commit abfa9ce

Browse files
committed
added ValResponse class to parse JSON from http
1 parent 2d29692 commit abfa9ce

File tree

13 files changed

+460
-178
lines changed

13 files changed

+460
-178
lines changed

libgrive/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ file (GLOB LIBGRIVE_SRC
6565
src/drive2/*.cc
6666
src/http/*.cc
6767
src/protocol/*.cc
68+
src/json/*.cc
6869
src/util/*.cc
6970
src/util/log/*.cc
7071
src/xml/*.cc

libgrive/src/drive2/JsonVal.cc

Lines changed: 0 additions & 135 deletions
This file was deleted.

libgrive/src/json/JsonParser.cc

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
grive: an GPL program to sync a local directory with Google Drive
3+
Copyright (C) 2013 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,
18+
MA 02110-1301, USA.
19+
*/
20+
21+
#include "JsonParser.hh"
22+
23+
#include "Val.hh"
24+
#include "ValBuilder.hh"
25+
26+
#include <yajl/yajl_parse.h>
27+
28+
namespace gr {
29+
30+
namespace
31+
{
32+
int OnNull( void *ctx )
33+
{
34+
ValVisitor *b = reinterpret_cast<ValVisitor*>(ctx) ;
35+
b->VisitNull() ;
36+
return true ;
37+
}
38+
39+
int OnBool( void *ctx, int value )
40+
{
41+
ValVisitor *b = reinterpret_cast<ValVisitor*>(ctx) ;
42+
b->Visit( static_cast<long long>(value) ) ;
43+
return true ;
44+
}
45+
46+
int OnInt( void *ctx, long long value )
47+
{
48+
ValVisitor *b = reinterpret_cast<ValVisitor*>(ctx) ;
49+
b->Visit(value) ;
50+
return true ;
51+
}
52+
53+
int OnDouble( void *ctx, double value )
54+
{
55+
ValVisitor *b = reinterpret_cast<ValVisitor*>(ctx) ;
56+
b->Visit(value) ;
57+
return true ;
58+
}
59+
60+
int OnStr( void *ctx, const unsigned char *str, std::size_t len )
61+
{
62+
ValVisitor *b = reinterpret_cast<ValVisitor*>(ctx) ;
63+
b->Visit( std::string(reinterpret_cast<const char*>(str), len) ) ;
64+
return true ;
65+
}
66+
67+
int StartMap( void *ctx )
68+
{
69+
ValVisitor *b = reinterpret_cast<ValVisitor*>(ctx) ;
70+
b->StartObject() ;
71+
return true ;
72+
}
73+
74+
int OnMapKey( void *ctx, const unsigned char *str, std::size_t len )
75+
{
76+
ValVisitor *b = reinterpret_cast<ValVisitor*>(ctx) ;
77+
b->VisitKey( std::string(reinterpret_cast<const char*>(str), len) ) ;
78+
return true ;
79+
}
80+
81+
int EndMap( void *ctx )
82+
{
83+
ValVisitor *b = reinterpret_cast<ValVisitor*>(ctx) ;
84+
b->EndObject() ;
85+
return true ;
86+
}
87+
88+
int StartArray( void *ctx )
89+
{
90+
ValVisitor *b = reinterpret_cast<ValVisitor*>(ctx) ;
91+
b->StartArray() ;
92+
return true ;
93+
}
94+
95+
int EndArray( void *ctx )
96+
{
97+
ValVisitor *b = reinterpret_cast<ValVisitor*>(ctx) ;
98+
b->EndArray() ;
99+
return true ;
100+
}
101+
102+
const yajl_callbacks callbacks = {
103+
OnNull,
104+
OnBool,
105+
OnInt,
106+
OnDouble,
107+
0,
108+
OnStr,
109+
StartMap,
110+
OnMapKey,
111+
EndMap,
112+
StartArray,
113+
EndArray,
114+
};
115+
}
116+
117+
void JsonParser::Parse( const std::string& json, ValVisitor *callback )
118+
{
119+
JsonParser parser( callback ) ;
120+
parser.Parse( json.c_str(), json.size() ) ;
121+
parser.Finish() ;
122+
}
123+
124+
struct JsonParser::Impl
125+
{
126+
ValVisitor *callback ;
127+
yajl_handle hand ;
128+
} ;
129+
130+
JsonParser::JsonParser( ValVisitor *callback ) :
131+
m_impl( new Impl )
132+
{
133+
m_impl->callback = callback ;
134+
m_impl->hand = yajl_alloc( &callbacks, 0, m_impl->callback ) ;
135+
}
136+
137+
JsonParser::~JsonParser()
138+
{
139+
yajl_free( m_impl->hand ) ;
140+
}
141+
142+
void JsonParser::Parse( const char *str, std::size_t size )
143+
{
144+
const unsigned char *ustr = reinterpret_cast<unsigned const char*>(str) ;
145+
146+
yajl_status r = yajl_parse( m_impl->hand, ustr, size ) ;
147+
148+
if ( r != yajl_status_ok )
149+
{
150+
unsigned char *msg = yajl_get_error( m_impl->hand, true, ustr, size ) ;
151+
std::string msg_str(reinterpret_cast<char*>(msg)) ;
152+
yajl_free_error(m_impl->hand, msg) ;
153+
154+
BOOST_THROW_EXCEPTION(
155+
Error()
156+
<< ParseErr_(msg_str)
157+
<< JsonText_(std::string(str,size))
158+
);
159+
}
160+
}
161+
162+
void JsonParser::Finish()
163+
{
164+
if ( yajl_complete_parse(m_impl->hand) != yajl_status_ok )
165+
{
166+
unsigned char *msg = yajl_get_error( m_impl->hand, false, 0, 0 ) ;
167+
std::string msg_str(reinterpret_cast<char*>(msg)) ;
168+
yajl_free_error(m_impl->hand, msg) ;
169+
170+
BOOST_THROW_EXCEPTION( Error() << ParseErr_(msg_str) ) ;
171+
}
172+
}
173+
174+
} // end of namespace gr::json

libgrive/src/json/JsonParser.hh

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
grive: an GPL program to sync a local directory with Google Drive
3+
Copyright (C) 2013 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,
18+
MA 02110-1301, USA.
19+
*/
20+
21+
#pragma once
22+
23+
#include "util/Exception.hh"
24+
25+
#include <string>
26+
#include <memory>
27+
28+
namespace gr {
29+
30+
class ValVisitor ;
31+
32+
class JsonParser
33+
{
34+
public :
35+
struct Error : virtual Exception {} ;
36+
typedef boost::error_info<struct ParseErr, std::string> ParseErr_ ;
37+
typedef boost::error_info<struct JsonText, std::string> JsonText_ ;
38+
39+
static void Parse( const std::string& json, ValVisitor *callback ) ;
40+
41+
explicit JsonParser( ValVisitor *callback ) ;
42+
~JsonParser() ;
43+
44+
void Parse( const char *str, std::size_t size ) ;
45+
void Finish() ;
46+
47+
private :
48+
struct Impl ;
49+
std::auto_ptr<Impl> m_impl ;
50+
} ;
51+
52+
} // end of namespace
53+

0 commit comments

Comments
 (0)