aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/scheme/scimpl_json_write.cpp
blob: aadd7e6cd5fb2a8582285c2219d08df842449ef0 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include "scimpl.h"
#include "scimpl_private.h"

#include <library/cpp/json/json_prettifier.h>
#include <library/cpp/string_utils/relaxed_escaper/relaxed_escaper.h>

#include <util/charset/utf8.h>
#include <util/generic/algorithm.h>
#include <util/generic/ymath.h>
#include <util/system/tls.h>

namespace NSc {
    bool TJsonOpts::StringPolicySafe(TStringBuf& s) {
        return IsUtf(s);
    }

    bool TJsonOpts::NumberPolicySafe(double& d) {
        return IsFinite(d);
    }

    static inline void WriteString(IOutputStream& out, TStringBuf s) {
        NEscJ::EscapeJ<true, true>(s, out);
    }

    static inline const NSc::TValue& GetValue(size_t, TStringBuf key, const TDict& dict) {
        return dict.find(key)->second;
    }

    static inline const NSc::TValue& GetValue(TDict::const_iterator it, TStringBuf, const TDict&) {
        return it->second;
    }

    static inline TStringBuf GetKey(size_t it, const NImpl::TKeySortContext::TGuard& keys) {
        return keys.GetVector()[it];
    }

    static inline TStringBuf GetKey(TDict::const_iterator it, const TDict&) {
        return it->first;
    }

    template <typename TDictKeys>
    static inline void WriteDict(IOutputStream& out, const TDictKeys& keys, const TDict& dict,
                                 const TJsonOpts& jopts, NImpl::TKeySortContext& sortCtx, NImpl::TSelfLoopContext& loopCtx) {
        using const_iterator = typename TDictKeys::const_iterator;
        const_iterator begin = keys.begin();
        const_iterator end = keys.end();
        for (const_iterator it = begin; it != end; ++it) {
            TStringBuf key = GetKey(it, keys);

            if (jopts.StringPolicy && !jopts.StringPolicy(key)) {
                ++begin;
                continue;
            }

            if (it != begin) {
                out << ',';
            }

            NEscJ::EscapeJ<true, true>(key, out);
            out << ':';

            GetValue(it, key, dict).DoWriteJsonImpl(out, jopts, sortCtx, loopCtx);
        }
    }

    void TValue::DoWriteJsonImpl(IOutputStream& out, const TJsonOpts& jopts,
                                 NImpl::TKeySortContext& sortCtx, NImpl::TSelfLoopContext& loopCtx) const {
        const TScCore& core = Core();

        NImpl::TSelfLoopContext::TGuard loopCheck(loopCtx, core);

        if (!loopCheck.Ok) {
            out << TStringBuf("null"); // a loop encountered (and asserted), skip the back reference
            return;
        }

        switch (core.ValueType) {
            default: {
                Y_ASSERT(false);
                [[fallthrough]]; /* no break */
            }
            case EType::Null: {
                out << TStringBuf("null");
                break;
            }
            case EType::Bool: {
                out << (core.IntNumber ? TStringBuf("true") : TStringBuf("false"));
                break;
            }
            case EType::IntNumber: {
                out << core.IntNumber;
                break;
            }
            case EType::FloatNumber: {
                double d = core.FloatNumber;
                if (!jopts.NumberPolicy || jopts.NumberPolicy(d)) {
                    out << d;
                } else {
                    out << TStringBuf("null");
                }
                break;
            }
            case EType::String: {
                TStringBuf s = core.String;
                if (!jopts.StringPolicy || jopts.StringPolicy(s)) {
                    WriteString(out, s);
                } else {
                    out << TStringBuf("null");
                }
                break;
            }
            case EType::Array: {
                out << '[';
                const TArray& a = core.GetArray();
                for (TArray::const_iterator it = a.begin(); it != a.end(); ++it) {
                    if (it != a.begin()) {
                        out << ',';
                    }

                    it->DoWriteJsonImpl(out, jopts, sortCtx, loopCtx);
                }
                out << ']';
                break;
            }
            case EType::Dict: {
                out << '{';

                const TDict& dict = core.GetDict();

                if (jopts.SortKeys) {
                    NImpl::TKeySortContext::TGuard keys(sortCtx, dict);
                    WriteDict(out, keys, dict, jopts, sortCtx, loopCtx);
                } else {
                    WriteDict(out, dict, dict, jopts, sortCtx, loopCtx);
                }

                out << '}';
                break;
            }
        }
    }

    const TValue& TValue::ToJson(IOutputStream& out, const TJsonOpts& jopts) const {
        using namespace NImpl;

        if (jopts.FormatJson) {
            TStringStream str;
            DoWriteJsonImpl(str, jopts, GetTlsInstance<TKeySortContext>(), GetTlsInstance<TSelfLoopContext>());
            NJson::PrettifyJson(str.Str(), out);
        } else {
            DoWriteJsonImpl(out, jopts, GetTlsInstance<TKeySortContext>(), GetTlsInstance<TSelfLoopContext>());
        }

        return *this;
    }

    TString TValue::ToJson(const TJsonOpts& jopts) const {
        TString s;
        {
            TStringOutput out(s);
            ToJson(out, jopts);
        }
        return s;
    }

    TJsonOpts TValue::MakeOptsSafeForSerializer(TJsonOpts opts) {
        opts.SortKeys = true;
        opts.StringPolicy = TJsonOpts::StringPolicySafe;
        opts.NumberPolicy = TJsonOpts::NumberPolicySafe;
        return opts;
    }

    TJsonOpts TValue::MakeOptsPrettyForSerializer(TJsonOpts opts) {
        opts.FormatJson = true;
        return MakeOptsSafeForSerializer(opts);
    }

    TString TValue::ToJsonSafe(const TJsonOpts& jopts) const {
        return ToJson(MakeOptsSafeForSerializer(jopts));
    }

    const TValue& TValue::ToJsonSafe(IOutputStream& out, const TJsonOpts& jopts) const {
        return ToJson(out, MakeOptsSafeForSerializer(jopts));
    }

    TString TValue::ToJsonPretty(const TJsonOpts& jopts) const {
        return ToJson(MakeOptsPrettyForSerializer(jopts));
    }

    const TValue& TValue::ToJsonPretty(IOutputStream& out, const TJsonOpts& jopts) const {
        return ToJson(out, MakeOptsPrettyForSerializer(jopts));
    }
}