evindj commented on code in PR #553:
URL: https://github.com/apache/iceberg-cpp/pull/553#discussion_r2853241336


##########
src/iceberg/expression/json_serde.cc:
##########
@@ -123,27 +181,252 @@ nlohmann::json ToJson(Expression::Operation op) {
   return json;
 }
 
+nlohmann::json ToJson(const NamedReference& ref) { return ref.name(); }
+
+Result<std::unique_ptr<NamedReference>> NamedReferenceFromJson(
+    const nlohmann::json& json) {
+  if (json.is_object() && json.contains(kType) &&
+      json[kType].get<std::string>() == kTypeReference && 
json.contains(kTerm)) {
+    return NamedReference::Make(json[kTerm].get<std::string>());
+  }
+  if (!json.is_string()) [[unlikely]] {
+    return JsonParseError("Expected string for named reference");
+  }
+  return NamedReference::Make(json.get<std::string>());
+}
+
+nlohmann::json ToJson(const UnboundTransform& transform) {
+  auto& mutable_transform = const_cast<UnboundTransform&>(transform);
+  nlohmann::json json;
+  json[kType] = kTransform;
+  json[kTransform] = transform.transform()->ToString();
+  json[kTerm] = mutable_transform.reference()->name();
+  return json;
+}
+
+Result<std::unique_ptr<UnboundTransform>> UnboundTransformFromJson(
+    const nlohmann::json& json) {
+  if (IsTransformTerm(json)) {
+    ICEBERG_ASSIGN_OR_RAISE(auto transform_str,
+                            GetJsonValue<std::string>(json, kTransform));
+    ICEBERG_ASSIGN_OR_RAISE(auto transform, 
TransformFromString(transform_str));
+    ICEBERG_ASSIGN_OR_RAISE(auto ref, NamedReferenceFromJson(json[kTerm]));
+    return UnboundTransform::Make(std::move(ref), std::move(transform));
+  }
+  return JsonParseError("Invalid unbound transform json: {}", 
SafeDumpJson(json));
+}
+
+nlohmann::json ToJson(const Literal& literal) {
+  if (literal.IsNull()) {
+    return nullptr;
+  }
+
+  const auto type_id = literal.type()->type_id();
+  const auto& value = literal.value();
+
+  switch (type_id) {
+    case TypeId::kBoolean:
+      return std::get<bool>(value);
+    case TypeId::kInt:
+      return std::get<int32_t>(value);
+    case TypeId::kDate:
+      return TransformUtil::HumanDay(std::get<int32_t>(value));
+    case TypeId::kLong:
+      return std::get<int64_t>(value);
+    case TypeId::kTime:
+      return TransformUtil::HumanTime(std::get<int64_t>(value));
+    case TypeId::kTimestamp:
+      return TransformUtil::HumanTimestamp(std::get<int64_t>(value));
+    case TypeId::kTimestampTz:
+      return TransformUtil::HumanTimestampWithZone(std::get<int64_t>(value));
+    case TypeId::kFloat:
+      return std::get<float>(value);
+    case TypeId::kDouble:
+      return std::get<double>(value);
+    case TypeId::kString:
+      return std::get<std::string>(value);
+    case TypeId::kBinary:
+    case TypeId::kFixed: {
+      const auto& bytes = std::get<std::vector<uint8_t>>(value);
+      std::string hex;
+      hex.reserve(bytes.size() * 2);
+      for (uint8_t byte : bytes) {
+        hex += std::format("{:02X}", byte);
+      }
+      return hex;
+    }
+    case TypeId::kDecimal: {
+      return literal.ToString();
+    }
+    case TypeId::kUuid:
+      return std::get<Uuid>(value).ToString();
+    default:
+      nlohmann::json json;
+      return json;
+  }
+}
+
+Result<Literal> LiteralFromJson(const nlohmann::json& json) {

Review Comment:
   I decided not to add it because java always passes null for this parameter.
   
https://github.com/apache/iceberg/blob/d2fbe427ecec298f1600260f40fcab1b7ab2e695/core/src/main/java/org/apache/iceberg/expressions/ExpressionParser.java#L262-L268
   If there is no objection, I think I will keep this as is.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to