diff options
| author | vvvv <[email protected]> | 2025-10-09 12:25:18 +0300 |
|---|---|---|
| committer | vvvv <[email protected]> | 2025-10-09 12:57:17 +0300 |
| commit | cb77d014972b2cdb27d2e6d979fc3a2772b27ad4 (patch) | |
| tree | 7f3bcd8ce71c6bd0f3ccc11e31b9f665475b819e /yql/essentials/minikql/dom/json.cpp | |
| parent | d58a8990d353b051c27e1069141117fdfde64358 (diff) | |
YQL-20086 minikql
commit_hash:e96f7390db5fcbe7e9f64f898141a263ad522daa
Diffstat (limited to 'yql/essentials/minikql/dom/json.cpp')
| -rw-r--r-- | yql/essentials/minikql/dom/json.cpp | 122 |
1 files changed, 78 insertions, 44 deletions
diff --git a/yql/essentials/minikql/dom/json.cpp b/yql/essentials/minikql/dom/json.cpp index 4d4b093f18a..9f3440d6288 100644 --- a/yql/essentials/minikql/dom/json.cpp +++ b/yql/essentials/minikql/dom/json.cpp @@ -24,8 +24,9 @@ namespace { size_t AsciiSize(const TStringBuf& str) { size_t s = 0U; - while (s < str.size() && isascii(str[s])) + while (s < str.size() && isascii(str[s])) { ++s; + } return s; } @@ -63,8 +64,8 @@ TString DecodeUtf(const TStringBuf& str, size_t from) return result; } -template<bool DecodeUtf8> -class TDomCallbacks : public TJsonCallbacks { +template <bool DecodeUtf8> +class TDomCallbacks: public TJsonCallbacks { public: TDomCallbacks(const IValueBuilder* valueBuilder, bool throwException) : TJsonCallbacks(throwException) @@ -171,57 +172,84 @@ private: std::stack<TUnboxedValues, TSmallVec<TUnboxedValues, TStdAllocatorForUdf<TUnboxedValues>>> Result_; }; -class TTestCallbacks : public TJsonCallbacks { +class TTestCallbacks: public TJsonCallbacks { public: TTestCallbacks() : TJsonCallbacks(false) - {} + { + } - bool OnNull() final { return true; } + bool OnNull() final { + return true; + } - bool OnBoolean(bool) final { return true; } + bool OnBoolean(bool) final { + return true; + } - bool OnInteger(long long) final { return true; } + bool OnInteger(long long) final { + return true; + } - bool OnUInteger(unsigned long long) final { return true; } + bool OnUInteger(unsigned long long) final { + return true; + } - bool OnDouble(double value) final { return !std::isinf(value); } + bool OnDouble(double value) final { + return !std::isinf(value); + } - bool OnString(const TStringBuf&) final { return true; } + bool OnString(const TStringBuf&) final { + return true; + } - bool OnOpenMap() final { return true; } + bool OnOpenMap() final { + return true; + } - bool OnMapKey(const TStringBuf&) final { return true; } + bool OnMapKey(const TStringBuf&) final { + return true; + } - bool OnCloseMap() final { return true; } + bool OnCloseMap() final { + return true; + } - bool OnOpenArray() final { return true; } + bool OnOpenArray() final { + return true; + } - bool OnCloseArray() final { return true; } + bool OnCloseArray() final { + return true; + } bool OnEnd() final { - if (HasResult_) + if (HasResult_) { return false; + } return HasResult_ = true; } - private: - bool HasResult_ = false; +private: + bool HasResult_ = false; }; bool IsEntity(const TUnboxedValuePod value) { switch (GetNodeType(value)) { - case ENodeType::Entity: return true; - case ENodeType::Attr: return IsEntity(value.GetVariantItem().Release()); - default: return false; + case ENodeType::Entity: + return true; + case ENodeType::Attr: + return IsEntity(value.GetVariantItem().Release()); + default: + return false; } } -template<bool SkipMapEntity, bool EncodeUtf8> +template <bool SkipMapEntity, bool EncodeUtf8> void WriteValue(const TUnboxedValuePod value, TJsonWriter& writer); -template<bool SkipMapEntity, bool EncodeUtf8> +template <bool SkipMapEntity, bool EncodeUtf8> void WriteArray(const TUnboxedValuePod value, TJsonWriter& writer) { writer.OpenArray(); if (value.IsBoxed()) { @@ -232,37 +260,42 @@ void WriteArray(const TUnboxedValuePod value, TJsonWriter& writer) { } } else { const auto it = value.GetListIterator(); - for (TUnboxedValue v; it.Next(v); WriteValue<SkipMapEntity, EncodeUtf8>(v, writer)) + for (TUnboxedValue v; it.Next(v); WriteValue<SkipMapEntity, EncodeUtf8>(v, writer)) { continue; + } } } writer.CloseArray(); } -template<bool SkipMapEntity, bool EncodeUtf8> +template <bool SkipMapEntity, bool EncodeUtf8> void WriteMap(const TUnboxedValuePod value, TJsonWriter& writer) { writer.OpenMap(); if (value.IsBoxed()) { TUnboxedValue key, payload; for (const auto it = value.GetDictIterator(); it.NextPair(key, payload);) { - if constexpr (SkipMapEntity) - if (IsEntity(payload)) + if constexpr (SkipMapEntity) { + if (IsEntity(payload)) { continue; + } + } const TStringBuf str = key.AsStringRef(); - if constexpr (EncodeUtf8) - if (const auto from = AsciiSize(str); from < str.size()) + if constexpr (EncodeUtf8) { + if (const auto from = AsciiSize(str); from < str.size()) { writer.Write(EncodeUtf(str, from)); - else + } else { writer.Write(str); - else + } + } else { writer.Write(str); + } WriteValue<SkipMapEntity, EncodeUtf8>(payload, writer); } } writer.CloseMap(); } -template<bool SkipMapEntity, bool EncodeUtf8> +template <bool SkipMapEntity, bool EncodeUtf8> void WriteValue(const TUnboxedValuePod value, TJsonWriter& writer) { switch (GetNodeType(value)) { case ENodeType::String: { @@ -283,7 +316,7 @@ void WriteValue(const TUnboxedValuePod value, TJsonWriter& writer) { case ENodeType::Double: return writer.Write(value.Get<double>()); case ENodeType::Entity: - return writer.WriteNull(); + return writer.WriteNull(); case ENodeType::List: return WriteArray<SkipMapEntity, EncodeUtf8>(value, writer); case ENodeType::Dict: @@ -298,7 +331,7 @@ void WriteValue(const TUnboxedValuePod value, TJsonWriter& writer) { } } -} +} // namespace bool IsValidJson(const TStringBuf json) { TMemoryInput input(json.data(), json.size()); @@ -332,18 +365,19 @@ TString SerializeJsonDom(const NUdf::TUnboxedValuePod dom, bool skipMapEntity, b config.FloatToStringMode = EFloatToStringMode::PREC_AUTO; TJsonWriter writer(&output, config); - if (skipMapEntity) - if (encodeUtf8) + if (skipMapEntity) { + if (encodeUtf8) { WriteValue<true, true>(dom, writer); - else + } else { WriteValue<true, false>(dom, writer); - else - if (encodeUtf8) - WriteValue<false, true>(dom, writer); - else - WriteValue<false, false>(dom, writer); + } + } else if (encodeUtf8) { + WriteValue<false, true>(dom, writer); + } else { + WriteValue<false, false>(dom, writer); + } writer.Flush(); return output.Str(); } -} +} // namespace NYql::NDom |
