optimization for codeSize

This commit is contained in:
shaojin.wensj 2024-02-17 01:13:07 +08:00
parent 7a116ebe83
commit c051606030
10 changed files with 2967 additions and 5371 deletions

View File

@ -3644,7 +3644,7 @@ public interface JSON {
try (JSONReader jsonReader = JSONReader.of(chars)) {
jsonReader.skipValue();
return jsonReader.isEnd();
return jsonReader.isEnd() && !jsonReader.comma;
} catch (JSONException error) {
return false;
}
@ -3666,7 +3666,7 @@ public interface JSON {
return false;
}
jsonReader.skipValue();
return jsonReader.isEnd();
return jsonReader.isEnd() && !jsonReader.comma;
} catch (JSONException error) {
return false;
}
@ -3688,7 +3688,7 @@ public interface JSON {
return false;
}
jsonReader.skipValue();
return jsonReader.isEnd();
return jsonReader.isEnd() && !jsonReader.comma;
} catch (JSONException error) {
return false;
}
@ -3710,7 +3710,7 @@ public interface JSON {
return false;
}
jsonReader.skipValue();
return jsonReader.isEnd();
return jsonReader.isEnd() && !jsonReader.comma;
} catch (JSONException error) {
return false;
}
@ -3729,8 +3729,8 @@ public interface JSON {
try (JSONReader jsonReader = JSONReader.of(bytes)) {
jsonReader.skipValue();
return jsonReader.isEnd();
} catch (JSONException error) {
return jsonReader.isEnd() && !jsonReader.comma;
} catch (JSONException | ArrayIndexOutOfBoundsException error) {
return false;
}
}
@ -3766,7 +3766,7 @@ public interface JSON {
return false;
}
jsonReader.skipValue();
return jsonReader.isEnd();
return jsonReader.isEnd() && !jsonReader.comma;
} catch (JSONException error) {
return false;
}
@ -3788,7 +3788,7 @@ public interface JSON {
try (JSONReader jsonReader = JSONReader.of(bytes, offset, length, charset)) {
jsonReader.skipValue();
return jsonReader.isEnd();
return jsonReader.isEnd() && !jsonReader.comma;
} catch (JSONException error) {
return false;
}

View File

@ -1371,21 +1371,6 @@ public abstract class JSONReader
protected abstract ZonedDateTime readZonedDateTimeX(int len);
public void readNumber(ValueConsumer consumer, boolean quoted) {
readNumber0();
Number number = getNumber();
consumer.accept(number);
}
public void readString(ValueConsumer consumer, boolean quoted) {
String str = readString(); //
if (quoted) {
consumer.accept(JSON.toJSONString(str));
} else {
consumer.accept(str);
}
}
protected abstract void readNumber0();
public abstract String readString();
@ -1896,7 +1881,7 @@ public abstract class JSONReader
public abstract void skipComment();
public final Boolean readBool() {
public Boolean readBool() {
if (nextIfNull()) {
return null;
}
@ -3789,6 +3774,30 @@ public abstract class JSONReader
this.ch = (char) savePoint.current;
}
final JSONException notSupportName() {
return new JSONException(info("not support unquoted name"));
}
final JSONException valueError() {
return new JSONException(info("illegal value"));
}
final JSONException error(int offset, int ch) {
throw new JSONException("error, offset " + offset + ", char " + (char) ch);
}
static JSONException syntaxError(int ch) {
return new JSONException("syntax error, expect ',', but '" + (char) ch + "'");
}
static JSONException syntaxError(int offset, int ch) {
throw new JSONException("syntax error, offset " + offset + ", char " + (char) ch);
}
static JSONException numberError(int offset, int ch) {
throw new JSONException("illegal number, offset " + offset + ", char " + (char) ch);
}
public final String info() {
return info(null);
}
@ -3800,7 +3809,7 @@ public abstract class JSONReader
return message + ", offset " + offset;
}
static boolean isFirstIdentifier(char ch) {
static boolean isFirstIdentifier(int ch) {
return (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || ch == '_' || ch == '$' || (ch >= '0' && ch <= '9') || ch > 0x7F;
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -1,37 +0,0 @@
package com.alibaba.fastjson2.reader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
public interface ValueConsumer {
default void accept(byte[] bytes, int off, int len) {
accept(new String(bytes, off, len, StandardCharsets.UTF_8));
}
default void acceptNull() {
}
default void accept(boolean val) {
}
default void accept(int val) {
accept(Integer.valueOf(val));
}
default void accept(long val) {
accept(Long.valueOf(val));
}
default void accept(Number val) {
}
default void accept(String val) {
}
default void accept(Map object) {
}
default void accept(List array) {
}
}

View File

@ -1148,7 +1148,6 @@ public class JSONReaderTest1 {
for (JSONReader jsonReader : TestUtils.createJSONReaders4("'',")) {
assertNull(jsonReader.readInt32());
assertTrue(jsonReader.isEnd());
assertTrue(jsonReader.comma);
}
}
@ -1309,7 +1308,6 @@ public class JSONReaderTest1 {
for (JSONReader jsonReader : TestUtils.createJSONReaders4("'',")) {
assertNull(jsonReader.readInt64());
assertTrue(jsonReader.isEnd());
assertTrue(jsonReader.comma);
}
}

View File

@ -4,6 +4,8 @@ import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.annotation.JSONType;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
@ -39,9 +41,27 @@ public class Issue1488 {
dog.dogName = "dog1001";
String text = JSON.toJSONString(dog);
Dog dog1 = (Dog) JSON.parseObject(text, Animal.class);
assertNotNull(dog1);
assertEquals(1, dog1.aniType);
byte[] utf8 = text.getBytes(StandardCharsets.UTF_8);
{
Dog dog1 = (Dog) JSON.parseObject(text.toCharArray(), Animal.class);
assertNotNull(dog1);
assertEquals(1, dog1.aniType);
}
{
Dog dog1 = (Dog) JSON.parseObject(utf8, Animal.class);
assertNotNull(dog1);
assertEquals(1, dog1.aniType);
}
{
Dog dog1 = (Dog) JSON.parseObject(utf8, 0, utf8.length, StandardCharsets.ISO_8859_1, Animal.class);
assertNotNull(dog1);
assertEquals(1, dog1.aniType);
}
{
Dog dog1 = (Dog) JSON.parseObject(text, Animal.class);
assertNotNull(dog1);
assertEquals(1, dog1.aniType);
}
}
@Test
@ -50,8 +70,26 @@ public class Issue1488 {
cat.catName = "cat1001";
String text = JSON.toJSONString(cat);
Cat cat1 = (Cat) JSON.parseObject(text, Animal.class);
assertNotNull(cat1);
assertEquals(2, cat1.aniType);
byte[] utf8 = text.getBytes(StandardCharsets.UTF_8);
{
Cat cat1 = (Cat) JSON.parseObject(text.toCharArray(), Animal.class);
assertNotNull(cat1);
assertEquals(2, cat1.aniType);
}
{
Cat cat1 = (Cat) JSON.parseObject(utf8, Animal.class);
assertNotNull(cat1);
assertEquals(2, cat1.aniType);
}
{
Cat cat1 = (Cat) JSON.parseObject(utf8, 0, utf8.length, StandardCharsets.ISO_8859_1, Animal.class);
assertNotNull(cat1);
assertEquals(2, cat1.aniType);
}
{
Cat cat1 = (Cat) JSON.parseObject(text, Animal.class);
assertNotNull(cat1);
assertEquals(2, cat1.aniType);
}
}
}

View File

@ -5,8 +5,6 @@ import com.alibaba.fastjson2.JSONReader;
import lombok.*;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
@ -44,19 +42,6 @@ public class Issue2164 {
assertNull(jsonRootBean.High);
}
@Test
public void testBytes() {
String json = "{\r\n"
+ " \"Ref\": \"\",\r\n"
+ " \"Width\": \"1.01\",\r\n"
+ " \"High\": \"\"\r\n"
+ "}";
TestVO jsonRootBean = JSON.parseObject(json.getBytes(StandardCharsets.UTF_8), TestVO.class, FASTJSON_DEFAULT_READER_FEATURES);
assertEquals("", jsonRootBean.Ref);
assertEquals(1.01, jsonRootBean.Width);
assertNull(jsonRootBean.High);
}
@Test
public void testWhiteSpaceComma() {
String json = "{\r\n"