- Made FastWriter output more compact.

- fixed bug in runjsontests.py script.
This commit is contained in:
Baptiste Lepilleur 2007-03-17 22:14:59 +00:00
parent 4cd8bae331
commit 2d4dd281f1
4 changed files with 50 additions and 12 deletions

View File

@ -9,21 +9,37 @@ namespace Json {
class Value; class Value;
/** \brief Abstract class for writers.
*/
class JSON_API Writer
{
public:
virtual ~Writer();
virtual std::string write( const Value &root ) = 0;
};
/** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly). /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly).
* *
* The JSON document is written in a single line. It is not intended for 'human' consumption, * The JSON document is written in a single line. It is not intended for 'human' consumption,
* but may be usefull to support feature such as RPC where bandwith is limited. * but may be usefull to support feature such as RPC where bandwith is limited.
* \sa Reader, Value * \sa Reader, Value
*/ */
class JSON_API FastWriter class JSON_API FastWriter : public Writer
{ {
public: public:
std::string write( const Value &root ); FastWriter();
void enableYAMLCompatibility();
public: // overridden from Writer
virtual std::string write( const Value &root );
private: private:
void writeValue( const Value &value ); void writeValue( const Value &value );
std::string document_; std::string document_;
bool yamlCompatiblityEnabled_;
}; };
/** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way. /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
@ -49,11 +65,12 @@ namespace Json {
public: public:
StyledWriter(); StyledWriter();
public: // overridden from Writer
/** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format. /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
* \param root Value to serialize. * \param root Value to serialize.
* \return String containing the JSON document that represent the root value. * \return String containing the JSON document that represent the root value.
*/ */
std::string write( const Value &root ); virtual std::string write( const Value &root );
private: private:
void writeValue( const Value &value ); void writeValue( const Value &value );

View File

@ -115,7 +115,8 @@ rewriteValueTree( const std::string &rewritePath,
const Json::Value &root, const Json::Value &root,
std::string &rewrite ) std::string &rewrite )
{ {
// Json::FastWriter writer; //Json::FastWriter writer;
//writer.enableYAMLCompatibility();
Json::StyledWriter writer; Json::StyledWriter writer;
rewrite = writer.write( root ); rewrite = writer.write( root );
FILE *fout = fopen( rewritePath.c_str(), "wt" ); FILE *fout = fopen( rewritePath.c_str(), "wt" );

View File

@ -67,10 +67,29 @@ std::string valueToQuotedString( const char *value )
return std::string("\"") + value + "\""; return std::string("\"") + value + "\"";
} }
// Class Writer
// //////////////////////////////////////////////////////////////////
Writer::~Writer()
{
}
// Class FastWriter // Class FastWriter
// ////////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////////
FastWriter::FastWriter()
: yamlCompatiblityEnabled_( false )
{
}
void
FastWriter::enableYAMLCompatibility()
{
yamlCompatiblityEnabled_ = true;
}
std::string std::string
FastWriter::write( const Value &root ) FastWriter::write( const Value &root )
{ {
@ -106,33 +125,34 @@ FastWriter::writeValue( const Value &value )
break; break;
case arrayValue: case arrayValue:
{ {
document_ += "[ "; document_ += "[";
int size = value.size(); int size = value.size();
for ( int index =0; index < size; ++index ) for ( int index =0; index < size; ++index )
{ {
if ( index > 0 ) if ( index > 0 )
document_ += ", "; document_ += ",";
writeValue( value[index] ); writeValue( value[index] );
} }
document_ += " ]"; document_ += "]";
} }
break; break;
case objectValue: case objectValue:
{ {
Value::Members members( value.getMemberNames() ); Value::Members members( value.getMemberNames() );
document_ += "{ "; document_ += "{";
for ( Value::Members::iterator it = members.begin(); for ( Value::Members::iterator it = members.begin();
it != members.end(); it != members.end();
++it ) ++it )
{ {
const std::string &name = *it; const std::string &name = *it;
if ( it != members.begin() ) if ( it != members.begin() )
document_ += ", "; document_ += ",";
document_ += valueToQuotedString( name.c_str() ); document_ += valueToQuotedString( name.c_str() );
document_ += " : "; document_ += yamlCompatiblityEnabled_ ? ": "
: ":";
writeValue( value[name] ); writeValue( value[name] );
} }
document_ += " }"; document_ += "}";
} }
break; break;
} }

View File

@ -83,7 +83,7 @@ if __name__ == '__main__':
sys.exit( 1 ) sys.exit( 1 )
jsontest_executable_path = os.path.normpath( os.path.abspath( sys.argv[1] ) ) jsontest_executable_path = os.path.normpath( os.path.abspath( sys.argv[1] ) )
if len(sys.argv) > 1: if len(sys.argv) > 2:
input_path = os.path.normpath( os.path.abspath( sys.argv[2] ) ) input_path = os.path.normpath( os.path.abspath( sys.argv[2] ) )
else: else:
input_path = None input_path = None