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
0015
0016 class JSON_API Writer {
0017 public:
0018 virtual ~Writer();
0019
0020 virtual std::string write(const Value &root) = 0;
0021 };
0022
0023
0024
0025
0026
0027
0028
0029 class JSON_API FastWriter : public Writer {
0030 public:
0031 FastWriter();
0032 ~FastWriter() override {}
0033
0034 void enableYAMLCompatibility();
0035
0036 public:
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
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064 class JSON_API StyledWriter : public Writer {
0065 public:
0066 StyledWriter();
0067 ~StyledWriter() override {}
0068
0069 public:
0070
0071
0072
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
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120 class JSON_API StyledStreamWriter {
0121 public:
0122 StyledStreamWriter(std::string indentation = "\t");
0123 ~StyledStreamWriter() {}
0124
0125 public:
0126
0127
0128
0129
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
0164
0165 std::ostream &operator<<(std::ostream &, const Value &root);
0166
0167 }
0168 }
0169
0170 #endif