aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/common/json2/serialize.h
blob: a7077cb6e6d951a4961a576941f17d84119da615 (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
#pragma once

#include "resource.h"

#include <yql/essentials/public/udf/udf_value.h>
#include <yql/essentials/public/udf/udf_helpers.h>
#include <yql/essentials/minikql/dom/json.h>

#include <yql/essentials/types/binary_json/write.h>

namespace NJson2Udf {
    using namespace NKikimr;
    using namespace NUdf;
    using namespace NYql;
    using namespace NDom;
    using namespace NBinaryJson;

    template <EDataSlot ResultType>
    class TSerialize : public TBoxedValue {
    public:
        TSerialize(TSourcePosition pos)
            : Pos_(pos)
        {
        }

        static const TStringRef& Name();

        static bool DeclareSignature(
            const TStringRef& name,
            TType* userType,
            IFunctionTypeInfoBuilder& builder,
            bool typesOnly) {
            Y_UNUSED(userType);
            if (name != Name()) {
                return false;
            }

            TType* resultType = nullptr;
            if constexpr (ResultType == EDataSlot::Json) {
                resultType = builder.SimpleType<TJson>();
            } else {
                resultType = builder.SimpleType<TJsonDocument>();
            }

            builder.Args()
                ->Add<TAutoMap<TJsonNodeResource>>()
                .Done()
                .Returns(resultType);

            if (!typesOnly) {
                builder.Implementation(new TSerialize(builder.GetSourcePosition()));
            }
            return true;
        }

    private:
        TUnboxedValue Run(
            const IValueBuilder* valueBuilder,
            const TUnboxedValuePod* args) const final {
            try {
                const TUnboxedValue& jsonDom = args[0];

                if constexpr (ResultType == EDataSlot::Json) {
                    return valueBuilder->NewString(SerializeJsonDom(jsonDom));
                } else {
                    const auto binaryJson = SerializeToBinaryJson(jsonDom);
                    return valueBuilder->NewString(TStringBuf(binaryJson.Data(), binaryJson.Size()));
                }
            } catch (const std::exception& e) {
                UdfTerminate((TStringBuilder() << Pos_ << " " << e.what()).data());
            }
        }

        TSourcePosition Pos_;
    };

    template <>
    const TStringRef& TSerialize<EDataSlot::Json>::Name() {
        static auto name = TStringRef::Of("Serialize");
        return name;
    }

    template <>
    const TStringRef& TSerialize<EDataSlot::JsonDocument>::Name() {
        static auto name = TStringRef::Of("SerializeToJsonDocument");
        return name;
    }
}