/*************************************************************************** * Copyright (C) 2005 by Jeff Ferr * * root@sat * * * * 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; either version 2 of the License, or * * (at your option) any later version. * * * * 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., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "jjson.h" #include #include #include #include #include #include #include using namespace jcommon; static void postPrint( const JSONNode * node ) { if( node->isLastChild() ) { const JSONNode * parent = node->getParent(); if( JSONNode::ePair == parent->getType() ) { parent = parent->getParent(); } if( JSONNode::eObject == parent->getType() ) { std::cout << "}"; } else { assert( JSONNode::eArray == parent->getType() ); std::cout << "]"; } postPrint( parent ); } else { if( NULL != node->getParent() ) { std::cout << ","; } } } int main( int argc, char * argv[] ) { char * filename = NULL; if( argc < 2 ) { std::cout << "usage: " << argv[0] << " " << std::endl; exit( -1 ); } else { filename = argv[1]; } FILE * fp = fopen ( filename, "r" ); if( NULL == fp ) { std::cout << "cannot not open " << filename << std::endl; exit( -1 ); } struct stat aStat; char * source = NULL; stat( filename, &aStat ); source = ( char * ) malloc ( aStat.st_size + 1 ); fread ( source, aStat.st_size, sizeof ( char ), fp ); fclose ( fp ); source[ aStat.st_size ] = '\0'; JSONDomParser parser; parser.append( source, strlen( source ) ); if( NULL != parser.getError() ) { std::cout << "\n\nerror: " << parser.getError() << std::endl; } free( source ); std::cout << "Test DomBuffer" << std::endl; JSONDomBuffer buffer( parser.getValue(), 1 ); std::cout << buffer.getBuffer() << std::endl << std::endl; std::cout << "Test Iterator" << std::endl; JSONIterator iterator( parser.getValue() ); for( ; ; ) { const JSONNode * node = iterator.getNext(); if( NULL == node ) break; switch( node->getType() ) { case JSONNode::eObject: std::cout << "{"; break; case JSONNode::eArray: std::cout << "["; break; case JSONNode::ePair: { JSONStringBuffer buffer; JSONCodec::encode( ((JSONPairNode*)node)->getName(), &buffer ); std::cout << "\"" << buffer.getBuffer() << "\" : "; break; } case JSONNode::eString: { JSONStringBuffer buffer; JSONCodec::encode( ((JSONStringNode*)node)->getValue(), &buffer ); std::cout << "\"" << buffer.getBuffer() << "\""; postPrint( node ); break; } case JSONNode::eNull: std::cout << "null"; postPrint( node ); break; case JSONNode::eDouble: std::cout << ((JSONDoubleNode*)node)->getValue(); postPrint( node ); break; case JSONNode::eInt: std::cout << ((JSONIntNode*)node)->getValue(); postPrint( node ); break; case JSONNode::eBoolean: std::cout << ((((JSONBooleanNode*)node)->getValue() == true)?"true":"false"); postPrint( node ); break; case JSONNode::eComment: std::cout << "//" << ((JSONCommentNode*)node)->getValue() << std::endl; break; default: std::cout << "unknown" << std::endl; break; } } std::cout << std::endl; return 0; }