diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/FastJsonObjectInput.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/FastJsonObjectInput.java index bf38939c3539..88cb3ff3e8d2 100644 --- a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/FastJsonObjectInput.java +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/FastJsonObjectInput.java @@ -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)); @@ -129,10 +129,11 @@ public T readObject(Class 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; } + } \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/GenericFastJsonObjectInput.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/GenericFastJsonObjectInput.java new file mode 100644 index 000000000000..e116c5558d62 --- /dev/null +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/GenericFastJsonObjectInput.java @@ -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 readObject(Class 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; + } + +} \ No newline at end of file diff --git a/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/GenericFastJsonSerialization.java b/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/GenericFastJsonSerialization.java new file mode 100644 index 000000000000..03eca651f8f3 --- /dev/null +++ b/dubbo-common/src/main/java/com/alibaba/dubbo/common/serialize/support/json/GenericFastJsonSerialization.java @@ -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); + } + +} \ No newline at end of file diff --git a/dubbo-common/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization b/dubbo-common/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization index 2362c9acb886..47c1d27519c4 100644 --- a/dubbo-common/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization +++ b/dubbo-common/src/main/resources/META-INF/dubbo/internal/com.alibaba.dubbo.common.serialize.Serialization @@ -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