Skip to content

Commit

Permalink
Merge ae0b7e9 into 3ab7dd7
Browse files Browse the repository at this point in the history
  • Loading branch information
swalrus1 authored Oct 15, 2024
2 parents 3ab7dd7 + ae0b7e9 commit 9a25bc2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 4 deletions.
77 changes: 73 additions & 4 deletions ydb/library/binary_json/write.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#include "write.h"

#include <contrib/libs/simdjson/include/simdjson/dom/array-inl.h>
#include <contrib/libs/simdjson/include/simdjson/dom/document-inl.h>
#include <contrib/libs/simdjson/include/simdjson/dom/element-inl.h>
#include <contrib/libs/simdjson/include/simdjson/dom/parser-inl.h>
#include <contrib/libs/simdjson/include/simdjson/dom/object-inl.h>
#include <library/cpp/json/json_reader.h>

#include <util/generic/vector.h>
Expand Down Expand Up @@ -551,17 +556,81 @@ void DomToJsonIndex(const NUdf::TUnboxedValue& value, TBinaryJsonCallbacks& call
}
}

void SimdJsonToJsonIndex(const simdjson::dom::element& value, TBinaryJsonCallbacks& callbacks) {
switch (value.type()) {
case simdjson::dom::element_type::STRING: {
std::string_view v;
Y_ABORT_UNLESS(value.get(v));
callbacks.OnString(v);
break;
}
case simdjson::dom::element_type::BOOL: {
bool v;
Y_ABORT_UNLESS(value.get(v));
callbacks.OnBoolean(v);
break;
}
case simdjson::dom::element_type::INT64: {
i64 v;
Y_ABORT_UNLESS(value.get(v));
callbacks.OnInteger(v);
break;
}
case simdjson::dom::element_type::UINT64: {
ui64 v;
Y_ABORT_UNLESS(value.get(v));
callbacks.OnUInteger(v);
break;
}
case simdjson::dom::element_type::DOUBLE: {
double v;
Y_ABORT_UNLESS(value.get(v));
callbacks.OnUInteger(v);
break;
}
case simdjson::dom::element_type::NULL_VALUE:
callbacks.OnNull();
break;
case simdjson::dom::element_type::ARRAY: {
callbacks.OnOpenArray();

simdjson::dom::array v;
Y_ABORT_UNLESS(value.get(v));
for (const auto& item : v) {
SimdJsonToJsonIndex(item, callbacks);
}

callbacks.OnCloseArray();
break;
}
case simdjson::dom::element_type::OBJECT: {
callbacks.OnOpenMap();

simdjson::dom::object v;
Y_ABORT_UNLESS(value.get(v));
for (const auto& item : v) {
callbacks.OnMapKey(item.key);
SimdJsonToJsonIndex(item.value, callbacks);
}

callbacks.OnCloseMap();
break;
}
}
}

}

TMaybe<TBinaryJson> SerializeToBinaryJsonImpl(const TStringBuf json) {
TMemoryInput input(json.data(), json.size());
TBinaryJsonCallbacks callbacks(/* throwException */ false);
if (!ReadJson(&input, &callbacks)) {
simdjson::dom::parser parser;
auto doc = parser.parse(json);
if (doc.error() != simdjson::SUCCESS) {
return Nothing();
}
TBinaryJsonCallbacks callbacks(/* throwException */ false);
SimdJsonToJsonIndex(doc.value(), callbacks);
TBinaryJsonSerializer serializer(std::move(callbacks).GetResult());
return std::move(serializer).Serialize();

}

TMaybe<TBinaryJson> SerializeToBinaryJson(const TStringBuf json) {
Expand Down
1 change: 1 addition & 0 deletions ydb/library/binary_json/ya.make
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ YQL_ABI_VERSION(
PEERDIR(
library/cpp/json
ydb/library/yql/minikql/dom
contrib/libs/simdjson
)

SRCS(
Expand Down

0 comments on commit 9a25bc2

Please sign in to comment.