Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:26:26

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