Convert增加StringConvertWrapper功能

This commit is contained in:
Redkale
2019-03-29 11:44:37 +08:00
parent 129ed25ca4
commit e4672355fc
6 changed files with 107 additions and 0 deletions

View File

@@ -91,6 +91,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
this.register(Number.class, NumberSimpledCoder.instance);
this.register(String.class, StringSimpledCoder.instance);
this.register(StringConvertWrapper.class, StringConvertWrapperSimpledCoder.instance);
this.register(CharSequence.class, CharSequenceSimpledCoder.instance);
this.register(java.util.Date.class, DateSimpledCoder.instance);
this.register(java.time.Duration.class, DurationSimpledCoder.instance);

View File

@@ -0,0 +1,55 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.convert;
/**
* 序列化去掉引号的String对象。
* <blockquote><pre>
* 场景: JavaBean a = ... ;
* Map map = new HashMap();
* map.put("bean", a);
* records.add(map);
* records需要在后期序列化写入库。 但是在这期间a的内部字段值可能就变化了会导致入库时并不是records.add的快照信息。
* 所以需要使用StringConvertWrapper
* Map map = new HashMap();
* map.put("bean", new StringConvertWrapper(a.toString()));
* records.add(map);
* 这样序列化写库时不需要在bean的值上面多一层引号。
* </pre></blockquote>
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*/
public class StringConvertWrapper {
protected String value;
public StringConvertWrapper() {
}
public StringConvertWrapper(String value) {
this.value = value;
}
public static StringConvertWrapper create(String value) {
return new StringConvertWrapper(value);
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}

View File

@@ -256,4 +256,11 @@ public abstract class Writer {
* @param value String值
*/
public abstract void writeString(String value);
/**
* 写入一个StringConvertWrapper值
*
* @param value StringConvertWrapper值
*/
public abstract void writeWrapper(StringConvertWrapper value);
}

View File

@@ -241,6 +241,11 @@ public class BsonWriter extends Writer {
writeTo(bytes);
}
@Override
public final void writeWrapper(StringConvertWrapper value) {
this.writeString(value == null ? null : value.getValue());
}
@Override
public final void writeNull() {
writeShort(Reader.SIGN_NULL);

View File

@@ -0,0 +1,34 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.redkale.convert.ext;
import org.redkale.convert.*;
/**
* String 的SimpledCoder实现
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class StringConvertWrapperSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, StringConvertWrapper> {
public static final StringConvertWrapperSimpledCoder instance = new StringConvertWrapperSimpledCoder();
@Override
public void convertTo(W out, StringConvertWrapper value) {
out.writeWrapper(value);
}
@Override
public StringConvertWrapper convertFrom(R in) {
return new StringConvertWrapper(in.readString());
}
}

View File

@@ -319,6 +319,11 @@ public class JsonWriter extends Writer {
writeTo(false, String.valueOf(value));
}
@Override
public final void writeWrapper(StringConvertWrapper value) {
writeTo(false, String.valueOf(value));
}
@Override
public final boolean needWriteClassName() {
return false;