ProtobufAnyDecoder

This commit is contained in:
redkale
2024-10-22 09:58:10 +08:00
parent 4695d787c5
commit f9744b8480
5 changed files with 51 additions and 91 deletions

View File

@@ -65,14 +65,13 @@ public class JsonAnyDecoder<T> implements Decodeable<JsonReader, T> {
JsonReader.ValueType vt = in.readType(); JsonReader.ValueType vt = in.readType();
if (vt == null) { if (vt == null) {
return 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) { protected T stringFrom(JsonReader in) {

View File

@@ -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;
}
}

View File

@@ -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序列化时将对象的类名写入WriterJSON则不写入。
*
* <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;
}
}

View File

@@ -14,6 +14,8 @@ import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.*; import java.util.*;
import java.util.concurrent.atomic.*; import java.util.concurrent.atomic.*;
import org.redkale.convert.Decodeable;
import org.redkale.convert.Encodeable;
import org.redkale.convert.SimpledCoder; import org.redkale.convert.SimpledCoder;
import org.redkale.convert.ext.*; import org.redkale.convert.ext.*;
import org.redkale.util.*; import org.redkale.util.*;
@@ -31,6 +33,47 @@ public abstract class ProtobufCoders {
// do nothing // 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 ------------------------------------- // ------------------------------------- boolean -------------------------------------
public static class ProtobufBoolSimpledCoder extends BoolSimpledCoder<ProtobufReader, ProtobufWriter> public static class ProtobufBoolSimpledCoder extends BoolSimpledCoder<ProtobufReader, ProtobufWriter>
implements ProtobufPrimitivable<Boolean>, ProtobufEncodeable<ProtobufWriter, Boolean> { implements ProtobufPrimitivable<Boolean>, ProtobufEncodeable<ProtobufWriter, Boolean> {

View File

@@ -77,8 +77,8 @@ public class ProtobufFactory extends ConvertFactory<ProtobufReader, ProtobufWrit
@Override @Override
protected void initSimpleCoderInRoot() { protected void initSimpleCoderInRoot() {
super.initSimpleCoderInRoot(); super.initSimpleCoderInRoot();
this.register(Object.class, new ProtobufAnyDecoder(this)); this.register(Object.class, ProtobufCoders.ProtobufAnyDecoder.instance);
this.register(Object.class, new ProtobufAnyEncoder(this)); this.register(Object.class, ProtobufCoders.ProtobufAnyEncoder.instance);
this.register(StringWrapper.class, ProtobufCoders.ProtobufStringWrapperSimpledCoder.instance); this.register(StringWrapper.class, ProtobufCoders.ProtobufStringWrapperSimpledCoder.instance);
this.register(CharSequence.class, ProtobufCoders.ProtobufCharSequenceSimpledCoder.instance); this.register(CharSequence.class, ProtobufCoders.ProtobufCharSequenceSimpledCoder.instance);
this.register(StringBuilder.class, ProtobufCoders.ProtobufStringBuilderSimpledCoder.instance); this.register(StringBuilder.class, ProtobufCoders.ProtobufStringBuilderSimpledCoder.instance);