Skip to content

Commit 6ba04dc

Browse files
committed
added operator<< to print out json
1 parent 6fa6e69 commit 6ba04dc

File tree

7 files changed

+118
-23
lines changed

7 files changed

+118
-23
lines changed

libgrive/src/json/ValWriter.cc renamed to libgrive/src/json/JsonWriter.cc

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
MA 02110-1301, USA.
1919
*/
2020

21-
#include "ValWriter.hh"
21+
#include "JsonWriter.hh"
2222
#include "util/DataStream.hh"
2323

2424
#include <yajl/yajl_gen.h>
@@ -27,81 +27,81 @@
2727

2828
namespace gr {
2929

30-
struct ValWriter::Impl
30+
struct JsonWriter::Impl
3131
{
3232
yajl_gen gen ;
3333
DataStream *out ;
3434
} ;
3535

36-
ValWriter::ValWriter( DataStream *out ) :
36+
JsonWriter::JsonWriter( DataStream *out ) :
3737
m_impl( new Impl )
3838
{
3939
assert( out != 0 ) ;
4040

4141
m_impl->out = out ;
4242
m_impl->gen = yajl_gen_alloc(0) ;
43-
yajl_gen_config( m_impl->gen, yajl_gen_print_callback, &ValWriter::WriteCallback, this ) ;
43+
yajl_gen_config( m_impl->gen, yajl_gen_print_callback, &JsonWriter::WriteCallback, this ) ;
4444
}
4545

46-
ValWriter::~ValWriter()
46+
JsonWriter::~JsonWriter()
4747
{
4848
yajl_gen_free( m_impl->gen ) ;
4949
}
5050

51-
void ValWriter::Visit( long long t )
51+
void JsonWriter::Visit( long long t )
5252
{
5353
yajl_gen_integer( m_impl->gen, t ) ;
5454
}
5555

56-
void ValWriter::Visit( double t )
56+
void JsonWriter::Visit( double t )
5757
{
5858
yajl_gen_double( m_impl->gen, t ) ;
5959
}
6060

61-
void ValWriter::Visit( const std::string& t )
61+
void JsonWriter::Visit( const std::string& t )
6262
{
6363
yajl_gen_string( m_impl->gen,
6464
reinterpret_cast<const unsigned char*>(t.c_str()), t.size() ) ;
6565
}
6666

67-
void ValWriter::Visit( bool t )
67+
void JsonWriter::Visit( bool t )
6868
{
6969
yajl_gen_bool( m_impl->gen, t ) ;
7070
}
7171

72-
void ValWriter::VisitNull()
72+
void JsonWriter::VisitNull()
7373
{
7474
yajl_gen_null( m_impl->gen ) ;
7575
}
7676

77-
void ValWriter::StartArray()
77+
void JsonWriter::StartArray()
7878
{
7979
yajl_gen_array_open( m_impl->gen ) ;
8080
}
8181

82-
void ValWriter::EndArray()
82+
void JsonWriter::EndArray()
8383
{
8484
yajl_gen_array_close( m_impl->gen ) ;
8585
}
8686

87-
void ValWriter::StartObject()
87+
void JsonWriter::StartObject()
8888
{
8989
yajl_gen_map_open( m_impl->gen ) ;
9090
}
9191

92-
void ValWriter::VisitKey( const std::string& t )
92+
void JsonWriter::VisitKey( const std::string& t )
9393
{
9494
Visit(t) ;
9595
}
9696

97-
void ValWriter::EndObject()
97+
void JsonWriter::EndObject()
9898
{
9999
yajl_gen_map_close( m_impl->gen ) ;
100100
}
101101

102-
void ValWriter::WriteCallback( void *ctx, const char *str, std::size_t size )
102+
void JsonWriter::WriteCallback( void *ctx, const char *str, std::size_t size )
103103
{
104-
ValWriter *pthis = reinterpret_cast<ValWriter*>(ctx) ;
104+
JsonWriter *pthis = reinterpret_cast<JsonWriter*>(ctx) ;
105105
assert( pthis != 0 ) ;
106106
assert( pthis->m_impl->out != 0 ) ;
107107

libgrive/src/json/ValWriter.hh renamed to libgrive/src/json/JsonWriter.hh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ namespace gr {
2727

2828
class DataStream ;
2929

30-
class ValWriter : public ValVisitor
30+
class JsonWriter : public ValVisitor
3131
{
3232
public :
33-
ValWriter( DataStream *out ) ;
34-
~ValWriter() ;
33+
JsonWriter( DataStream *out ) ;
34+
~JsonWriter() ;
3535

3636
void Visit( long long t ) ;
3737
void Visit( double t ) ;

libgrive/src/json/Val.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
*/
2020

2121
#include "Val.hh"
22+
#include "JsonWriter.hh"
2223
#include "ValVisitor.hh"
24+
#include "util/StdStream.hh"
2325

2426
#include <iostream>
2527

@@ -172,6 +174,15 @@ void Val::Visit( ValVisitor *visitor ) const
172174
}
173175
}
174176

177+
std::ostream& operator<<( std::ostream& os, const Val& val )
178+
{
179+
StdStream ss( os.rdbuf() ) ;
180+
JsonWriter wr( &ss ) ;
181+
val.Visit( &wr ) ;
182+
183+
return os ;
184+
}
185+
175186
} // end of namespace
176187

177188
namespace std

libgrive/src/json/Val.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public :
111111
Val FindInArray( const std::string& key, const std::string& value ) const ;
112112
bool FindInArray( const std::string& key, const std::string& value, Val& result ) const ;
113113

114-
// friend std::ostream& operator<<( std::ostream& os, const Val& json ) ;
114+
friend std::ostream& operator<<( std::ostream& os, const Val& val ) ;
115115
void Visit( ValVisitor *visitor ) const ;
116116

117117
private :

libgrive/src/util/StdStream.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 "StdStream.hh"
22+
23+
#include <streambuf>
24+
25+
namespace gr {
26+
27+
StdStream::StdStream( std::streambuf *buf ) :
28+
m_adaptee( buf )
29+
{
30+
}
31+
32+
std::size_t StdStream::Read( char *data, std::size_t size )
33+
{
34+
return m_adaptee == 0 ? 0 : m_adaptee->sgetn( data, size ) ;
35+
}
36+
37+
std::size_t StdStream::Write( const char *data, std::size_t size )
38+
{
39+
return m_adaptee == 0 ? 0 : m_adaptee->sputn( data, size ) ;
40+
}
41+
42+
} // end of namespace

libgrive/src/util/StdStream.hh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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 "DataStream.hh"
24+
25+
#include <iosfwd>
26+
27+
namespace gr {
28+
29+
class StdStream : public DataStream
30+
{
31+
public :
32+
explicit StdStream( std::streambuf *buf ) ;
33+
34+
std::size_t Read( char *data, std::size_t size ) ;
35+
std::size_t Write( const char *data, std::size_t size ) ;
36+
37+
private :
38+
std::streambuf *m_adaptee ;
39+
} ;
40+
41+
} // end of namespace
42+

libgrive/test/btest/JsonValTest.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
#include "json/JsonParser.hh"
2121
#include "json/Val.hh"
2222
#include "json/ValBuilder.hh"
23-
#include "json/ValWriter.hh"
23+
#include "json/JsonWriter.hh"
2424
#include "util/StringStream.hh"
2525

2626
#include <boost/test/unit_test.hpp>
@@ -46,7 +46,7 @@ BOOST_AUTO_TEST_CASE( Test )
4646
BOOST_CHECK_EQUAL( json["key"].As<long long>(), 100 ) ;
4747

4848
StringStream ss ;
49-
ValWriter wr( &ss ) ;
49+
JsonWriter wr( &ss ) ;
5050
json.Visit( &wr ) ;
5151

5252
BOOST_CHECK_EQUAL( ss.Str(), "{\"key\":100}" ) ;

0 commit comments

Comments
 (0)