Drizzled Public API Documentation

writer.h
00001 /*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00002  * 
00003  *  JSON Library, originally from http://jsoncpp.sourceforge.net/
00004  *
00005  *  Copyright (C) 2011 Stewart Smith
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions are
00010  *  met:
00011  *
00012  *      * Redistributions of source code must retain the above copyright
00013  *  notice, this list of conditions and the following disclaimer.
00014  *
00015  *      * Redistributions in binary form must reproduce the above
00016  *  copyright notice, this list of conditions and the following disclaimer
00017  *  in the documentation and/or other materials provided with the
00018  *  distribution.
00019  *
00020  *      * The names of its contributors may not be used to endorse or
00021  *  promote products derived from this software without specific prior
00022  *  written permission.
00023  *
00024  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00027  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00028  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00029  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00030  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00031  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00032  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00033  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  */
00037 
00038 #pragma once
00039 
00040 # include "value.h"
00041 # include <vector>
00042 # include <string>
00043 # include <iosfwd>
00044 
00045 namespace Json {
00046 
00047    class Value;
00048 
00051    class JSON_API Writer
00052    {
00053    public:
00054       virtual ~Writer();
00055 
00056       virtual std::string write( const Value &root ) = 0;
00057    };
00058 
00065    class JSON_API FastWriter : public Writer
00066    {
00067    public:
00068       FastWriter();
00069 
00070       void enableYAMLCompatibility();
00071 
00072    public: // overridden from Writer
00073       virtual std::string write( const Value &root );
00074 
00075    private:
00076       void writeValue( const Value &value );
00077 
00078       std::string document_;
00079       bool yamlCompatiblityEnabled_;
00080    };
00081 
00100    class JSON_API StyledWriter: public Writer
00101    {
00102    public:
00103       StyledWriter();
00104 
00105    public: // overridden from Writer
00110       virtual std::string write( const Value &root );
00111 
00112    private:
00113       void writeValue( const Value &value );
00114       void writeArrayValue( const Value &value );
00115       bool isMultineArray( const Value &value );
00116       void pushValue( const std::string &value );
00117       void writeIndent();
00118       void writeWithIndent( const std::string &value );
00119       void indent();
00120       void unindent();
00121       void writeCommentBeforeValue( const Value &root );
00122       void writeCommentAfterValueOnSameLine( const Value &root );
00123       bool hasCommentForValue( const Value &value );
00124       static std::string normalizeEOL( const std::string &text );
00125 
00126       typedef std::vector<std::string> ChildValues;
00127 
00128       ChildValues childValues_;
00129       std::string document_;
00130       std::string indentString_;
00131       int rightMargin_;
00132       int indentSize_;
00133       bool addChildValues_;
00134    };
00135 
00156    class JSON_API StyledStreamWriter
00157    {
00158    public:
00159       StyledStreamWriter( std::string indentation="\t" );
00160       ~StyledStreamWriter(){}
00161 
00162    public:
00168       void write( std::ostream &out, const Value &root );
00169 
00170    private:
00171       void writeValue( const Value &value );
00172       void writeArrayValue( const Value &value );
00173       bool isMultineArray( const Value &value );
00174       void pushValue( const std::string &value );
00175       void writeIndent();
00176       void writeWithIndent( const std::string &value );
00177       void indent();
00178       void unindent();
00179       void writeCommentBeforeValue( const Value &root );
00180       void writeCommentAfterValueOnSameLine( const Value &root );
00181       bool hasCommentForValue( const Value &value );
00182       static std::string normalizeEOL( const std::string &text );
00183 
00184       typedef std::vector<std::string> ChildValues;
00185 
00186       ChildValues childValues_;
00187       std::ostream* document_;
00188       std::string indentString_;
00189       int rightMargin_;
00190       std::string indentation_;
00191       bool addChildValues_;
00192    };
00193 
00194    std::string JSON_API valueToString( Int value );
00195    std::string JSON_API valueToString( UInt value );
00196    std::string JSON_API valueToString( double value );
00197    std::string JSON_API valueToString( bool value );
00198    std::string JSON_API valueToQuotedString( const char *value );
00199 
00202    std::ostream& operator<<( std::ostream&, const Value &root );
00203 
00204 } // namespace Json