You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2022/08/22 09:04:11 UTC

[GitHub] [rocketmq-schema-registry] humkum commented on a diff in pull request #41: support JSON serde

humkum commented on code in PR #41:
URL: https://github.com/apache/rocketmq-schema-registry/pull/41#discussion_r951191055


##########
client/src/main/java/org/apache/rocketmq/schema/registry/client/serde/json/JsonSerializer.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * 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.rocketmq.schema.registry.client.serde.json;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.avro.io.EncoderFactory;
+import org.apache.rocketmq.schema.registry.client.SchemaRegistryClient;
+import org.apache.rocketmq.schema.registry.client.exceptions.RestClientException;
+import org.apache.rocketmq.schema.registry.client.exceptions.SerializationException;
+import org.apache.rocketmq.schema.registry.client.rest.JacksonMapper;
+import org.apache.rocketmq.schema.registry.client.serde.Serializer;
+import org.apache.rocketmq.schema.registry.common.constant.SchemaConstants;
+import org.apache.rocketmq.schema.registry.common.dto.GetSchemaResponse;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Map;
+
+public class JsonSerializer<T> implements Serializer<T> {
+    private final SchemaRegistryClient registryClient;
+    private final ObjectMapper objectMapper;
+    private final EncoderFactory encoderFactory = EncoderFactory.get();
+
+    public JsonSerializer(SchemaRegistryClient registryClient) {
+        this.objectMapper = JacksonMapper.INSTANCE;
+        this.registryClient = registryClient;
+    }
+
+    @Override
+    public void configure(Map<String, Object> configs) {
+
+    }
+
+    @Override
+    public byte[] serialize(String subject, T originMessage) {
+        if (null == originMessage) {
+            return null;
+        }
+
+        if (null == registryClient) {
+            throw new SerializationException("please initialize the schema registry client first");
+        }
+
+        try {
+            GetSchemaResponse response = registryClient.getSchemaBySubject(subject);
+            long schemaRecordId = response.getRecordId();
+            ByteArrayOutputStream out = new ByteArrayOutputStream();
+            ByteBuffer buffer = ByteBuffer.allocate(SchemaConstants.SCHEMA_RECORD_ID_LENGTH);
+            out.write(buffer.putLong(schemaRecordId).array());
+            out.write(objectMapper.writeValueAsBytes(originMessage));
+
+            byte[] bytes = out.toByteArray();
+            out.close();

Review Comment:
   I have fix this, please check this again.



-- 
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: dev-unsubscribe@rocketmq.apache.org

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