ProtobufAnyDecoder
This commit is contained in:
@@ -65,14 +65,13 @@ public class JsonAnyDecoder<T> implements Decodeable<JsonReader, T> {
|
||||
JsonReader.ValueType vt = in.readType();
|
||||
if (vt == null) {
|
||||
return null;
|
||||
} else if (vt == JsonReader.ValueType.ARRAY) {
|
||||
return (T) this.collectionDecoder.convertFrom(in);
|
||||
} else if (vt == JsonReader.ValueType.MAP) {
|
||||
return (T) this.mapDecoder.convertFrom(in);
|
||||
} else {
|
||||
return (T) stringFrom(in);
|
||||
}
|
||||
switch (vt) {
|
||||
case ARRAY:
|
||||
return (T) this.collectionDecoder.convertFrom(in);
|
||||
case MAP:
|
||||
return (T) this.mapDecoder.convertFrom(in);
|
||||
}
|
||||
return (T) stringFrom(in);
|
||||
}
|
||||
|
||||
protected T stringFrom(JsonReader in) {
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2116 Redkale
|
||||
* All rights reserved.
|
||||
*/
|
||||
package org.redkale.convert.pb;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import org.redkale.convert.Decodeable;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class ProtobufAnyDecoder<T> implements Decodeable<ProtobufReader, T> {
|
||||
|
||||
final ProtobufFactory factory;
|
||||
|
||||
ProtobufAnyDecoder(ProtobufFactory factory) {
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T convertFrom(ProtobufReader in) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getType() {
|
||||
return Object.class;
|
||||
}
|
||||
}
|
||||
@@ -1,51 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2116 Redkale
|
||||
* All rights reserved.
|
||||
*/
|
||||
package org.redkale.convert.pb;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import org.redkale.convert.Encodeable;
|
||||
|
||||
/**
|
||||
* 对不明类型的对象进行序列化; PROTOBUF序列化时将对象的类名写入Writer,JSON则不写入。
|
||||
*
|
||||
* <p>详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @param <T> 序列化的泛型类型
|
||||
*/
|
||||
public final class ProtobufAnyEncoder<T> implements Encodeable<ProtobufWriter, T> {
|
||||
|
||||
final ProtobufFactory factory;
|
||||
|
||||
ProtobufAnyEncoder(ProtobufFactory factory) {
|
||||
this.factory = factory;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void convertTo(final ProtobufWriter out, final T value) {
|
||||
if (value == null) {
|
||||
out.writeNull();
|
||||
} else {
|
||||
Class clazz = value.getClass();
|
||||
if (clazz == Object.class) {
|
||||
out.writeObjectB(value);
|
||||
out.writeObjectE(value);
|
||||
return;
|
||||
}
|
||||
factory.loadEncoder(clazz).convertTo(out, value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getType() {
|
||||
return Object.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean specifyable() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,8 @@ import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.*;
|
||||
import org.redkale.convert.Decodeable;
|
||||
import org.redkale.convert.Encodeable;
|
||||
import org.redkale.convert.SimpledCoder;
|
||||
import org.redkale.convert.ext.*;
|
||||
import org.redkale.util.*;
|
||||
@@ -31,6 +33,47 @@ public abstract class ProtobufCoders {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public static class ProtobufAnyDecoder<T> implements Decodeable<ProtobufReader, T> {
|
||||
|
||||
public static final ProtobufAnyDecoder instance = new ProtobufAnyDecoder();
|
||||
|
||||
@Override
|
||||
public T convertFrom(ProtobufReader in) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getType() {
|
||||
return Object.class;
|
||||
}
|
||||
}
|
||||
|
||||
public static class ProtobufAnyEncoder<T>
|
||||
implements Encodeable<ProtobufWriter, T>, ProtobufEncodeable<ProtobufWriter, T> {
|
||||
|
||||
public static final ProtobufAnyEncoder instance = new ProtobufAnyEncoder();
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public void convertTo(final ProtobufWriter out, final T value) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int computeSize(ProtobufWriter out, int tagSize, T value) {
|
||||
throw new UnsupportedOperationException("Not supported yet.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type getType() {
|
||||
return Object.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean specifyable() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// ------------------------------------- boolean -------------------------------------
|
||||
public static class ProtobufBoolSimpledCoder extends BoolSimpledCoder<ProtobufReader, ProtobufWriter>
|
||||
implements ProtobufPrimitivable<Boolean>, ProtobufEncodeable<ProtobufWriter, Boolean> {
|
||||
|
||||
@@ -77,8 +77,8 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
|
||||
@Override
|
||||
protected void initSimpleCoderInRoot() {
|
||||
super.initSimpleCoderInRoot();
|
||||
this.register(Object.class, new ProtobufAnyDecoder(this));
|
||||
this.register(Object.class, new ProtobufAnyEncoder(this));
|
||||
this.register(Object.class, ProtobufCoders.ProtobufAnyDecoder.instance);
|
||||
this.register(Object.class, ProtobufCoders.ProtobufAnyEncoder.instance);
|
||||
this.register(StringWrapper.class, ProtobufCoders.ProtobufStringWrapperSimpledCoder.instance);
|
||||
this.register(CharSequence.class, ProtobufCoders.ProtobufCharSequenceSimpledCoder.instance);
|
||||
this.register(StringBuilder.class, ProtobufCoders.ProtobufStringBuilderSimpledCoder.instance);
|
||||
|
||||
Reference in New Issue
Block a user