aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/protobuf/json/json_value_output.h
blob: 3fc6ff2ab0ffc43f90520f07680d6291de87ed9f (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
#pragma once

#include "json_output.h"

#include <library/cpp/json/writer/json_value.h>

#include <util/generic/stack.h>

namespace NProtobufJson {
    class TJsonValueOutput: public IJsonOutput {
    public:
        TJsonValueOutput(NJson::TJsonValue& value)
            : Root(value)
        {
            Context.emplace(TContext::JSON_AFTER_KEY, Root);
        }

        void DoWrite(const TStringBuf& s) override;
        void DoWrite(const TString& s) override;
        void DoWrite(int i) override;
        void DoWrite(unsigned int i) override;
        void DoWrite(long long i) override;
        void DoWrite(unsigned long long i) override;
        void DoWrite(float f) override;
        void DoWrite(double f) override;
        void DoWrite(bool b) override;
        void DoWriteNull() override;

        void DoBeginList() override;
        void DoEndList() override;

        void DoBeginObject() override;
        void DoWriteKey(const TStringBuf& key) override;
        void DoEndObject() override;

        void DoWriteRawJson(const TStringBuf& str) override;

    private:
        template <typename T>
        void WriteImpl(const T& t);

        struct TContext {
            enum EType {
                JSON_MAP,
                JSON_ARRAY,
                JSON_AFTER_KEY,
            };

            TContext(EType type, NJson::TJsonValue& value)
                : Type(type)
                , Value(value)
            {
            }

            EType Type;
            NJson::TJsonValue& Value;
        };

        NJson::TJsonValue& Root;
        TStack<TContext, TVector<TContext>> Context;
    };

}