Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor & fix testcase #3251

Merged
merged 2 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 7 additions & 43 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -1616,47 +1616,11 @@ public abstract void writeDateTimeISO8601(

public abstract void write(List array);

public void write(Map map) {
if (map == null) {
this.writeNull();
return;
}

if (map.isEmpty()) {
writeRaw('{', '}');
return;
}

final long NONE_DIRECT_FEATURES = ReferenceDetection.mask
| PrettyFormat.mask
| NotWriteEmptyArray.mask
| NotWriteDefaultValue.mask;

if ((context.features & NONE_DIRECT_FEATURES) != 0) {
ObjectWriter objectWriter = context.getObjectWriter(map.getClass());
objectWriter.write(this, map, null, null, 0);
return;
}

write0('{');
boolean first = true;
for (Map.Entry o : (Iterable<Map.Entry>) map.entrySet()) {
if (!first) {
write0(',');
}

writeAny(
o.getKey());
write0(':');
writeAny(
o.getValue());

first = false;
}
write0('}');
public final void write(JSONObject map) {
write((Map) map);
}

public void write(JSONObject map) {
public void write(Map<?, ?> map) {
if (map == null) {
this.writeNull();
return;
Expand All @@ -1667,10 +1631,6 @@ public void write(JSONObject map) {
return;
}

final long NONE_DIRECT_FEATURES = ReferenceDetection.mask
| NotWriteEmptyArray.mask
| NotWriteDefaultValue.mask;

if ((context.features & NONE_DIRECT_FEATURES) != 0) {
ObjectWriter objectWriter = context.getObjectWriter(map.getClass());
objectWriter.write(this, map, null, null, 0);
Expand Down Expand Up @@ -2756,4 +2716,8 @@ public Object getAttachment() {
public void setAttachment(Object attachment) {
this.attachment = attachment;
}

protected final void overflowLevel() {
throw new JSONException("level too large : " + level);
}
}
15 changes: 0 additions & 15 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java
Original file line number Diff line number Diff line change
Expand Up @@ -2658,21 +2658,6 @@ public void write(Map map) {
endObject();
}

@Override
public void write(JSONObject object) {
if (object == null) {
writeNull();
return;
}

startObject();
for (Map.Entry entry : object.entrySet()) {
writeAny(entry.getKey());
writeAny(entry.getValue());
}
endObject();
}

@Override
public byte[] getBytes() {
return Arrays.copyOf(bytes, off);
Expand Down
40 changes: 17 additions & 23 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,10 @@ public final void writeColon() {

@Override
public final void startObject() {
if (level >= context.maxLevel) {
throw new JSONException("level too large : " + level);
if (++level > context.maxLevel) {
overflowLevel();
}

level++;
startObject = true;

int off = this.off;
Expand Down Expand Up @@ -190,11 +189,10 @@ public final void writeComma() {

@Override
public final void startArray() {
if (level >= context.maxLevel) {
throw new JSONException("level too large : " + level);
if (++level > context.maxLevel) {
overflowLevel();
}

level++;
int off = this.off;
int minCapacity = off + 3 + pretty * level;
char[] chars = this.chars;
Expand Down Expand Up @@ -1533,7 +1531,7 @@ public final void writeChar(char ch) {
@Override
public final void writeRaw(char ch) {
if (off == chars.length) {
ensureCapacity(off + 1);
grow0(off + 1);
}
chars[off++] = ch;
}
Expand Down Expand Up @@ -1996,12 +1994,17 @@ public final void writeNameRaw(char[] name, int coff, int len) {

final void ensureCapacity(int minCapacity) {
if (minCapacity > chars.length) {
grow(minCapacity);
grow0(minCapacity);
}
}

private char[] grow(int minCapacity) {
return chars = Arrays.copyOf(chars, newCapacity(minCapacity, chars.length));
grow0(minCapacity);
return chars;
}

private void grow0(int minCapacity) {
chars = Arrays.copyOf(chars, newCapacity(minCapacity, chars.length));
}

public final void writeInt32(int[] value) {
Expand Down Expand Up @@ -2941,7 +2944,7 @@ public final void writeRaw(byte[] bytes) {
}

@Override
public final void write(JSONObject map) {
public final void write(Map<?, ?> map) {
if (pretty != PRETTY_NON) {
super.write(map);
return;
Expand All @@ -2958,10 +2961,7 @@ public final void write(JSONObject map) {
return;
}

if (off == chars.length) {
grow(off + 1);
}
chars[off++] = '{';
writeRaw('{');

boolean first = true;
for (Map.Entry entry : map.entrySet()) {
Expand All @@ -2971,10 +2971,7 @@ public final void write(JSONObject map) {
}

if (!first) {
if (off == chars.length) {
grow(off + 1);
}
chars[off++] = ',';
writeRaw(',');
}

first = false;
Expand All @@ -2985,10 +2982,7 @@ public final void write(JSONObject map) {
writeAny(key);
}

if (off == chars.length) {
grow(off + 1);
}
chars[off++] = ':';
writeRaw(':');

if (value == null) {
writeNull();
Expand Down Expand Up @@ -3035,7 +3029,7 @@ public final void write(JSONObject map) {
objectWriter.write(this, value, null, null, 0);
}

endObject();
writeRaw('}');
}

@Override
Expand Down
12 changes: 5 additions & 7 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,10 @@ public final void writeColon() {

@Override
public final void startObject() {
if (level >= context.maxLevel) {
throw new JSONException("level too large : " + level);
if (++level > context.maxLevel) {
overflowLevel();
}

level++;
startObject = true;

int off = this.off;
Expand Down Expand Up @@ -290,11 +289,10 @@ public final void writeComma() {

@Override
public final void startArray() {
if (level >= context.maxLevel) {
throw new JSONException("level too large : " + level);
if (++level > context.maxLevel) {
overflowLevel();
}

level++;
int off = this.off;
int minCapacity = off + 3 + pretty * level;
byte[] bytes = this.bytes;
Expand Down Expand Up @@ -2930,7 +2928,7 @@ public final void writeNameRaw(char[] bytes, int offset, int len) {
}

@Override
public final void write(JSONObject map) {
public final void write(Map<?, ?> map) {
if (pretty != PRETTY_NON) {
super.write(map);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public <T> ObjectReader<T> createObjectReader(
}

Class fieldClass = fieldReader.fieldClass;
if (!Modifier.isPublic(fieldClass.getModifiers())) {
if (fieldClass != null && !Modifier.isPublic(fieldClass.getModifiers())) {
match = false;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,14 @@ public int compareTo(Object o) {
return 1;
}

if (this.method != null && other.method == null) {
return -1;
}

if (this.method == null && other.method != null) {
return 1;
}

return nameCompare;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void testMap() {
JSON.toJSONString(map);
} catch (Exception e) {
assertTrue(e instanceof JSONException);
assertEquals("level too large : 2048", e.getMessage());
assertEquals("level too large : 2049", e.getMessage());
}
}

Expand All @@ -30,8 +30,9 @@ public void testList() {
try {
JSON.toJSONString(list);
} catch (Exception e) {
e.printStackTrace();
assertTrue(e instanceof JSONException);
assertEquals("level too large : 2048", e.getMessage());
assertEquals("level too large : 2049", e.getMessage());
}
}
}
Loading