StringConvertWrapper 改成 StringWrapper

This commit is contained in:
Redkale
2019-08-30 15:18:03 +08:00
parent 543ecc071a
commit f767f40e56
7 changed files with 49 additions and 72 deletions

View File

@@ -91,7 +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(StringWrapper.class, StringWrapperSimpledCoder.instance);
this.register(CharSequence.class, CharSequenceSimpledCoder.instance);
this.register(StringBuilder.class, CharSequenceSimpledCoder.StringBuilderSimpledCoder.instance);
this.register(java.util.Date.class, DateSimpledCoder.instance);

View File

@@ -1,63 +0,0 @@
/*
* 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;
import org.redkale.convert.json.JsonConvert;
/**
* 序列化去掉引号的String对象。
* <blockquote><pre>
* 场景: JavaBean bean = ... ;
* Map map = new HashMap();
* map.put("bean", a);
* records.add(map);
* records需要在后期序列化写入库。 但是在这期间bean的内部字段值可能就变化了会导致入库时并不是records.add的快照信息。
* 所以需要使用StringConvertWrapper
* Map map = new HashMap();
* map.put("bean", new StringConvertWrapper(bean.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(Object value) {
return create(JsonConvert.root(), value);
}
public static StringConvertWrapper create(TextConvert convert, Object value) {
if (value == null) return new StringConvertWrapper(null);
if (value instanceof String) return new StringConvertWrapper((String) value);
return new StringConvertWrapper(convert.convertTo(value));
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}

View File

@@ -6,6 +6,7 @@
package org.redkale.convert;
import java.lang.reflect.*;
import org.redkale.util.StringWrapper;
/**
* 序列化的数据输出流
@@ -262,5 +263,5 @@ public abstract class Writer {
*
* @param value StringConvertWrapper值
*/
public abstract void writeWrapper(StringConvertWrapper value);
public abstract void writeWrapper(StringWrapper value);
}

View File

@@ -242,7 +242,7 @@ public class BsonWriter extends Writer {
}
@Override
public final void writeWrapper(StringConvertWrapper value) {
public final void writeWrapper(StringWrapper value) {
this.writeString(value == null ? null : value.getValue());
}

View File

@@ -6,6 +6,7 @@
package org.redkale.convert.ext;
import org.redkale.convert.*;
import org.redkale.util.StringWrapper;
/**
* String 的SimpledCoder实现
@@ -17,18 +18,18 @@ import org.redkale.convert.*;
* @param <R> Reader输入的子类型
* @param <W> Writer输出的子类型
*/
public final class StringConvertWrapperSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, StringConvertWrapper> {
public final class StringWrapperSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, StringWrapper> {
public static final StringConvertWrapperSimpledCoder instance = new StringConvertWrapperSimpledCoder();
public static final StringWrapperSimpledCoder instance = new StringWrapperSimpledCoder();
@Override
public void convertTo(W out, StringConvertWrapper value) {
public void convertTo(W out, StringWrapper value) {
out.writeWrapper(value);
}
@Override
public StringConvertWrapper convertFrom(R in) {
return new StringConvertWrapper(in.readString());
public StringWrapper convertFrom(R in) {
return new StringWrapper(in.readString());
}
}

View File

@@ -320,7 +320,7 @@ public class JsonWriter extends Writer {
}
@Override
public final void writeWrapper(StringConvertWrapper value) {
public final void writeWrapper(StringWrapper value) {
writeTo(false, String.valueOf(value));
}

View File

@@ -0,0 +1,38 @@
/*
* 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.util;
import java.io.Serializable;
/**
*
* @author zhangjx
*/
public class StringWrapper implements Serializable {
protected String value;
public StringWrapper() {
}
public StringWrapper(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return value;
}
}