Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-05-20 22:39:45

0001 #ifndef JSON_WRITER_H_INCLUDED
0002 #define JSON_WRITER_H_INCLUDED
0003 
0004 #include "value.h"
0005 #include <vector>
0006 #include <string>
0007 #include <iostream>
0008 
0009 namespace jsoncollector {
0010   namespace Json {
0011 
0012     class Value;
0013 
0014     /** \brief Abstract class for writers.
0015     */
0016     class JSON_API Writer {
0017     public:
0018       virtual ~Writer();
0019 
0020       virtual std::string write(const Value &root) = 0;
0021     };
0022 
0023     /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly).
0024     *
0025     * The JSON document is written in a single line. It is not intended for 'human' consumption,
0026     * but may be usefull to support feature such as RPC where bandwith is limited.
0027     * \sa Reader, Value
0028     */
0029     class JSON_API FastWriter : public Writer {
0030     public:
0031       FastWriter();
0032       ~FastWriter() override {}
0033 
0034       void enableYAMLCompatibility();
0035 
0036     public:  // overridden from Writer
0037       std::string write(const Value &root) override;
0038 
0039     private:
0040       void writeValue(const Value &value);
0041 
0042       std::string document_;
0043       bool yamlCompatiblityEnabled_;
0044     };
0045 
0046     /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
0047     *
0048     * The rules for line break and indent are as follow:
0049     * - Object value:
0050     *     - if empty then print {} without indent and line break
0051     *     - if not empty the print '{', line break & indent, print one value per line
0052     *       and then unindent and line break and print '}'.
0053     * - Array value:
0054     *     - if empty then print [] without indent and line break
0055     *     - if the array contains no object value, empty array or some other value types,
0056     *       and all the values fit on one lines, then print the array on a single line.
0057     *     - otherwise, it the values do not fit on one line, or the array contains
0058     *       object or non empty array, then print one value per line.
0059     *
0060     * If the Value have comments then they are outputed according to their #CommentPlacement.
0061     *
0062     * \sa Reader, Value, Value::setComment()
0063     */
0064     class JSON_API StyledWriter : public Writer {
0065     public:
0066       StyledWriter();
0067       ~StyledWriter() override {}
0068 
0069     public:  // overridden from Writer
0070       /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
0071        * \param root Value to serialize.
0072        * \return String containing the JSON document that represents the root value.
0073        */
0074       std::string write(const Value &root) override;
0075 
0076     private:
0077       void writeValue(const Value &value);
0078       void writeArrayValue(const Value &value);
0079       bool isMultineArray(const Value &value);
0080       void pushValue(const std::string &value);
0081       void writeIndent();
0082       void writeWithIndent(const std::string &value);
0083       void indent();
0084       void unindent();
0085       void writeCommentBeforeValue(const Value &root);
0086       void writeCommentAfterValueOnSameLine(const Value &root);
0087       bool hasCommentForValue(const Value &value);
0088       static std::string normalizeEOL(const std::string &text);
0089 
0090       typedef std::vector<std::string> ChildValues;
0091 
0092       ChildValues childValues_;
0093       std::string document_;
0094       std::string indentString_;
0095       int rightMargin_;
0096       int indentSize_;
0097       bool addChildValues_;
0098     };
0099 
0100     /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way,
0101         to a stream rather than to a string.
0102     *
0103     * The rules for line break and indent are as follow:
0104     * - Object value:
0105     *     - if empty then print {} without indent and line break
0106     *     - if not empty the print '{', line break & indent, print one value per line
0107     *       and then unindent and line break and print '}'.
0108     * - Array value:
0109     *     - if empty then print [] without indent and line break
0110     *     - if the array contains no object value, empty array or some other value types,
0111     *       and all the values fit on one lines, then print the array on a single line.
0112     *     - otherwise, it the values do not fit on one line, or the array contains
0113     *       object or non empty array, then print one value per line.
0114     *
0115     * If the Value have comments then they are outputed according to their #CommentPlacement.
0116     *
0117     * \param indentation Each level will be indented by this amount extra.
0118     * \sa Reader, Value, Value::setComment()
0119     */
0120     class JSON_API StyledStreamWriter {
0121     public:
0122       StyledStreamWriter(std::string indentation = "\t");
0123       ~StyledStreamWriter() {}
0124 
0125     public:
0126       /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
0127        * \param out Stream to write to. (Can be ostringstream, e.g.)
0128        * \param root Value to serialize.
0129        * \note There is no point in deriving from Writer, since write() should not return a value.
0130        */
0131       void write(std::ostream &out, const Value &root);
0132 
0133     private:
0134       void writeValue(const Value &value);
0135       void writeArrayValue(const Value &value);
0136       bool isMultineArray(const Value &value);
0137       void pushValue(const std::string &value);
0138       void writeIndent();
0139       void writeWithIndent(const std::string &value);
0140       void indent();
0141       void unindent();
0142       void writeCommentBeforeValue(const Value &root);
0143       void writeCommentAfterValueOnSameLine(const Value &root);
0144       bool hasCommentForValue(const Value &value);
0145       static std::string normalizeEOL(const std::string &text);
0146 
0147       typedef std::vector<std::string> ChildValues;
0148 
0149       ChildValues childValues_;
0150       std::ostream *document_;
0151       std::string indentString_;
0152       int rightMargin_;
0153       std::string indentation_;
0154       bool addChildValues_;
0155     };
0156 
0157     std::string JSON_API valueToString(Int value);
0158     std::string JSON_API valueToString(UInt value);
0159     std::string JSON_API valueToString(double value);
0160     std::string JSON_API valueToString(bool value);
0161     std::string JSON_API valueToQuotedString(const char *value);
0162 
0163     /// \brief Output using the StyledStreamWriter.
0164     /// \see Json::operator>>()
0165     std::ostream &operator<<(std::ostream &, const Value &root);
0166 
0167   }  // namespace Json
0168 }  // namespace jsoncollector
0169 
0170 #endif  // JSON_WRITER_H_INCLUDED