修复StringWrapper序列化json问题

This commit is contained in:
redkale
2023-08-12 22:05:20 +08:00
parent eb1c0e1513
commit 1d52cc5acf
3 changed files with 48 additions and 8 deletions

View File

@@ -10,7 +10,7 @@ import java.nio.charset.Charset;
import java.util.Objects;
import java.util.function.Supplier;
import org.redkale.convert.ConvertException;
import org.redkale.util.Utility;
import org.redkale.util.*;
/**
* 以ByteBuffer为数据载体的JsonWriter
@@ -787,7 +787,7 @@ public class JsonByteBufferWriter extends JsonWriter {
}
}
if (len == chs.length) {
writeTo(-1, true, chs, 0, len);
writeTo(-1, quote, chs, 0, len);
return;
}
int expandsize = -1;
@@ -860,6 +860,16 @@ public class JsonByteBufferWriter extends JsonWriter {
writeTo(expandsize, quote, cs, 0, sb.length());
}
@Override
public void writeWrapper(StringWrapper wrapper) {
if (wrapper == null || wrapper.getValue() == null) {
writeNull();
return;
}
final char[] chs = Utility.charArray(wrapper.getValue());
writeTo(-1, false, chs, 0, chs.length);
}
@Override
public String toString() {
return Objects.toString(this);

View File

@@ -490,6 +490,42 @@ public class JsonBytesWriter extends JsonWriter implements ByteTuple {
count = curr;
}
@Override
public void writeWrapper(StringWrapper wrapper) {
if (wrapper == null || wrapper.getValue() == null) {
writeNull();
return;
}
String value = wrapper.getValue();
if (Utility.isLatin1(value)) {
writeTo(Utility.latin1ByteArray(value));
return;
}
byte[] bytes = expand(value.length() * 4 + 2);
int curr = count;
int len = value.length();
for (int i = 0; i < len; i++) {
char ch = value.charAt(i);
if (ch < 0x80) {
bytes[curr++] = (byte) ch;
} else if (ch < 0x800) {
bytes[curr++] = (byte) (0xc0 | (ch >> 6));
bytes[curr++] = (byte) (0x80 | (ch & 0x3f));
} else if (Character.isSurrogate(ch)) { //连取两个
int uc = Character.toCodePoint(ch, value.charAt(++i));
bytes[curr++] = (byte) (0xf0 | ((uc >> 18)));
bytes[curr++] = (byte) (0x80 | ((uc >> 12) & 0x3f));
bytes[curr++] = (byte) (0x80 | ((uc >> 6) & 0x3f));
bytes[curr++] = (byte) (0x80 | (uc & 0x3f));
} else {
bytes[curr++] = (byte) (0xe0 | ((ch >> 12)));
bytes[curr++] = (byte) (0x80 | ((ch >> 6) & 0x3f));
bytes[curr++] = (byte) (0x80 | (ch & 0x3f));
}
}
count = curr;
}
@Override
public String toString() {
return new String(content, 0, count, StandardCharsets.UTF_8);

View File

@@ -7,7 +7,6 @@ package org.redkale.convert.json;
import java.lang.reflect.Type;
import org.redkale.convert.*;
import org.redkale.util.StringWrapper;
/**
*
@@ -172,11 +171,6 @@ public abstract class JsonWriter extends Writer {
writeLatin1To(false, String.valueOf(value));
}
@Override
public final void writeWrapper(StringWrapper value) {
writeString(false, String.valueOf(value));
}
@Override
public final boolean needWriteClassName() {
return false;