grive2/libgrive/src/json/ValBuilder.cc

138 lines
2.5 KiB
C++
Raw Normal View History

2013-05-01 18:47:37 +04:00
/*
grive: an GPL program to sync a local directory with Google Drive
Copyright (C) 2013 Wan Wai Ho
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
MA 02110-1301, USA.
*/
#include "ValBuilder.hh"
#include <cassert>
namespace gr {
ValBuilder::ValBuilder( )
{
}
ValBuilder::~ValBuilder()
{
}
void ValBuilder::Visit( long long t )
2013-05-01 18:47:37 +04:00
{
Build(Val(t)) ;
}
void ValBuilder::Visit( double t )
2013-05-01 18:47:37 +04:00
{
Build(Val(t)) ;
}
void ValBuilder::Visit( const std::string& t )
2013-05-01 18:47:37 +04:00
{
Build(Val(t)) ;
}
void ValBuilder::Visit( bool t )
2013-05-01 18:47:37 +04:00
{
Build(Val(t)) ;
}
void ValBuilder::VisitNull()
2013-05-01 18:47:37 +04:00
{
Build(Val()) ;
}
void ValBuilder::Build( const Val& t )
{
if ( m_ctx.empty() )
2013-05-02 20:40:04 +04:00
{
Level l = { Val::Null(), t } ;
m_ctx.push( l ) ;
}
2013-05-01 18:47:37 +04:00
2013-05-02 20:40:04 +04:00
else if ( m_ctx.top().val.Is<Val::Array>() )
2013-05-01 18:47:37 +04:00
{
2013-05-02 20:40:04 +04:00
Val::Array& ar = m_ctx.top().val.As<Val::Array>() ;
2013-05-01 18:47:37 +04:00
ar.push_back( t ) ;
}
2013-05-02 20:40:04 +04:00
else if ( m_ctx.top().val.Is<Val::Object>() )
2013-05-01 18:47:37 +04:00
{
2013-05-02 20:40:04 +04:00
if ( !m_ctx.top().key.Is<std::string>() )
2013-05-01 18:47:37 +04:00
BOOST_THROW_EXCEPTION( Error() << NoKey_(t) ) ;
else
{
2013-05-02 20:40:04 +04:00
Val::Object& obj = m_ctx.top().val.As<Val::Object>() ;
obj.insert( std::make_pair( m_ctx.top().key.Str(), t ) ) ;
m_ctx.top().key = Val::Null() ;
2013-05-01 18:47:37 +04:00
}
}
else
2013-05-02 20:40:04 +04:00
BOOST_THROW_EXCEPTION( Error() << Unexpected_(m_ctx.top().val) ) ;
2013-05-01 18:47:37 +04:00
}
void ValBuilder::VisitKey( const std::string& t )
2013-05-01 18:47:37 +04:00
{
2013-05-02 20:40:04 +04:00
m_ctx.top().key = t ;
2013-05-01 18:47:37 +04:00
}
void ValBuilder::StartArray()
{
2013-05-02 20:40:04 +04:00
Level l = { Val::Null(), Val(Val::Array()) } ;
m_ctx.push(l) ;
2013-05-01 18:47:37 +04:00
}
void ValBuilder::EndArray()
{
End( Val::array_type ) ;
}
void ValBuilder::End( Val::TypeEnum type )
{
2013-05-02 20:40:04 +04:00
if ( m_ctx.top().val.Type() == type )
2013-05-01 18:47:37 +04:00
{
2013-05-02 20:40:04 +04:00
assert( m_ctx.top().key.Is<void>() ) ;
2013-05-01 18:47:37 +04:00
// get top Val from stack
Val current ;
2013-05-02 20:40:04 +04:00
current.Swap( m_ctx.top().val ) ;
2013-05-01 18:47:37 +04:00
m_ctx.pop() ;
Build(current) ;
}
}
void ValBuilder::StartObject()
{
2013-05-02 20:40:04 +04:00
Level l = { Val::Null(), Val( Val::Object() ) } ;
m_ctx.push(l) ;
2013-05-01 18:47:37 +04:00
}
void ValBuilder::EndObject()
{
End( Val::object_type ) ;
}
Val ValBuilder::Result() const
{
assert( m_ctx.size() == 1U ) ;
2013-05-02 20:40:04 +04:00
return m_ctx.top().val ;
2013-05-01 18:47:37 +04:00
}
} // end of namespace