Coverage Report

Created: 2017-04-27 19:33

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/polly/lib/External/JSON/include/json/writer.h
Line
Count
Source (jump to first uncovered line)
1
#ifndef JSON_WRITER_H_INCLUDED
2
# define JSON_WRITER_H_INCLUDED
3
4
# include "value.h"
5
# include <vector>
6
# include <string>
7
# include <iostream>
8
9
namespace Json {
10
11
   class Value;
12
13
   /** \brief Abstract class for writers.
14
    */
15
   class JSON_API Writer
16
   {
17
   public:
18
      virtual ~Writer();
19
20
      virtual std::string write( const Value &root ) = 0;
21
   };
22
23
   /** \brief Outputs a Value in <a HREF="http://www.json.org">JSON</a> format without formatting (not human friendly).
24
    *
25
    * The JSON document is written in a single line. It is not intended for 'human' consumption,
26
    * but may be useful to support feature such as RPC where bandwidth is limited.
27
    * \sa Reader, Value
28
    */
29
   class JSON_API FastWriter : public Writer
30
   {
31
   public:
32
      FastWriter();
33
0
      virtual ~FastWriter(){}
34
35
      void enableYAMLCompatibility();
36
37
   public: // overridden from Writer
38
      virtual std::string write( const Value &root );
39
40
   private:
41
      void writeValue( const Value &value );
42
43
      std::string document_;
44
      bool yamlCompatiblityEnabled_;
45
   };
46
47
   /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way.
48
    *
49
    * The rules for line break and indent are as follow:
50
    * - Object value:
51
    *     - if empty then print {} without indent and line break
52
    *     - if not empty the print '{', line break & indent, print one value per line
53
    *       and then unindent and line break and print '}'.
54
    * - Array value:
55
    *     - if empty then print [] without indent and line break
56
    *     - if the array contains no object value, empty array or some other value types,
57
    *       and all the values fit on one lines, then print the array on a single line.
58
    *     - otherwise, it the values do not fit on one line, or the array contains
59
    *       object or non empty array, then print one value per line.
60
    *
61
    * If the Value have comments then they are outputed according to their #CommentPlacement.
62
    *
63
    * \sa Reader, Value, Value::setComment()
64
    */
65
   class JSON_API StyledWriter: public Writer
66
   {
67
   public:
68
      StyledWriter();
69
0
      virtual ~StyledWriter(){}
70
71
   public: // overridden from Writer
72
      /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
73
       * \param root Value to serialize.
74
       * \return String containing the JSON document that represents the root value.
75
       */
76
      virtual std::string write( const Value &root );
77
78
   private:
79
      void writeValue( const Value &value );
80
      void writeArrayValue( const Value &value );
81
      bool isMultineArray( const Value &value );
82
      void pushValue( const std::string &value );
83
      void writeIndent();
84
      void writeWithIndent( const std::string &value );
85
      void indent();
86
      void unindent();
87
      void writeCommentBeforeValue( const Value &root );
88
      void writeCommentAfterValueOnSameLine( const Value &root );
89
      bool hasCommentForValue( const Value &value );
90
      static std::string normalizeEOL( const std::string &text );
91
92
      typedef std::vector<std::string> ChildValues;
93
94
      ChildValues childValues_;
95
      std::string document_;
96
      std::string indentString_;
97
      int rightMargin_;
98
      int indentSize_;
99
      bool addChildValues_;
100
   };
101
102
   /** \brief Writes a Value in <a HREF="http://www.json.org">JSON</a> format in a human friendly way,
103
        to a stream rather than to a string.
104
    *
105
    * The rules for line break and indent are as follow:
106
    * - Object value:
107
    *     - if empty then print {} without indent and line break
108
    *     - if not empty the print '{', line break & indent, print one value per line
109
    *       and then unindent and line break and print '}'.
110
    * - Array value:
111
    *     - if empty then print [] without indent and line break
112
    *     - if the array contains no object value, empty array or some other value types,
113
    *       and all the values fit on one lines, then print the array on a single line.
114
    *     - otherwise, it the values do not fit on one line, or the array contains
115
    *       object or non empty array, then print one value per line.
116
    *
117
    * If the Value have comments then they are outputed according to their #CommentPlacement.
118
    *
119
    * \sa Reader, Value, Value::setComment()
120
    */
121
   class JSON_API StyledStreamWriter
122
   {
123
   public:
124
      StyledStreamWriter( std::string indentation="\t" );
125
0
      ~StyledStreamWriter(){}
126
127
   public:
128
      /** \brief Serialize a Value in <a HREF="http://www.json.org">JSON</a> format.
129
       * \param out Stream to write to. (Can be ostringstream, e.g.)
130
       * \param root Value to serialize.
131
       * \note There is no point in deriving from Writer, since write() should not return a value.
132
       */
133
      void write( std::ostream &out, const Value &root );
134
135
   private:
136
      void writeValue( const Value &value );
137
      void writeArrayValue( const Value &value );
138
      bool isMultineArray( const Value &value );
139
      void pushValue( const std::string &value );
140
      void writeIndent();
141
      void writeWithIndent( const std::string &value );
142
      void indent();
143
      void unindent();
144
      void writeCommentBeforeValue( const Value &root );
145
      void writeCommentAfterValueOnSameLine( const Value &root );
146
      bool hasCommentForValue( const Value &value );
147
      static std::string normalizeEOL( const std::string &text );
148
149
      typedef std::vector<std::string> ChildValues;
150
151
      ChildValues childValues_;
152
      std::ostream* document_;
153
      std::string indentString_;
154
      int rightMargin_;
155
      std::string indentation_;
156
      bool addChildValues_;
157
   };
158
159
   std::string JSON_API valueToString( Int value );
160
   std::string JSON_API valueToString( UInt value );
161
   std::string JSON_API valueToString( double value );
162
   std::string JSON_API valueToString( bool value );
163
   std::string JSON_API valueToQuotedString( const char *value );
164
165
   /// \brief Output using the StyledStreamWriter.
166
   /// \see Json::operator>>()
167
   std::ostream& operator<<( std::ostream&, const Value &root );
168
169
} // namespace Json
170
171
172
173
#endif // JSON_WRITER_H_INCLUDED