Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add jackson serialization extension #231

Merged
merged 4 commits into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions dubbo-extensions-dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
<fastjson_version>1.2.83</fastjson_version>
<fst_version>2.48-jdk-6</fst_version>
<fury_version>0.2.0</fury_version>
<jackson_version>2.15.3</jackson_version>
<gson_version>2.8.9</gson_version>
<kryo_version>5.4.0</kryo_version>
<kryo_serializers_version>0.45</kryo_serializers_version>
Expand Down Expand Up @@ -304,6 +305,26 @@
<artifactId>fury-core</artifactId>
<version>${fury_version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson_version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson_version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson_version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson_version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
61 changes: 61 additions & 0 deletions dubbo-serialization-extensions/dubbo-serialization-jackson/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!--
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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.dubbo.extensions</groupId>
<artifactId>dubbo-serialization-extensions</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>dubbo-serialization-jackson</artifactId>
<description>The jackson serialization module of dubbo project</description>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-api</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*
* 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.dubbo.common.serialize.jackson;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.dubbo.common.serialize.ObjectInput;

import java.io.BufferedReader;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;

/**
* Jackson object input implementation
*/
public class JacksonObjectInput implements ObjectInput {

private final ObjectMapper MAPPER;

private final BufferedReader READER;

public JacksonObjectInput(InputStream inputStream) {
this(new InputStreamReader(inputStream));
}

public JacksonObjectInput(Reader reader) {
this.READER = new BufferedReader(reader);
this.MAPPER = new ObjectMapper()
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.registerModule(new JavaTimeModule())
;
}

@Override
public Object readObject() throws IOException, ClassNotFoundException {
return read(Object.class);
}

@Override
public <T> T readObject(Class<T> cls) throws IOException, ClassNotFoundException {
return read(cls);
}

@Override
public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFoundException {
return read(type);
}

@Override
public boolean readBool() throws IOException {
return read(boolean.class);
}

@Override
public byte readByte() throws IOException {
return read(byte.class);
}

@Override
public short readShort() throws IOException {
return read(short.class);
}

@Override
public int readInt() throws IOException {
return read(int.class);
}

@Override
public long readLong() throws IOException {
return read(long.class);
}

@Override
public float readFloat() throws IOException {
return read(float.class);
}

@Override
public double readDouble() throws IOException {
return read(double.class);
}

@Override
public String readUTF() throws IOException {
return read(String.class);
}

@Override
public byte[] readBytes() throws IOException {
return readLine().getBytes();
}

private String readLine() throws IOException {
String line = READER.readLine();
if (line == null || line.trim().isEmpty()) {
throw new EOFException();
}
return line;
}

private <T> T read(Class<T> cls) throws IOException {
String json = readLine();
return MAPPER.readValue(json, cls);
}

private <T> T read(Type type) throws IOException {
String json = readLine();
return MAPPER.readValue(json, new TypeReference<T>() {
@Override
public Type getType() {
return type;
}
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* 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.dubbo.common.serialize.jackson;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.dubbo.common.serialize.ObjectOutput;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;

/**
* Jackson object output implementation
*/
public class JacksonObjectOutput implements ObjectOutput {

private final ObjectMapper MAPPER;

private final PrintWriter WRITER;

public JacksonObjectOutput(OutputStream outputStream) {
this(new OutputStreamWriter(outputStream));
}

public JacksonObjectOutput(Writer writer) {
this.WRITER = new PrintWriter(writer);
this.MAPPER = new ObjectMapper()
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.registerModule(new JavaTimeModule())
;
}

@Override
public void writeObject(Object obj) throws IOException {
char[] jsonChars = convertJsonToCharArray(MAPPER.writeValueAsString(obj));
WRITER.write(jsonChars, 0, jsonChars.length);
WRITER.println();
WRITER.flush();
}

@Override
public void writeBool(boolean v) throws IOException {
writeObject(v);
}

@Override
public void writeByte(byte v) throws IOException {
writeObject(v);
}

@Override
public void writeShort(short v) throws IOException {
writeObject(v);
}

@Override
public void writeInt(int v) throws IOException {
writeObject(v);
}

@Override
public void writeLong(long v) throws IOException {
writeObject(v);
}

@Override
public void writeFloat(float v) throws IOException {
writeObject(v);
}

@Override
public void writeDouble(double v) throws IOException {
writeObject(v);
}

@Override
public void writeUTF(String v) throws IOException {
writeObject(v);
}

@Override
public void writeBytes(byte[] v) throws IOException {
WRITER.println(new String(v));
}

@Override
public void writeBytes(byte[] v, int off, int len) throws IOException {
WRITER.println(new String(v, off, len));
}

@Override
public void flushBuffer() throws IOException {
WRITER.flush();
}

private char[] convertJsonToCharArray(String json) {
return json.toCharArray();
}

}
Loading
Loading