ConvertStandardString

This commit is contained in:
redkale
2024-09-29 14:27:52 +08:00
parent ab2773ba86
commit b724ce1a0c
30 changed files with 109 additions and 69 deletions

View File

@@ -163,12 +163,12 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
@Override @Override
public void convertTo(W out, Object value) { public void convertTo(W out, Object value) {
out.writeSmallString(value == null ? null : value.toString()); out.writeStandardString(value == null ? null : value.toString());
} }
@Override @Override
public Object convertFrom(R in) { public Object convertFrom(R in) {
String t = in.readSmallString(); String t = in.readStandardString();
return t == null ? null : sqldateInvoker.invoke(null, t); return t == null ? null : sqldateInvoker.invoke(null, t);
} }
}); });
@@ -179,12 +179,12 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
@Override @Override
public void convertTo(W out, Object value) { public void convertTo(W out, Object value) {
out.writeSmallString(value == null ? null : value.toString()); out.writeStandardString(value == null ? null : value.toString());
} }
@Override @Override
public Object convertFrom(R in) { public Object convertFrom(R in) {
String t = in.readSmallString(); String t = in.readStandardString();
return t == null ? null : sqltimeInvoker.invoke(null, t); return t == null ? null : sqltimeInvoker.invoke(null, t);
} }
}); });
@@ -195,12 +195,12 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
@Override @Override
public void convertTo(W out, Object value) { public void convertTo(W out, Object value) {
out.writeSmallString(value == null ? null : value.toString()); out.writeStandardString(value == null ? null : value.toString());
} }
@Override @Override
public Object convertFrom(R in) { public Object convertFrom(R in) {
String t = in.readSmallString(); String t = in.readStandardString();
return t == null ? null : timestampInvoker.invoke(null, t); return t == null ? null : timestampInvoker.invoke(null, t);
} }
}); });

View File

@@ -5,19 +5,22 @@
*/ */
package org.redkale.convert; package org.redkale.convert;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*; import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME; import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.*;
/** /**
* 序列化时标记String字段的值是否为无转义字符且长度不超过255的字符串通常用于类名、字段名、枚举值字符串等 * 序列化时标记String字段的值是否为无转义字符且长度不超过255的字符串通常用于类名、字段名、枚举值字符串等
* replace by {@link org.redkale.convert.ConvertStandardString}
* *
* <p>详情见: https://redkale.org * <p>详情见: https://redkale.org
* *
* @see org.redkale.convert.ConvertStandardString
* @deprecated 2.8.0
* @author zhangjx * @author zhangjx
* @since 2.3.0 * @since 2.3.0
*/ */
@Deprecated(since = "2.8.0")
@Target({METHOD, FIELD}) @Target({METHOD, FIELD})
@Retention(RUNTIME) @Retention(RUNTIME)
public @interface ConvertSmallString {} public @interface ConvertSmallString {}

View File

@@ -0,0 +1,23 @@
/*
* Copyright (c) 2016-2116 Redkale
* All rights reserved.
*/
package org.redkale.convert;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
/**
* 序列化时标记String字段的值是否为无转义字符且长度不超过255的字符串通常用于类名、字段名、枚举值字符串等
*
* <p>详情见: https://redkale.org
*
* @author zhangjx
* @since 2.8.0
*/
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface ConvertStandardString {}

View File

@@ -189,7 +189,7 @@ public abstract class Reader {
* *
* @return String值 * @return String值
*/ */
public abstract String readSmallString(); public abstract String readStandardString();
/** /**
* 读取反解析对象的类名 * 读取反解析对象的类名

View File

@@ -335,7 +335,7 @@ public abstract class Writer {
* *
* @param value 非空且不含需要转义的字符的String值 * @param value 非空且不含需要转义的字符的String值
*/ */
public abstract void writeSmallString(String value); public abstract void writeStandardString(String value);
/** /**
* 写入一个String值 * 写入一个String值

View File

@@ -151,7 +151,7 @@ public class BsonByteBufferReader extends BsonReader {
} }
@Override @Override
public final String readSmallString() { public final String readStandardString() {
int len = 0xff & readByte(); int len = 0xff & readByte();
if (len == 0) { if (len == 0) {
return ""; return "";

View File

@@ -255,7 +255,7 @@ public class BsonReader extends Reader {
@Override @Override
public final DeMember readFieldName(final DeMemberInfo memberInfo) { public final DeMember readFieldName(final DeMemberInfo memberInfo) {
final String exceptedField = readSmallString(); final String exceptedField = readStandardString();
this.fieldTypeEnum = readByte(); this.fieldTypeEnum = readByte();
return memberInfo.getMemberByField(exceptedField); return memberInfo.getMemberByField(exceptedField);
} }
@@ -345,11 +345,11 @@ public class BsonReader extends Reader {
@Override @Override
public final String readClassName() { public final String readClassName() {
return readSmallString(); return readStandardString();
} }
@Override @Override
public String readSmallString() { public String readStandardString() {
int len = 0xff & readByte(); int len = 0xff & readByte();
if (len == 0) { if (len == 0) {
return ""; return "";

View File

@@ -234,13 +234,13 @@ public class BsonWriter extends Writer implements ByteTuple {
@Override @Override
public final void writeClassName(String clazz) { public final void writeClassName(String clazz) {
writeSmallString(clazz == null ? "" : clazz); writeStandardString(clazz == null ? "" : clazz);
} }
@Override @Override
public final void writeObjectB(Object obj) { public final void writeObjectB(Object obj) {
super.writeObjectB(obj); super.writeObjectB(obj);
writeSmallString(""); writeStandardString("");
writeShort(BsonReader.SIGN_OBJECTB); writeShort(BsonReader.SIGN_OBJECTB);
} }
@@ -253,7 +253,7 @@ public class BsonWriter extends Writer implements ByteTuple {
@Override @Override
public final void writeFieldName(EnMember member, String fieldName, Type fieldType, int fieldPos) { public final void writeFieldName(EnMember member, String fieldName, Type fieldType, int fieldPos) {
writeByte(BsonReader.SIGN_HASNEXT); writeByte(BsonReader.SIGN_HASNEXT);
writeSmallString(fieldName); writeStandardString(fieldName);
writeByte(BsonFactory.typeEnum(fieldType)); writeByte(BsonFactory.typeEnum(fieldType));
} }
@@ -263,7 +263,7 @@ public class BsonWriter extends Writer implements ByteTuple {
* @param value String值 * @param value String值
*/ */
@Override @Override
public final void writeSmallString(String value) { public final void writeStandardString(String value) {
if (value.isEmpty()) { if (value.isEmpty()) {
writeTo((byte) 0); writeTo((byte) 0);
return; return;

View File

@@ -29,12 +29,12 @@ public final class BigDecimalSimpledCoder<R extends Reader, W extends Writer> ex
out.writeNull(); out.writeNull();
return; return;
} }
out.writeSmallString(value.toString()); out.writeStandardString(value.toString());
} }
@Override @Override
public BigDecimal convertFrom(R in) { public BigDecimal convertFrom(R in) {
String value = in.readSmallString(); String value = in.readStandardString();
if (value == null) { if (value == null) {
return null; return null;
} }
@@ -57,7 +57,7 @@ public final class BigDecimalSimpledCoder<R extends Reader, W extends Writer> ex
if (value == null) { if (value == null) {
out.writeNull(); out.writeNull();
} else { } else {
out.writeSmallString(value.toString()); out.writeStandardString(value.toString());
} }
} }

View File

@@ -55,7 +55,7 @@ public final class BigIntegerSimpledCoder<R extends Reader, W extends Writer> ex
if (value == null) { if (value == null) {
out.writeNull(); out.writeNull();
} else { } else {
out.writeSmallString(value.toString()); out.writeStandardString(value.toString());
} }
} }
@@ -86,7 +86,7 @@ public final class BigIntegerSimpledCoder<R extends Reader, W extends Writer> ex
out.writeNull(); out.writeNull();
} else { } else {
String s = value.toString(16); String s = value.toString(16);
out.writeSmallString(s.charAt(0) == '-' ? ("-0x" + s.substring(1)) : ("0x" + s)); out.writeStandardString(s.charAt(0) == '-' ? ("-0x" + s.substring(1)) : ("0x" + s));
} }
} }

View File

@@ -32,7 +32,7 @@ public class DurationSimpledCoder<R extends Reader, W extends Writer> extends Si
@Override @Override
public Duration convertFrom(R in) { public Duration convertFrom(R in) {
String value = in.readSmallString(); String value = in.readStandardString();
if (value == null) { if (value == null) {
return null; return null;
} }

View File

@@ -73,14 +73,14 @@ public final class EnumSimpledCoder<R extends Reader, W extends Writer, E extend
} else if (valueEncoder != null) { } else if (valueEncoder != null) {
valueEncoder.convertTo(out, enumToValues.get(value)); valueEncoder.convertTo(out, enumToValues.get(value));
} else { } else {
out.writeSmallString(value.toString()); out.writeStandardString(value.toString());
} }
} }
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public E convertFrom(final R in) { public E convertFrom(final R in) {
String value = in.readSmallString(); String value = in.readStandardString();
if (value == null) { if (value == null) {
return null; return null;
} }

View File

@@ -43,13 +43,13 @@ public class InstantSimpledCoder<R extends Reader, W extends Writer> extends Sim
if (value == null) { if (value == null) {
out.writeNull(); out.writeNull();
} else { } else {
out.writeSmallString(value.toString()); out.writeStandardString(value.toString());
} }
} }
@Override @Override
public Instant convertFrom(R in) { public Instant convertFrom(R in) {
final String str = in.readSmallString(); final String str = in.readStandardString();
if (str == null) { if (str == null) {
return null; return null;
} }

View File

@@ -45,12 +45,12 @@ public final class IntSimpledCoder<R extends Reader, W extends Writer> extends S
@Override @Override
public void convertTo(final W out, final Integer value) { public void convertTo(final W out, final Integer value) {
if (value == null) { if (value == null) {
out.writeSmallString("0x0"); out.writeStandardString("0x0");
} else { } else {
if (value < 0) { if (value < 0) {
throw new ConvertException("Negative values (" + value + ") are not supported"); throw new ConvertException("Negative values (" + value + ") are not supported");
} }
out.writeSmallString("0x" + Integer.toHexString(value)); out.writeStandardString("0x" + Integer.toHexString(value));
} }
} }

View File

@@ -59,13 +59,13 @@ public final class LocalDateSimpledCoder<R extends Reader, W extends Writer> ext
if (value == null) { if (value == null) {
out.writeNull(); out.writeNull();
} else { } else {
out.writeSmallString(value.toString()); out.writeStandardString(value.toString());
} }
} }
@Override @Override
public LocalDate convertFrom(R in) { public LocalDate convertFrom(R in) {
final String str = in.readSmallString(); final String str = in.readStandardString();
if (str == null) { if (str == null) {
return null; return null;
} }

View File

@@ -92,13 +92,13 @@ public final class LocalDateTimeSimpledCoder<R extends Reader, W extends Writer>
if (value == null) { if (value == null) {
out.writeNull(); out.writeNull();
} else { } else {
out.writeSmallString(value.toString()); out.writeStandardString(value.toString());
} }
} }
@Override @Override
public LocalDateTime convertFrom(R in) { public LocalDateTime convertFrom(R in) {
final String str = in.readSmallString(); final String str = in.readStandardString();
if (str == null) { if (str == null) {
return null; return null;
} }

View File

@@ -57,13 +57,13 @@ public final class LocalTimeSimpledCoder<R extends Reader, W extends Writer> ext
if (value == null) { if (value == null) {
out.writeNull(); out.writeNull();
} else { } else {
out.writeSmallString(value.toString()); out.writeStandardString(value.toString());
} }
} }
@Override @Override
public LocalTime convertFrom(R in) { public LocalTime convertFrom(R in) {
final String str = in.readSmallString(); final String str = in.readStandardString();
if (str == null) { if (str == null) {
return null; return null;
} }

View File

@@ -45,12 +45,12 @@ public final class LongSimpledCoder<R extends Reader, W extends Writer> extends
@Override @Override
public void convertTo(final W out, final Long value) { public void convertTo(final W out, final Long value) {
if (value == null) { if (value == null) {
out.writeSmallString("0x0"); out.writeStandardString("0x0");
} else { } else {
if (value < 0) { if (value < 0) {
throw new ConvertException("Negative values (" + value + ") are not supported"); throw new ConvertException("Negative values (" + value + ") are not supported");
} }
out.writeSmallString("0x" + Long.toHexString(value)); out.writeStandardString("0x" + Long.toHexString(value));
} }
} }

View File

@@ -39,12 +39,12 @@ public final class StringSimpledCoder<R extends Reader, W extends Writer> extend
@Override @Override
public void convertTo(W out, String value) { public void convertTo(W out, String value) {
out.writeSmallString(value); out.writeStandardString(value);
} }
@Override @Override
public String convertFrom(R in) { public String convertFrom(R in) {
return in.readSmallString(); return in.readStandardString();
} }
} }
} }

View File

@@ -25,13 +25,13 @@ public class TypeSimpledCoder<R extends Reader, W extends Writer> extends Simple
if (value == null) { if (value == null) {
out.writeNull(); out.writeNull();
} else { } else {
out.writeSmallString(value.getName()); out.writeStandardString(value.getName());
} }
} }
@Override @Override
public Class convertFrom(R in) { public Class convertFrom(R in) {
String str = in.readSmallString(); String str = in.readStandardString();
if (str == null) { if (str == null) {
return null; return null;
} }

View File

@@ -60,13 +60,13 @@ public final class Uint128SimpledCoder<R extends Reader, W extends Writer> exten
if (value == null) { if (value == null) {
out.writeNull(); out.writeNull();
} else { } else {
out.writeSmallString(value.toString()); out.writeStandardString(value.toString());
} }
} }
@Override @Override
public Uint128 convertFrom(R in) { public Uint128 convertFrom(R in) {
final String str = in.readSmallString(); final String str = in.readStandardString();
if (str == null) { if (str == null) {
return null; return null;
} }

View File

@@ -486,7 +486,7 @@ public class JsonByteBufferReader extends JsonReader {
* @return String值 * @return String值
*/ */
@Override @Override
public final String readSmallString() { public final String readStandardString() {
return readString(true); return readString(true);
} }

View File

@@ -144,12 +144,12 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
if (fieldType == int.class) { if (fieldType == int.class) {
intFieldCount++; intFieldCount++;
} }
if (fieldType == String.class && membersSize == 1 && readConvertSmallString(factory, element) != null) { if (fieldType == String.class && membersSize == 1 && isConvertStandardString(factory, element)) {
onlyOneLatin1FieldObjectFlag = true; onlyOneLatin1FieldObjectFlag = true;
} else if (fieldType != short.class } else if (fieldType != short.class
&& fieldType != int.class && fieldType != int.class
&& fieldType != long.class && fieldType != long.class
&& !(fieldType == String.class && readConvertSmallString(factory, element) != null)) { && !(fieldType == String.class && isConvertStandardString(factory, element))) {
onlyShotIntLongLatin1MoreFieldObjectFlag = false; onlyShotIntLongLatin1MoreFieldObjectFlag = false;
} }
} }
@@ -612,7 +612,7 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
mv.visitVarInsn(loadid, maxLocals); mv.visitVarInsn(loadid, maxLocals);
mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeDouble", "(D)V", false); mv.visitMethodInsn(INVOKEVIRTUAL, writerName, "writeDouble", "(D)V", false);
} else if (fieldType == String.class) { } else if (fieldType == String.class) {
if (readConvertSmallString(factory, element) == null) { if (!isConvertStandardString(factory, element)) {
mv.visitVarInsn(ALOAD, 1); mv.visitVarInsn(ALOAD, 1);
mv.visitVarInsn(loadid, maxLocals); mv.visitVarInsn(loadid, maxLocals);
mv.visitMethodInsn( mv.visitMethodInsn(
@@ -875,23 +875,37 @@ public abstract class JsonDynEncoder<T> extends ObjectEncoder<JsonWriter, T> {
} }
} }
protected static ConvertSmallString readConvertSmallString(JsonFactory factory, AccessibleObject element) { protected static boolean isConvertStandardString(JsonFactory factory, AccessibleObject element) {
if (element instanceof Field) { if (element instanceof Field) {
return ((Field) element).getAnnotation(ConvertSmallString.class); return ((Field) element).getAnnotation(ConvertStandardString.class) != null
|| ((Field) element).getAnnotation(ConvertSmallString.class) != null;
} }
Method method = (Method) element; Method method = (Method) element;
ConvertSmallString small = method.getAnnotation(ConvertSmallString.class); ConvertStandardString standard = method.getAnnotation(ConvertStandardString.class);
if (small == null) { if (standard == null) {
try { try {
Field f = method.getDeclaringClass().getDeclaredField(factory.readGetSetFieldName(method)); Field f = method.getDeclaringClass().getDeclaredField(factory.readGetSetFieldName(method));
if (f != null) { if (f != null) {
small = f.getAnnotation(ConvertSmallString.class); standard = f.getAnnotation(ConvertStandardString.class);
} }
} catch (Exception e) { } catch (Exception e) {
// do nothing // do nothing
} }
} }
return small; if (standard == null) {
if (method.getAnnotation(ConvertSmallString.class) != null) {
return true;
}
try {
Field f = method.getDeclaringClass().getDeclaredField(factory.readGetSetFieldName(method));
if (f != null && f.getAnnotation(ConvertSmallString.class) != null) {
return true;
}
} catch (Exception e) {
// do nothing
}
}
return standard != null;
} }
protected static Class readGetSetFieldType(AccessibleObject element) { protected static Class readGetSetFieldType(AccessibleObject element) {

View File

@@ -107,7 +107,7 @@ public class JsonReader extends Reader {
nextGoodChar(true); // 读掉 { [ nextGoodChar(true); // 读掉 { [
for (String key1 : keys) { for (String key1 : keys) {
while (this.hasNext()) { while (this.hasNext()) {
String field = this.readSmallString(); String field = this.readStandardString();
readBlank(); readBlank();
if (key1.equals(field)) { if (key1.equals(field)) {
break; break;
@@ -129,7 +129,7 @@ public class JsonReader extends Reader {
break; break;
case '{': case '{':
while (hasNext()) { while (hasNext()) {
this.readSmallString(); // 读掉field this.readStandardString(); // 读掉field
this.readBlank(); this.readBlank();
this.skipValue(); this.skipValue();
} }
@@ -663,7 +663,7 @@ public class JsonReader extends Reader {
} }
public final String readFieldName() { public final String readFieldName() {
return this.readSmallString(); return this.readStandardString();
} }
@Override @Override
@@ -708,7 +708,7 @@ public class JsonReader extends Reader {
@Override @Override
public final boolean readBoolean() { public final boolean readBoolean() {
return "true".equalsIgnoreCase(this.readSmallString()); return "true".equalsIgnoreCase(this.readStandardString());
} }
@Override @Override
@@ -759,7 +759,7 @@ public class JsonReader extends Reader {
@Override @Override
public final float readFloat() { public final float readFloat() {
String chars = readSmallString(); String chars = readStandardString();
if (chars != null) { if (chars != null) {
chars = chars.trim(); chars = chars.trim();
} }
@@ -778,7 +778,7 @@ public class JsonReader extends Reader {
@Override @Override
public final double readDouble() { public final double readDouble() {
String chars = readSmallString(); String chars = readStandardString();
if (chars != null) { if (chars != null) {
chars = chars.trim(); chars = chars.trim();
} }
@@ -796,7 +796,7 @@ public class JsonReader extends Reader {
} }
@Override @Override
public String readSmallString() { public String readStandardString() {
final int eof = this.limit; final int eof = this.limit;
if (this.position == eof) { if (this.position == eof) {
return null; return null;

View File

@@ -159,7 +159,7 @@ public abstract class JsonWriter extends Writer {
} }
@Override @Override
public final void writeSmallString(String value) { public final void writeStandardString(String value) {
writeLatin1To(true, value); writeLatin1To(true, value);
} }

View File

@@ -46,7 +46,7 @@ public class ProtobufEnumSimpledCoder<R extends ProtobufReader, W extends Protob
if (value == null) { if (value == null) {
out.writeNull(); out.writeNull();
} else if (enumtostring) { } else if (enumtostring) {
out.writeSmallString(value.toString()); out.writeStandardString(value.toString());
} else { } else {
out.writeUInt32(value.ordinal()); out.writeUInt32(value.ordinal());
} }
@@ -56,7 +56,7 @@ public class ProtobufEnumSimpledCoder<R extends ProtobufReader, W extends Protob
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public E convertFrom(final R in) { public E convertFrom(final R in) {
if (enumtostring) { if (enumtostring) {
String value = in.readSmallString(); String value = in.readStandardString();
return value == null ? null : (E) Enum.valueOf((Class<E>) type, value); return value == null ? null : (E) Enum.valueOf((Class<E>) type, value);
} else { } else {
int value = in.readRawVarint32(); int value = in.readRawVarint32();

View File

@@ -439,7 +439,7 @@ public class ProtobufReader extends Reader {
} }
@Override @Override
public final String readSmallString() { public final String readStandardString() {
return readString(); return readString();
} }

View File

@@ -385,7 +385,7 @@ public class ProtobufWriter extends Writer implements ByteTuple {
} }
@Override @Override
public final void writeSmallString(String value) { public final void writeStandardString(String value) {
writeString(value); writeString(value);
} }

View File

@@ -9,7 +9,7 @@ import java.lang.reflect.Type;
import java.util.*; import java.util.*;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import org.redkale.convert.Convert; import org.redkale.convert.Convert;
import org.redkale.convert.ConvertSmallString; import org.redkale.convert.ConvertStandardString;
import org.redkale.convert.Decodeable; import org.redkale.convert.Decodeable;
import org.redkale.convert.json.*; import org.redkale.convert.json.*;
import org.redkale.util.TypeToken; import org.redkale.util.TypeToken;
@@ -93,7 +93,7 @@ public class Json5Test {
public String name; public String name;
@ConvertSmallString @ConvertStandardString
public String desc; public String desc;
@Override @Override

View File

@@ -20,7 +20,7 @@ public final class Message {
private List<Long> longs; private List<Long> longs;
@ConvertSmallString @ConvertStandardString
private String message; private String message;
public Message() {} public Message() {}