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

fix bug of serialize or deserialie generic type use fastjson eg:resul… #390

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -34,7 +34,7 @@
*/
public class FastJsonObjectInput implements ObjectInput {

private final BufferedReader reader;
protected final BufferedReader reader;

public FastJsonObjectInput(InputStream in){
this(new InputStreamReader(in));
Expand Down Expand Up @@ -129,10 +129,11 @@ public <T> T readObject(Class<T> cls, Type type) throws IOException,ClassNotFoun
return (T) PojoUtils.realize(value, cls, type);
}

private String readLine() throws IOException, EOFException {
protected String readLine() throws IOException, EOFException {
String line = reader.readLine();
if(line == null || line.trim().length() == 0) throw new EOFException();
return line;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed 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 com.alibaba.dubbo.common.serialize.support.json;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Field;
import java.lang.reflect.Type;

import com.alibaba.dubbo.common.utils.PojoUtils;
import com.alibaba.dubbo.common.utils.ReflectUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.parser.DefaultJSONParser;
import com.alibaba.fastjson.parser.ParserConfig;

/**
* JsonObjectInput
*
* @author william.liangf
*/
public class GenericFastJsonObjectInput extends FastJsonObjectInput {

public GenericFastJsonObjectInput(InputStream in) {
super(in);
}

public GenericFastJsonObjectInput(Reader reader) {
super(reader);
}

protected boolean isConvertToJsonByType(Class<?> clz, Type type) {
if (clz == null) {
return false;
}

if (type == null) {
return false;
}

if (clz.isEnum()) {
return false;
}

if (ReflectUtils.isPrimitives(clz)) {
return false;
}
//
// if (clz.isArray()) {
// return false;
// }
// if (Collection.class.isAssignableFrom(clz)) {
// return false;
// }
//
// if (Map.class.isAssignableFrom(clz)) {
// return false;
// }
return true;
}

@SuppressWarnings("unchecked")
@Override
public <T> T readObject(Class<T> cls, Type type) throws IOException, ClassNotFoundException {
Object value = null;
if (isConvertToJsonByType(cls, type)) {
String json = readLine();
value = JSON.parseObject(json, type);
} else {
value = super.readObject(cls, type);
return (T) value;
}
return (T) PojoUtils.realize(value, cls, type);
}

private static ParserConfig dubboFastjsonConfig =null;

private static ParserConfig getDubboFastjsonConfigParseConfig(){
if (dubboFastjsonConfig==null){
ParserConfig newConfig =new ParserConfig();
ParserConfig globalInstance = ParserConfig.getGlobalInstance();
Field[] declaredFields = ParserConfig.class.getDeclaredFields();
for (Field field : declaredFields) {
if (!field.isAccessible()){
field.setAccessible(true);
}
try {
Object globalValue = field.get(globalInstance);
field.set(newConfig, globalValue);
} catch (IllegalArgumentException e) {
//skip
} catch (IllegalAccessException e) {
//skip
}
}
//必须是AutoTypeSupport=true
newConfig.setAutoTypeSupport(true);
dubboFastjsonConfig =newConfig;
}
return dubboFastjsonConfig;
}

@Override
public Object readObject() throws IOException, ClassNotFoundException {
String json = readLine();
if (json == null) {
return null;
}
DefaultJSONParser parser = new DefaultJSONParser(json, getDubboFastjsonConfigParseConfig(), JSON.DEFAULT_PARSER_FEATURE);
Object value = parser.parse();
parser.handleResovleTask(value);
parser.close();

return value;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright 1999-2011 Alibaba Group.
*
* Licensed 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 com.alibaba.dubbo.common.serialize.support.json;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.common.serialize.ObjectInput;
import com.alibaba.dubbo.common.serialize.ObjectOutput;
import com.alibaba.dubbo.common.serialize.Serialization;

/**
* FastJsonSerialization
*
* @author william.liangf
*/
public class GenericFastJsonSerialization implements Serialization {

public byte getContentTypeId() {
return 6;
}

public String getContentType() {
return "text/json";
}

public ObjectOutput serialize(URL url, OutputStream output) throws IOException {
return new FastJsonObjectOutput(output);
}

public ObjectInput deserialize(URL url, InputStream input) throws IOException {
return new GenericFastJsonObjectInput(input);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ hessian2=com.alibaba.dubbo.common.serialize.support.hessian.Hessian2Serializatio
java=com.alibaba.dubbo.common.serialize.support.java.JavaSerialization
compactedjava=com.alibaba.dubbo.common.serialize.support.java.CompactedJavaSerialization
json=com.alibaba.dubbo.common.serialize.support.json.JsonSerialization
fastjson=com.alibaba.dubbo.common.serialize.support.json.FastJsonSerialization
fastjson=com.alibaba.dubbo.common.serialize.support.json.GenericFastJsonSerialization
nativejava=com.alibaba.dubbo.common.serialize.support.nativejava.NativeJavaSerialization
kryo=com.alibaba.dubbo.common.serialize.support.kryo.KryoSerialization
fst=com.alibaba.dubbo.common.serialize.support.fst.FstSerialization
Expand Down