aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/json/json_writer.cpp
blob: 3d058bae360e1522cdc5bef2f70e611c6cd4b1e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "json_writer.h"

#include <util/charset/utf8.h>
#include <util/generic/algorithm.h>
#include <util/string/cast.h>
#include <util/system/yassert.h>

namespace NJson {
    TJsonWriter::TJsonWriter(IOutputStream* out, bool formatOutput, bool sortkeys, bool validateUtf8)
        : Out(out)
        , Buf(NJsonWriter::HEM_UNSAFE)
        , DoubleNDigits(TJsonWriterConfig::DefaultDoubleNDigits)
        , FloatNDigits(TJsonWriterConfig::DefaultFloatNDigits)
        , FloatToStringMode(TJsonWriterConfig::DefaultFloatToStringMode)
        , SortKeys(sortkeys)
        , ValidateUtf8(validateUtf8)
        , DontEscapeStrings(false)
        , DontFlushInDestructor(false)
    {
        Buf.SetIndentSpaces(formatOutput ? 2 : 0);
    }

    TJsonWriter::TJsonWriter(IOutputStream* out, const TJsonWriterConfig& config, bool DFID)
        : Out(config.Unbuffered ? nullptr : out)
        , Buf(NJsonWriter::HEM_UNSAFE, config.Unbuffered ? out : nullptr)
        , DoubleNDigits(config.DoubleNDigits)
        , FloatNDigits(config.FloatNDigits)
        , FloatToStringMode(config.FloatToStringMode)
        , SortKeys(config.SortKeys)
        , ValidateUtf8(config.ValidateUtf8)
        , DontEscapeStrings(config.DontEscapeStrings)
        , DontFlushInDestructor(DFID)
    {
        Buf.SetIndentSpaces(config.FormatOutput ? 2 : 0);
        Buf.SetWriteNanAsString(config.WriteNanAsString);
    }

    TJsonWriter::~TJsonWriter() {
        // if we write to socket it's possible to get exception here
        // don't use exceptions in destructors
        if (!DontFlushInDestructor) {
            try {
                Flush();
            } catch (...) {
            }
        }
    }

    void TJsonWriter::Flush() {
        if (Out) {
            Buf.FlushTo(Out);
        }
    }

    void TJsonWriter::OpenMap() {
        Buf.BeginObject();
    }

    void TJsonWriter::CloseMap() {
        Buf.EndObject();
    }

    void TJsonWriter::OpenArray() {
        Buf.BeginList();
    }

    void TJsonWriter::CloseArray() {
        Buf.EndList();
    }

    void TJsonWriter::Write(const TStringBuf& value) {
        if (ValidateUtf8 && !IsUtf(value))
            throw yexception() << "JSON writer: invalid UTF-8";
        if (Buf.KeyExpected()) {
            Buf.WriteKey(value);
        } else {
            if (DontEscapeStrings) {
                Buf.UnsafeWriteValue(TString("\"") + value + '"');
            } else {
                Buf.WriteString(value);
            }
        }
    }

    void TJsonWriter::WriteNull() {
        Buf.WriteNull();
    }

    void TJsonWriter::Write(float value) {
        Buf.WriteFloat(value, FloatToStringMode, FloatNDigits);
    }

    void TJsonWriter::Write(double value) {
        Buf.WriteDouble(value, FloatToStringMode, DoubleNDigits);
    }

    void TJsonWriter::Write(long long value) {
        Buf.WriteLongLong(value);
    }

    void TJsonWriter::Write(unsigned long long value) {
        Buf.WriteULongLong(value);
    }

    void TJsonWriter::Write(bool value) {
        Buf.WriteBool(value);
    }

    namespace {
        struct TLessStrPtr {
            bool operator()(const TString* a, const TString* b) const {
                return *a < *b;
            }
        };
    }

    void TJsonWriter::Write(const TJsonValue* v) {
        Buf.WriteJsonValue(v, SortKeys, FloatToStringMode, DoubleNDigits);
    }

    void TJsonWriter::Write(const TJsonValue& v) {
        Buf.WriteJsonValue(&v, SortKeys, FloatToStringMode, DoubleNDigits);
    }

    TString WriteJson(const TJsonValue* value, bool formatOutput, bool sortkeys, bool validateUtf8) {
        TStringStream ss;
        WriteJson(&ss, value, formatOutput, sortkeys, validateUtf8);
        return ss.Str();
    }

    TString WriteJson(const TJsonValue& value, bool formatOutput, bool sortkeys, bool validateUtf8) {
        TStringStream ss;
        WriteJson(&ss, &value, formatOutput, sortkeys, validateUtf8);
        return ss.Str();
    }

    void WriteJson(IOutputStream* out, const TJsonValue* val, bool formatOutput, bool sortkeys, bool validateUtf8) {
        TJsonWriter w(out, formatOutput, sortkeys, validateUtf8);
        w.Write(val);
        w.Flush();
    }

    void WriteJson(IOutputStream* out, const TJsonValue* val, const TJsonWriterConfig& config) {
        TJsonWriter w(out, config, true);
        w.Write(val);
        w.Flush();
    }

}