json
This commit is contained in:
@@ -877,29 +877,30 @@ public class JsonReader extends Reader {
|
|||||||
|
|
||||||
protected String readString(boolean flag) {
|
protected String readString(boolean flag) {
|
||||||
final char[] text0 = this.text;
|
final char[] text0 = this.text;
|
||||||
char expected = nextGoodChar(true);
|
final int end = limit;
|
||||||
|
char quote = nextGoodChar(true);
|
||||||
int curr = this.position;
|
int curr = this.position;
|
||||||
if (expected != '"' && expected != '\'') {
|
if (quote != '"' && quote != '\'') {
|
||||||
if (expected == 'n'
|
final int start = curr;
|
||||||
&& text0.length > curr + 3
|
if (quote == 'n'
|
||||||
&& (text0[1 + curr] == 'u' && text0[2 + curr] == 'l' && text0[3 + curr] == 'l')) {
|
&& end >= curr + 3
|
||||||
if (text0[++curr] == 'u' && text0[++curr] == 'l' && text0[++curr] == 'l') {
|
&& text0[1 + curr] == 'u'
|
||||||
|
&& text0[2 + curr] == 'l'
|
||||||
|
&& text0[3 + curr] == 'l') {
|
||||||
|
curr += 3;
|
||||||
this.position = curr;
|
this.position = curr;
|
||||||
if (text0.length > curr + 4) {
|
if (curr < end) {
|
||||||
char ch = text0[curr + 1];
|
char ch = text0[++curr];
|
||||||
if (ch == ',' || ch <= ' ' || ch == '}' || ch == ']' || (flag && ch == ':')) {
|
if (ch == ',' || ch <= ' ' || ch == '}' || ch == ']' || (flag && ch == ':')) {
|
||||||
|
// null值
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
final int start = curr - 3;
|
// null开头的字符串
|
||||||
for (; ; ) {
|
for (; curr <= end; curr++) {
|
||||||
if (curr >= text0.length) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
ch = text0[curr];
|
ch = text0[curr];
|
||||||
if (ch == ',' || ch <= ' ' || ch == '}' || ch == ']' || (flag && ch == ':')) {
|
if (ch == ',' || ch <= ' ' || ch == '}' || ch == ']' || (flag && ch == ':')) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
curr++;
|
|
||||||
}
|
}
|
||||||
if (curr == start) {
|
if (curr == start) {
|
||||||
throw new ConvertException("expected a string after a key but '" + text0[position]
|
throw new ConvertException("expected a string after a key but '" + text0[position]
|
||||||
@@ -907,14 +908,12 @@ public class JsonReader extends Reader {
|
|||||||
}
|
}
|
||||||
this.position = curr - 1;
|
this.position = curr - 1;
|
||||||
return new String(text0, start, curr - start);
|
return new String(text0, start, curr - start);
|
||||||
} else {
|
} else { // null值,已到尾部
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
final int start = curr;
|
|
||||||
for (; ; ) {
|
for (; ; ) {
|
||||||
if (curr >= text0.length) {
|
if (curr > end) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
char ch = text0[curr];
|
char ch = text0[curr];
|
||||||
@@ -930,17 +929,13 @@ public class JsonReader extends Reader {
|
|||||||
this.position = curr - 1;
|
this.position = curr - 1;
|
||||||
return new String(text0, start, curr - start);
|
return new String(text0, start, curr - start);
|
||||||
}
|
}
|
||||||
this.position = curr;
|
|
||||||
throw new ConvertException("expected a ':' after a key but '" + text0[position] + "' (position = "
|
|
||||||
+ position + ") in " + new String(this.text));
|
|
||||||
}
|
}
|
||||||
final int start = ++curr;
|
final int start = ++curr;
|
||||||
int end = limit;
|
|
||||||
CharArray array = null;
|
CharArray array = null;
|
||||||
char c;
|
char c;
|
||||||
for (; curr <= end; curr++) {
|
for (; curr <= end; curr++) {
|
||||||
char ch = text0[curr];
|
char ch = text0[curr];
|
||||||
if (ch == expected) {
|
if (ch == quote) {
|
||||||
break;
|
break;
|
||||||
} else if (ch == '\\') {
|
} else if (ch == '\\') {
|
||||||
if (array == null) {
|
if (array == null) {
|
||||||
|
|||||||
@@ -5,11 +5,14 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.test.convert;
|
package org.redkale.test.convert;
|
||||||
|
|
||||||
|
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.ConvertSmallString;
|
||||||
|
import org.redkale.convert.Decodeable;
|
||||||
import org.redkale.convert.json.*;
|
import org.redkale.convert.json.*;
|
||||||
|
import org.redkale.util.TypeToken;
|
||||||
|
|
||||||
/** @author zhangjx */
|
/** @author zhangjx */
|
||||||
public class Json5Test {
|
public class Json5Test {
|
||||||
@@ -18,6 +21,7 @@ public class Json5Test {
|
|||||||
Json5Test test = new Json5Test();
|
Json5Test test = new Json5Test();
|
||||||
test.run1();
|
test.run1();
|
||||||
test.run2();
|
test.run2();
|
||||||
|
test.run3();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@@ -55,6 +59,28 @@ public class Json5Test {
|
|||||||
System.out.println(convert.convertTo(bean));
|
System.out.println(convert.convertTo(bean));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void run3() throws Exception {
|
||||||
|
Decodeable<JsonReader, String> decoder = JsonFactory.root().loadDecoder(String.class);
|
||||||
|
String val = decoder.convertFrom(new JsonReader("null"));
|
||||||
|
Assertions.assertTrue(val == null);
|
||||||
|
val = decoder.convertFrom(new JsonReader("nullable"));
|
||||||
|
Assertions.assertEquals("nullable", val);
|
||||||
|
|
||||||
|
LinkedHashMap<String, String> map = new LinkedHashMap<>();
|
||||||
|
map.put("name1", null);
|
||||||
|
map.put("name2", "nullable");
|
||||||
|
map.put("site", "002");
|
||||||
|
String json = JsonConvert.root().convertTo(map);
|
||||||
|
System.out.println(json);
|
||||||
|
Map<String, String> rs = JsonConvert.root().convertFrom(MAP_TYPE, json.replace("\"nullable\"", "nullable"));
|
||||||
|
String json2 = JsonConvert.root().convertTo(rs);
|
||||||
|
System.out.println(json2);
|
||||||
|
Assertions.assertEquals(json, json2);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Type MAP_TYPE = new TypeToken<LinkedHashMap<String, String>>() {}.getType();
|
||||||
|
|
||||||
public static class Json5Bean {
|
public static class Json5Bean {
|
||||||
|
|
||||||
public int id;
|
public int id;
|
||||||
|
|||||||
Reference in New Issue
Block a user