aboutsummaryrefslogtreecommitdiffstats
path: root/yql/essentials/udfs/common/json2/parse.h
blob: 0020c164c2bd0852d8176bc03bb1c8908840afd9 (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
#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 <library/cpp/json/json_reader.h>

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

    class TParse: public TBoxedValue {
    public:
        TParse(TSourcePosition pos)
            : Pos_(pos)
        {
        }

        static const TStringRef& Name() {
            static auto name = TStringRef::Of("Parse");
            return name;
        }

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

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

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

    private:
        TUnboxedValue Run(
            const IValueBuilder* valueBuilder,
            const TUnboxedValuePod* args) const final {
            Y_UNUSED(valueBuilder);
            try {
                const auto json = args[0].AsStringRef();
                return TryParseJsonDom(json, valueBuilder);
            } catch (const std::exception& e) {
                UdfTerminate((TStringBuilder() << Pos_ << " " << e.what()).data());
            }
        }

        TSourcePosition Pos_;
    };
}