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

[Dubbo-4632]protobuf-json序列化bug #4634

Merged
merged 1 commit into from
Jul 23, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
*/
package org.apache.dubbo.common.serialize.protobuf.support;

import com.google.common.base.Charsets;
import com.google.protobuf.BoolValue;
import com.google.protobuf.DoubleValue;
import com.google.protobuf.FloatValue;
import com.google.protobuf.Int32Value;
import com.google.protobuf.Int64Value;
import com.google.protobuf.StringValue;
import org.apache.dubbo.common.serialize.ObjectInput;

import java.io.BufferedReader;
Expand All @@ -24,6 +31,7 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.Map;

/**
* GenericGoogleProtobuf object input implementation
Expand All @@ -37,52 +45,52 @@ public GenericProtobufObjectInput(InputStream in) {

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

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

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

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

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

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

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

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

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

@Override
public Object readObject() throws IOException {
return read(String.class);
public Object readObject() {
throw new UnsupportedOperationException();
}

@Override
Expand All @@ -105,7 +113,11 @@ private String readLine() throws IOException {
}

private <T> T read(Class<T> cls) throws IOException {
if (!ProtobufUtils.isSupported(cls)) {
if (cls.equals(Map.class)) {
// only for attachments
String json = readLine();
return (T) ProtobufUtils.deserialize(json, MapValue.Map.class).getAttachmentsMap();
} else if (!ProtobufUtils.isSupported(cls)) {
throw new IllegalArgumentException("This serialization only support google protobuf entity, the class is :" + cls.getName());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,20 @@
*/
package org.apache.dubbo.common.serialize.protobuf.support;

import com.google.common.base.Charsets;
import com.google.protobuf.BoolValue;
import com.google.protobuf.DoubleValue;
import com.google.protobuf.FloatValue;
import com.google.protobuf.Int32Value;
import com.google.protobuf.Int64Value;
import com.google.protobuf.StringValue;
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.util.Map;

/**
* GenericGoogleProtobuf object output implementation
Expand All @@ -36,61 +44,65 @@ public GenericProtobufObjectOutput(OutputStream out) {

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

writeObject(BoolValue.newBuilder().setValue(v).build());
}

@Override
public void writeByte(byte v) throws IOException {
writeObject(v);
writeObject(Int32Value.newBuilder().setValue((v)).build());
}

@Override
public void writeShort(short v) throws IOException {
writeObject(v);
writeObject(Int32Value.newBuilder().setValue(v).build());
}

@Override
public void writeInt(int v) throws IOException {
writeObject(v);
writeObject(Int32Value.newBuilder().setValue(v).build());
}

@Override
public void writeLong(long v) throws IOException {
writeObject(v);
writeObject(Int64Value.newBuilder().setValue(v).build());
}

@Override
public void writeFloat(float v) throws IOException {
writeObject(v);
writeObject(FloatValue.newBuilder().setValue(v).build());
}

@Override
public void writeDouble(double v) throws IOException {
writeObject(v);
writeObject(DoubleValue.newBuilder().setValue(v).build());
}

@Override
public void writeUTF(String v) throws IOException {
writeObject(v);
writeObject(StringValue.newBuilder().setValue(v).build());
}

@Override
public void writeBytes(byte[] b) {
writer.println(new String(b));
public void writeBytes(byte[] b) throws IOException {
writeUTF(new String(b, Charsets.ISO_8859_1));
}

@Override
public void writeBytes(byte[] b, int off, int len) {
writer.println(new String(b, off, len));
public void writeBytes(byte[] b, int off, int len) throws IOException {
writeUTF(new String(b, off, len, Charsets.ISO_8859_1));
}


@Override
public void writeObject(Object obj) throws IOException {
if (obj == null) {
throw new IllegalArgumentException("This serialization only support google protobuf object, the object is : null");
}

if (!ProtobufUtils.isSupported(obj.getClass())) {
if (obj instanceof Map) {
// only for attachment
obj = MapValue.Map.newBuilder().putAllAttachments((Map) obj).build();
} else if (!ProtobufUtils.isSupported(obj.getClass())) {
throw new IllegalArgumentException("This serialization only support google protobuf object, the object class is: " + obj.getClass().getName());
}

Expand Down
Loading