From 016ee632c118e1fb61f2516d40c6817cbed8334d Mon Sep 17 00:00:00 2001 From: redkale Date: Tue, 17 Sep 2024 16:33:03 +0800 Subject: [PATCH] json --- src/main/java/org/redkale/convert/Reader.java | 4 - .../org/redkale/convert/bson/BsonReader.java | 1 - .../convert/json/JsonByteBufferReader.java | 13 +-- .../org/redkale/convert/json/JsonReader.java | 92 ++++++++++--------- .../org/redkale/test/convert/Json5Test.java | 22 ++--- 5 files changed, 59 insertions(+), 73 deletions(-) diff --git a/src/main/java/org/redkale/convert/Reader.java b/src/main/java/org/redkale/convert/Reader.java index 4353306e7..88ce8925e 100644 --- a/src/main/java/org/redkale/convert/Reader.java +++ b/src/main/java/org/redkale/convert/Reader.java @@ -20,9 +20,6 @@ public abstract class Reader { MAP; } - // 当前对象字段名的游标 - protected int fieldIndex; - public static final short SIGN_NULL = -1; public static final short SIGN_NOLENGTH = -2; @@ -93,7 +90,6 @@ public abstract class Reader { * @return 返回字段数 */ public String readObjectB(final Class clazz) { - this.fieldIndex = 0; return null; } diff --git a/src/main/java/org/redkale/convert/bson/BsonReader.java b/src/main/java/org/redkale/convert/bson/BsonReader.java index 660c1fe46..1d5b72497 100644 --- a/src/main/java/org/redkale/convert/bson/BsonReader.java +++ b/src/main/java/org/redkale/convert/bson/BsonReader.java @@ -138,7 +138,6 @@ public class BsonReader extends Reader { @Override public final String readObjectB(final Class clazz) { - this.fieldIndex = 0; // 必须要重置为0 final String newcls = readClassName(); if (newcls != null && !newcls.isEmpty()) { return newcls; diff --git a/src/main/java/org/redkale/convert/json/JsonByteBufferReader.java b/src/main/java/org/redkale/convert/json/JsonByteBufferReader.java index b35c5e02a..a6740cf60 100644 --- a/src/main/java/org/redkale/convert/json/JsonByteBufferReader.java +++ b/src/main/java/org/redkale/convert/json/JsonByteBufferReader.java @@ -164,7 +164,6 @@ public class JsonByteBufferReader extends JsonReader { */ @Override public final String readObjectB(final Class clazz) { - this.fieldIndex = 0; // 必须要重置为0 char ch = nextGoodChar(true); if (ch == '{') { return ""; @@ -175,17 +174,7 @@ public class JsonByteBufferReader extends JsonReader { if (ch == 'N' && nextChar() == 'U' && nextChar() == 'L' && nextChar() == 'L') { return null; } - int pos = this.position; - StringBuilder sb = new StringBuilder(); - sb.append(ch); - char one; - try { - while ((one = nextChar()) != 0) sb.append(one); - } catch (Exception e) { - // do nothing - } - throw new ConvertException( - "a json object text must begin with '{' (position = " + pos + ") but '" + ch + "' in (" + sb + ")"); + throw new ConvertException("a json object must begin with '{' (position = " + position + ") but '" + ch + "'"); } /** diff --git a/src/main/java/org/redkale/convert/json/JsonReader.java b/src/main/java/org/redkale/convert/json/JsonReader.java index cb0f9cfee..3593f2374 100644 --- a/src/main/java/org/redkale/convert/json/JsonReader.java +++ b/src/main/java/org/redkale/convert/json/JsonReader.java @@ -175,41 +175,45 @@ public class JsonReader extends Reader { * @return 有效字符 */ protected char nextGoodChar(boolean allowComment) { - char c; - for (; ; ) { - c = nextChar(); - if (c == 0) { - return c; // 0 表示buffer结尾了 - } + char c = 0; + char[] text0 = this.text; + int end = this.limit; + int curr = ++this.position; + for (; curr <= end; curr++) { + c = text0[curr]; if (c > ' ') { - if (allowComment && c == '/') { // 支持单行和多行注释 - char n = nextChar(); - if (n == '/') { - for (; ; ) { - if (nextChar() == '\n') { + if (c == '/' && allowComment) { // 支持单行和多行注释 + char n = text0[++curr]; + if (n == '/') { // 单行注释 + for (++curr; curr <= end; curr++) { + if (text0[curr] == '\n') { break; } } + this.position = curr; return nextGoodChar(allowComment); - } else if (n == '*') { + } else if (n == '*') { // 多行注释 char nc; char lc = 0; - for (; ; ) { - nc = nextChar(); + for (++curr; curr <= end; curr++) { + nc = text0[curr]; if (nc == '/' && lc == '*') { break; } lc = nc; } + this.position = curr; return nextGoodChar(allowComment); } else { - throw new ConvertException("illegal escape(" + n + ") (position = " + this.position + ") in '" - + new String(text) + "'"); + throw new ConvertException( + "illegal escape(" + n + ") (position = " + curr + ") in " + new String(text)); } } - return c; + break; } } + this.position = curr; + return c; } /** @@ -266,7 +270,6 @@ public class JsonReader extends Reader { */ @Override public String readObjectB(final Class clazz) { - this.fieldIndex = 0; // 必须要重置为0 if (this.text.length == 0) { return null; } @@ -280,7 +283,7 @@ public class JsonReader extends Reader { if (ch == 'N' && text[++position] == 'U' && text[++position] == 'L' && text[++position] == 'L') { return null; } - throw new ConvertException("a json object text must begin with '{' (position = " + position + ") but '" + ch + throw new ConvertException("a json object must begin with '{' (position = " + position + ") but '" + ch + "' in " + new String(this.text)); } @@ -350,8 +353,8 @@ public class JsonReader extends Reader { if (ch == ':') { return; } - throw new ConvertException("'" + new String(text) + "'expected a ':' but '" + ch + "'(position = " + position - + ") in (" + new String(this.text) + ")"); + throw new ConvertException( + "expected a ':' but '" + ch + "'(position = " + position + ") in " + new String(this.text)); } @Override @@ -477,13 +480,14 @@ public class JsonReader extends Reader { } return negative ? Integer.MIN_VALUE : Integer.MAX_VALUE; } + int curr = ++this.position; + int end = this.limit; + char[] text0 = this.text; + char ch; boolean hex = false; boolean dot = false; - for (; ; ) { - char ch = nextChar(); - if (ch == 0) { - break; - } + for (; curr <= end; curr++) { + ch = text0[curr]; if (ch >= '0' && ch <= '9') { if (dot) { continue; @@ -520,12 +524,13 @@ public class JsonReader extends Reader { } else if (ch == '.') { dot = true; } else if (ch == ',' || ch == '}' || ch == ']' || ch <= ' ' || ch == ':') { - backChar(ch); + curr--; break; } else { throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")"); } } + this.position = curr; return negative ? -value : value; } @@ -612,13 +617,14 @@ public class JsonReader extends Reader { } return negative ? Long.MIN_VALUE : Long.MAX_VALUE; } + int curr = ++this.position; + int end = this.limit; + char[] text0 = this.text; + char ch; boolean hex = false; boolean dot = false; - for (; ; ) { - char ch = nextChar(); - if (ch == 0) { - break; - } + for (; curr <= end; curr++) { + ch = text0[curr]; if (ch >= '0' && ch <= '9') { if (dot) { continue; @@ -655,12 +661,13 @@ public class JsonReader extends Reader { } else if (ch == '.') { dot = true; } else if (ch == ',' || ch == '}' || ch == ']' || ch <= ' ' || ch == ':') { - backChar(ch); + curr--; break; } else { throw new ConvertException("illegal escape(" + ch + ") (position = " + position + ")"); } } + this.position = curr; return negative ? -value : value; } @@ -677,37 +684,32 @@ public class JsonReader extends Reader { DeMemberNode node = memberInfo.getMemberNode(); char ch = nextGoodChar(true); // 需要跳过注释 final char[] text0 = this.text; - int currpos = this.position; + int curr = this.position; if (ch == '"' || ch == '\'') { final char quote = ch; for (; ; ) { - ch = text0[++currpos]; + ch = text0[++curr]; if (ch == quote) { break; } node = node == null ? null : node.getNode(ch); } - this.position = currpos; + this.position = curr; return node == null ? null : node.getValue(); } else { node = node == null ? null : node.getNode(ch); - int start = currpos; for (; ; ) { - if (currpos == eof) { + if (curr == eof) { break; } - ch = text0[++currpos]; + ch = text0[++curr]; if (ch == ',' || ch == ']' || ch == '}' || ch <= ' ' || ch == ':') { + curr--; break; } node = node == null ? null : node.getNode(ch); } - int len = currpos - start; - if (len < 1) { - this.position = currpos; - } else { - this.position = currpos - 1; - } + this.position = curr; return node == null ? null : node.getValue(); } } diff --git a/src/test/java/org/redkale/test/convert/Json5Test.java b/src/test/java/org/redkale/test/convert/Json5Test.java index cc7c2ad88..a5e93e264 100644 --- a/src/test/java/org/redkale/test/convert/Json5Test.java +++ b/src/test/java/org/redkale/test/convert/Json5Test.java @@ -22,15 +22,6 @@ public class Json5Test { @Test public void run1() throws Exception { - JsonFactory factory = JsonFactory.root().withFeatures(Convert.FEATURE_TINY | Convert.FEATURE_NULLABLE); - final JsonConvert convert = factory.getConvert(); - Json5Bean bean = new Json5Bean(); - bean.id = 60; - System.out.println(convert.convertTo(bean)); - } - - @Test - public void run2() throws Exception { JsonConvert convert = JsonConvert.root(); Json5Bean bean = new Json5Bean(); bean.id = 500; @@ -55,11 +46,20 @@ public class Json5Test { System.out.println(Arrays.toString(ints1)); } + @Test + public void run2() throws Exception { + JsonFactory factory = JsonFactory.root().withFeatures(Convert.FEATURE_TINY | Convert.FEATURE_NULLABLE); + final JsonConvert convert = factory.getConvert(); + Json5Bean bean = new Json5Bean(); + bean.id = 60; + System.out.println(convert.convertTo(bean)); + } + public static class Json5Bean { public int id; - public int idx; + public long idx; public float decmails; @@ -74,7 +74,7 @@ public class Json5Test { public int hashCode() { int hash = 7; hash = 47 * hash + this.id; - hash = 47 * hash + this.idx; + hash = 47 * hash + (int) this.idx; hash = 47 * hash + Float.floatToIntBits(this.decmails); hash = 47 * hash + (int) (this.value ^ (this.value >>> 32)); hash = 47 * hash + Objects.hashCode(this.name);