You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/02/11 21:46:51 UTC

[GitHub] [iceberg] kbendick commented on a change in pull request #4097: [WIP] REST Catalog - Initial Http Client Interface & Basic Implementation

kbendick commented on a change in pull request #4097:
URL: https://github.com/apache/iceberg/pull/4097#discussion_r805026329



##########
File path: core/src/main/java/org/apache/iceberg/rest/RESTSerializers.java
##########
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iceberg.rest;
+
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.module.SimpleModule;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import java.io.IOException;
+import org.apache.iceberg.catalog.Namespace;
+
+public class RESTSerializers {
+
+  private RESTSerializers() {
+  }
+
+  public static void registerAll(ObjectMapper mapper) {
+    SimpleModule module = new SimpleModule();
+    module
+        .addSerializer(Namespace.class, new NamespaceSerializer())
+        .addDeserializer(Namespace.class, new NamespaceDeserializer());
+    mapper.registerModule(module);
+  }
+
+  public static class NamespaceDeserializer extends JsonDeserializer<Namespace> {
+    @Override
+    public Namespace deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
+      JsonNode jsonNode = p.getCodec().readTree(p);
+      return namespaceFromNode(p, jsonNode);
+    }
+  }
+
+  public static class NamespaceSerializer extends JsonSerializer<Namespace> {
+    @Override
+    public void serialize(Namespace namespace, JsonGenerator gen, SerializerProvider serializers)
+        throws IOException {
+      String[] parts = namespace.levels();
+      gen.writeArray(parts, 0, parts.length);
+    }
+  }
+
+  private static Namespace namespaceFromNode(JsonParser parser, JsonNode jsonNode) throws IOException {
+    if (!jsonNode.isArray()) {
+      throw new JsonParseException(parser, "Namespace must be an array: " + jsonNode);
+    }
+    ArrayNode arrayNode = (ArrayNode) jsonNode;
+    String[] levels = new String[arrayNode.size()];
+    for (int i = 0; i < levels.length; i++) {
+      levels[i] = arrayNode.get(i).asText();
+    }
+    return Namespace.of(levels);
+  }
+}

Review comment:
       This is taken from the other PR and can be ignored here,




-- 
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: issues-unsubscribe@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org