修复JsonByteBuffer解析bug

This commit is contained in:
redkale
2023-05-09 14:45:25 +08:00
parent 454e6b60d7
commit 2202465e31
4 changed files with 224 additions and 9 deletions

View File

@@ -164,6 +164,7 @@ public class JsonByteBufferReader extends JsonReader {
if (ch == 'N' && nextChar() == 'U' && nextChar() == 'L' && nextChar() == 'L') { if (ch == 'N' && nextChar() == 'U' && nextChar() == 'L' && nextChar() == 'L') {
return SIGN_NULL; return SIGN_NULL;
} }
int pos = this.position;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(ch); sb.append(ch);
char one; char one;
@@ -171,7 +172,7 @@ public class JsonByteBufferReader extends JsonReader {
while ((one = nextChar()) != 0) sb.append(one); while ((one = nextChar()) != 0) sb.append(one);
} catch (Exception e) { } catch (Exception e) {
} }
throw new ConvertException("a json array text must begin with '[' (position = " + position + ") but '" + ch + "' in (" + sb + ")"); throw new ConvertException("a json array text must begin with '[' (position = " + pos + ") but '" + ch + "' in (" + sb + ")");
} }
/** /**
@@ -183,6 +184,7 @@ public class JsonByteBufferReader extends JsonReader {
if (ch == ':') { if (ch == ':') {
return; return;
} }
int pos = this.position;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append(ch); sb.append(ch);
char one; char one;
@@ -190,7 +192,7 @@ public class JsonByteBufferReader extends JsonReader {
while ((one = nextChar()) != 0) sb.append(one); while ((one = nextChar()) != 0) sb.append(one);
} catch (Exception e) { } catch (Exception e) {
} }
throw new ConvertException("expected a ':' but '" + ch + "'(position = " + position + ") in (" + sb + ")"); throw new ConvertException("expected a ':' but '" + ch + "'(position = " + pos + ") in (" + sb + ")");
} }
/** /**

View File

@@ -365,7 +365,7 @@ public class JsonReader extends Reader {
if (nt == '}' || nt == ']') { if (nt == '}' || nt == ']') {
return false; return false;
} }
backChar(ch); backChar(nt);
return true; return true;
} }
if (ch == '}' || ch == ']') { if (ch == '}' || ch == ']') {

View File

@@ -9,7 +9,7 @@ import java.io.*;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.channels.ReadableByteChannel; import java.nio.channels.ReadableByteChannel;
import java.nio.charset.*; import java.nio.charset.*;
import java.util.Arrays; import java.util.*;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
/** /**
@@ -31,17 +31,18 @@ public final class ByteArray implements ByteTuple {
} }
public ByteArray(int size) { public ByteArray(int size) {
content = new byte[Math.max(1, size)]; this.content = new byte[Math.max(1, size)];
} }
public ByteArray(ByteTuple tuple) { public ByteArray(ByteTuple tuple) {
content = tuple.content(); this.content = tuple.content();
count = tuple.length(); this.count = tuple.length();
} }
public ByteArray(byte[] bs) { public ByteArray(byte[] bs) {
content = bs; Objects.requireNonNull(bs);
count = 0; this.content = bs;
this.count = bs.length;
} }
/** /**

View File

@@ -0,0 +1,212 @@
/*
*
*/
package org.redkale.test.convert;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.junit.jupiter.api.*;
import org.redkale.annotation.Comment;
import org.redkale.convert.json.JsonConvert;
import org.redkale.util.TypeToken;
/**
*
* @author zhangjx
*/
public class JsonPvBeanTest {
public static void main(String[] args) throws Throwable {
JsonPvBeanTest test = new JsonPvBeanTest();
test.run();
}
@Test
public void run() throws Exception {
String json = "[\n"
+ " {\n"
+ " \"pagename\": \"首页\",\n"
+ " \"cate\": \"home_page\",\n"
+ " \"functions\": [\n"
+ " {\n"
+ " \"functionname\": \"茶室\",\n"
+ " \"type\": \"tea\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"桌游\",\n"
+ " \"type\": \"board\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"密室\",\n"
+ " \"type\": \"escape\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"剧本杀\",\n"
+ " \"type\": \"drama\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"PS5/switch\",\n"
+ " \"type\": \"ps5\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"电竞\",\n"
+ " \"type\": \"game\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"赛事\",\n"
+ " \"type\": \"match\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"预约\",\n"
+ " \"type\": \"book\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"充值\",\n"
+ " \"type\": \"charge\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"福利中心\",\n"
+ " \"type\": \"weal\"\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " {\n"
+ " \"pagename\": \"福利中心\",\n"
+ " \"cate\": \"weal_page\",\n"
+ " \"functions\": [\n"
+ " {\n"
+ " \"functionname\": \"卡券套餐\",\n"
+ " \"type\": \"card\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"优惠券\",\n"
+ " \"type\": \"coupon\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"充值\",\n"
+ " \"type\": \"charge\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"团购\",\n"
+ " \"type\": \"group\"\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " {\n"
+ " \"pagename\": \"地图找店\",\n"
+ " \"cate\": \"map_page\",\n"
+ " \"functions\": [\n"
+ " {\n"
+ " \"functionname\": \"搜索框\",\n"
+ " \"type\": \"search\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"店铺详情\",\n"
+ " \"type\": \"site\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"城市切换\",\n"
+ " \"type\": \"city\"\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " {\n"
+ " \"pagename\": \"房间服务\",\n"
+ " \"cate\": \"site_page\",\n"
+ " \"functions\": [\n"
+ " {\n"
+ " \"functionname\": \"切换门店\",\n"
+ " \"type\": \"venue\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"确认支付\",\n"
+ " \"type\": \"pay\"\n"
+ " }\n"
+ " ]\n"
+ " },\n"
+ " {\n"
+ " \"pagename\": \"个人中心\",\n"
+ " \"cate\": \"personal_page\",\n"
+ " \"functions\": [\n"
+ " {\n"
+ " \"functionname\": \"会员中心\",\n"
+ " \"type\": \"vip\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"余额\",\n"
+ " \"type\": \"amount\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"优惠券\",\n"
+ " \"type\": \"coupon\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"卡券套餐\",\n"
+ " \"type\": \"card\"\n"
+ " },\n"
+ " {\n"
+ " \"functionname\": \"积分\",\n"
+ " \"type\": \"exp\"\n"
+ " }\n"
+ " ]\n"
+ " }\n"
+ "]\n"
+ "";
List<JsonPvBean> list = null;
list = JsonConvert.root().convertFrom(new TypeToken<List<JsonPvBean>>() {
}.getType(), json);
Assertions.assertNotNull(list);
Assertions.assertEquals(5, list.size());
System.out.println("-----------------");
list = JsonConvert.root().convertFrom(new TypeToken<List<JsonPvBean>>() {
}.getType(), ByteBuffer.wrap(json.getBytes(StandardCharsets.UTF_8)));
Assertions.assertNotNull(list);
Assertions.assertEquals(5, list.size());
System.out.println("-----------------");
InputStream in = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8));
list = JsonConvert.root().convertFrom(new TypeToken<List<JsonPvBean>>() {
}.getType(), in);
Assertions.assertNotNull(list);
Assertions.assertEquals(5, list.size());
System.out.println(list);
}
public static class JsonPvBean {
@Comment("页面名称")
public String pagename;
@Comment("页面类别")
public String cate;
@Comment("页面类别")
public String code;
@Comment("页面功能点")
public List<Functions> functions;
@Override
public String toString() {
return "{\"pagename\":\"" + pagename + "\",\"cate\":\"" + cate + "\",\"code\":\"" + code + "\",\"functions\":" + functions + "}";
}
public static class Functions {
@Comment("功能名称")
public String functionname;
@Comment("功能类型")
public String type;
@Override
public String toString() {
return "{\"functionname\":\"" + functionname + "\",\"type\":\"" + type + "\"}";
}
}
}
}