Skip to content

Commit 27817e8

Browse files
committed
using the new Json classes for bgrive
1 parent 6ba04dc commit 27817e8

File tree

8 files changed

+114
-39
lines changed

8 files changed

+114
-39
lines changed

libgrive/src/drive2/Drive.cc

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,12 @@
2222

2323
#include "CommonUri.hh"
2424
#include "Feed.hh"
25-
#include "protocol/Json.hh"
25+
#include "json/Val.hh"
2626
#include "util/Exception.hh"
2727

28+
#include <boost/bind.hpp>
29+
30+
#include <algorithm>
2831
#include <iostream>
2932
#include <iterator>
3033

@@ -74,8 +77,8 @@ void Drive::NewResource( http::Agent *agent, Feed& items )
7477

7578
while ( items.Next( agent ) )
7679
{
77-
std::vector<Json> item_json = items.Content()["items"].AsArray() ;
78-
for ( std::vector<Json>::iterator i = item_json.begin() ; i != item_json.end() ; ++i )
80+
std::vector<Val> item_json = items.Content()["items"].AsArray() ;
81+
for ( std::vector<Val>::iterator i = item_json.begin() ; i != item_json.end() ; ++i )
7982
NewResource( *i ) ;
8083
}
8184
}
@@ -89,16 +92,19 @@ Resource* Drive::NewResource( http::Agent *agent, const std::string& id )
8992
return NewResource( feed.Content() ) ;
9093
}
9194

92-
Resource* Drive::NewResource( const Json& item )
95+
Resource* Drive::NewResource( const Val& item )
9396
{
9497
// assume resource is directly under root
9598
std::string parent_id = m_root != 0 ? m_root->ID() : "" ;
9699

97-
Json parents ;
100+
Val parents ;
98101
if ( item.Get( "parents", parents ) )
99102
{
103+
std::vector<Val> pids_val = parents.Select( "id" ) ;
100104
std::vector<std::string> pids ;
101-
parents.Select<std::string>( "id", std::back_inserter(pids) ) ;
105+
std::transform( pids_val.begin(), pids_val.end(),
106+
std::back_inserter( pids ),
107+
boost::bind( &Val::Str, _1 ) ) ;
102108

103109
// only the first parent counts
104110
if ( !pids.empty() )

libgrive/src/drive2/Drive.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ namespace http
3434
class Agent ;
3535
}
3636

37-
class Json ;
37+
class Val ;
3838

3939
namespace v2 {
4040

@@ -76,7 +76,7 @@ public :
7676
const Resource* Parent( const Resource *child ) const ;
7777

7878
private :
79-
Resource* NewResource( const Json& item ) ;
79+
Resource* NewResource( const Val& item ) ;
8080
Resource* NewResource( http::Agent *agent, const std::string& id ) ;
8181
void NewResource( http::Agent *agent, Feed& items ) ;
8282

libgrive/src/drive2/Feed.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include "http/Agent.hh"
2323
#include "http/Header.hh"
24-
#include "protocol/JsonResponse.hh"
24+
#include "json/ValResponse.hh"
2525

2626
#include <iostream>
2727

@@ -31,22 +31,22 @@ Feed::Feed( const std::string& base ) :
3131
m_base( base )
3232
{
3333
// Next() will grab this link
34-
m_content.Add( "nextLink", Json(base) ) ;
34+
m_content.Add( "nextLink", Val(base) ) ;
3535
}
3636

3737
void Feed::Query( const std::string& field, const std::string& value )
3838
{
3939
std::string url = m_content["nextLink"].Str() ;
40-
m_content.Add( "nextLink", Json( url + "?q=" + field + "+%3d+%27" + value + "%27" ) ) ;
40+
m_content.Add( "nextLink", Val( url + "?q=" + field + "+%3d+%27" + value + "%27" ) ) ;
4141
}
4242

4343
bool Feed::Next( http::Agent *agent )
4444
{
45-
Json url ;
45+
Val url ;
4646
if ( !m_content.Get("nextLink", url) )
4747
return false ;
4848

49-
http::JsonResponse out ;
49+
http::ValResponse out ;
5050
try
5151
{
5252
agent->Get( url.Str(), &out, http::Header() ) ;
@@ -61,7 +61,7 @@ bool Feed::Next( http::Agent *agent )
6161
return true ;
6262
}
6363

64-
Json Feed::Content() const
64+
Val Feed::Content() const
6565
{
6666
return m_content ;
6767
}

libgrive/src/drive2/Feed.hh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
#pragma once
2121

22-
#include "protocol/Json.hh"
22+
#include "json/Val.hh"
2323
#include "util/Exception.hh"
2424

2525
#include <string>
@@ -32,15 +32,15 @@ namespace http
3232
class Header ;
3333
}
3434

35-
class Json ;
35+
class Val ;
3636

3737
namespace v2 {
3838

3939
class Feed
4040
{
4141
public :
4242
// exception info
43-
typedef boost::error_info<struct DriveFeed, Json> DriveFeed_ ;
43+
typedef boost::error_info<struct DriveFeed, Val> DriveFeed_ ;
4444

4545
public :
4646
Feed( const std::string& base ) ;
@@ -49,11 +49,11 @@ public :
4949

5050
bool Next( http::Agent *agent ) ;
5151

52-
Json Content() const ;
52+
Val Content() const ;
5353

5454
private :
5555
std::string m_base ;
56-
Json m_content ;
56+
Val m_content ;
5757
} ;
5858

5959
} } // end of namespace gr::v2

libgrive/src/json/Val.cc

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,32 @@
2727

2828
namespace gr {
2929

30+
const Val& Val::Null()
31+
{
32+
static const Val null( null_type ) ;
33+
return null ;
34+
}
35+
3036
Val::Val( ) :
31-
m_base( new Impl<void> )
37+
m_base( new Impl<Object> )
3238
{
3339
}
3440

41+
Val::Val( TypeEnum type )
42+
{
43+
switch ( type )
44+
{
45+
case int_type: m_base.reset( new Impl<long long> ) ; break ;
46+
case bool_type: m_base.reset( new Impl<bool> ) ; break ;
47+
case double_type: m_base.reset( new Impl<double> ) ; break ;
48+
case string_type: m_base.reset( new Impl<std::string> ) ; break ;
49+
case array_type: m_base.reset( new Impl<Array> ) ; break ;
50+
case object_type: m_base.reset( new Impl<Object> ) ; break ;
51+
case null_type:
52+
default: m_base.reset( new Impl<void> ) ; break ;
53+
}
54+
}
55+
3556
Val::Val( const Val& v ) :
3657
m_base( v.m_base->Clone() )
3758
{
@@ -174,6 +195,35 @@ void Val::Visit( ValVisitor *visitor ) const
174195
}
175196
}
176197

198+
void Val::Select( const Object& obj, const std::string& key, std::vector<Val>& result ) const
199+
{
200+
Object::const_iterator i = obj.find(key) ;
201+
if ( i != obj.end() )
202+
result.push_back(i->second) ;
203+
}
204+
205+
/** If \a this is an array of objects, this function returns all values of
206+
the objects in the array with the key \a key. If \a this is an object,
207+
just return the value with the key \a key.
208+
*/
209+
std::vector<Val> Val::Select( const std::string& key ) const
210+
{
211+
std::vector<Val> result ;
212+
if ( Is<Object>() )
213+
Select( As<Object>(), key, result ) ;
214+
215+
else if ( Is<Array>() )
216+
{
217+
const Array& array = As<Array>() ;
218+
for ( Array::const_iterator i = array.begin() ; i != array.end() ; ++i )
219+
{
220+
if ( i->Is<Object>() )
221+
Select( i->As<Object>(), key, result ) ;
222+
}
223+
}
224+
return result ;
225+
}
226+
177227
std::ostream& operator<<( std::ostream& os, const Val& val )
178228
{
179229
StdStream ss( os.rdbuf() ) ;

libgrive/src/json/Val.hh

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,11 @@ public :
5858
public :
5959
Val() ;
6060
Val( const Val& v ) ;
61+
explicit Val( TypeEnum type ) ;
6162
~Val() ;
6263

64+
static const Val& Null() ;
65+
6366
template <typename T>
6467
explicit Val( const T& t )
6568
{
@@ -110,19 +113,22 @@ public :
110113
void Add( const Val& json ) ;
111114
Val FindInArray( const std::string& key, const std::string& value ) const ;
112115
bool FindInArray( const std::string& key, const std::string& value, Val& result ) const ;
113-
116+
117+
std::vector<Val> Select( const std::string& key ) const ;
118+
114119
friend std::ostream& operator<<( std::ostream& os, const Val& val ) ;
115120
void Visit( ValVisitor *visitor ) const ;
116121

117122
private :
118123
struct Base ;
119-
124+
120125
template <typename T>
121126
struct Impl ;
122127

123128
std::auto_ptr<Base> m_base ;
124129

125130
private :
131+
void Select( const Object& obj, const std::string& key, std::vector<Val>& result ) const ;
126132
} ;
127133

128134
template <> struct Val::Type2Enum<void> { static const TypeEnum type = null_type ; } ;
@@ -160,6 +166,7 @@ template <typename T>
160166
struct Val::Impl : public Base
161167
{
162168
T val ;
169+
Impl( ) : val() {}
163170
Impl( const T& t ) : val(t) {}
164171
Impl<T>* Clone() const { return new Impl<T>(val); }
165172
TypeEnum Type() const { return Type2Enum<T>::type ; }

libgrive/src/json/ValBuilder.cc

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,37 +60,41 @@ void ValBuilder::VisitNull()
6060
void ValBuilder::Build( const Val& t )
6161
{
6262
if ( m_ctx.empty() )
63-
m_ctx.push( t ) ;
63+
{
64+
Level l = { Val::Null(), t } ;
65+
m_ctx.push( l ) ;
66+
}
6467

65-
else if ( m_ctx.top().Is<Val::Array>() )
68+
else if ( m_ctx.top().val.Is<Val::Array>() )
6669
{
67-
Val::Array& ar = m_ctx.top().As<Val::Array>() ;
70+
Val::Array& ar = m_ctx.top().val.As<Val::Array>() ;
6871
ar.push_back( t ) ;
6972
}
70-
else if ( m_ctx.top().Is<Val::Object>() )
73+
else if ( m_ctx.top().val.Is<Val::Object>() )
7174
{
72-
if ( m_key.get() == 0 )
75+
if ( !m_ctx.top().key.Is<std::string>() )
7376
BOOST_THROW_EXCEPTION( Error() << NoKey_(t) ) ;
7477

7578
else
7679
{
77-
Val::Object& obj = m_ctx.top().As<Val::Object>() ;
78-
obj.insert( std::make_pair( m_key->As<std::string>(), t ) ) ;
79-
m_key.reset() ;
80+
Val::Object& obj = m_ctx.top().val.As<Val::Object>() ;
81+
obj.insert( std::make_pair( m_ctx.top().key.Str(), t ) ) ;
82+
m_ctx.top().key = Val::Null() ;
8083
}
8184
}
8285
else
83-
BOOST_THROW_EXCEPTION( Error() << Unexpected_(m_ctx.top()) ) ;
86+
BOOST_THROW_EXCEPTION( Error() << Unexpected_(m_ctx.top().val) ) ;
8487
}
8588

8689
void ValBuilder::VisitKey( const std::string& t )
8790
{
88-
m_key.reset( new Val(t) ) ;
91+
m_ctx.top().key = t ;
8992
}
9093

9194
void ValBuilder::StartArray()
9295
{
93-
m_ctx.push( Val( Val::Array() ) ) ;
96+
Level l = { Val::Null(), Val(Val::Array()) } ;
97+
m_ctx.push(l) ;
9498
}
9599

96100
void ValBuilder::EndArray()
@@ -100,11 +104,13 @@ void ValBuilder::EndArray()
100104

101105
void ValBuilder::End( Val::TypeEnum type )
102106
{
103-
if ( m_ctx.top().Type() == type )
107+
if ( m_ctx.top().val.Type() == type )
104108
{
109+
assert( m_ctx.top().key.Is<void>() ) ;
110+
105111
// get top Val from stack
106112
Val current ;
107-
current.Swap( m_ctx.top() ) ;
113+
current.Swap( m_ctx.top().val ) ;
108114
m_ctx.pop() ;
109115

110116
Build(current) ;
@@ -113,7 +119,8 @@ void ValBuilder::End( Val::TypeEnum type )
113119

114120
void ValBuilder::StartObject()
115121
{
116-
m_ctx.push( Val( Val::Object() ) ) ;
122+
Level l = { Val::Null(), Val( Val::Object() ) } ;
123+
m_ctx.push(l) ;
117124
}
118125

119126
void ValBuilder::EndObject()
@@ -124,7 +131,7 @@ void ValBuilder::EndObject()
124131
Val ValBuilder::Result() const
125132
{
126133
assert( m_ctx.size() == 1U ) ;
127-
return m_ctx.top() ;
134+
return m_ctx.top().val ;
128135
}
129136

130137
} // end of namespace

libgrive/src/json/ValBuilder.hh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,13 @@ private :
6161
void End( Val::TypeEnum type ) ;
6262

6363
private :
64-
std::stack<Val> m_ctx ;
65-
std::auto_ptr<Val> m_key ;
64+
struct Level
65+
{
66+
Val key ;
67+
Val val ;
68+
} ;
69+
70+
std::stack<Level> m_ctx ;
6671
} ;
6772

6873
} // end of namespace

0 commit comments

Comments
 (0)