Repository: alibaba/fastjson2
Pull request #3251 (2025-01-02 01:41:21)

refactor & fix testcase
### What this PR does / why we need it?



### Summary of your change



#### Please indicate you've done the following:

- [ ] Made sure tests are passing and test coverage is added if needed.
- [ ] Made sure commit message follow the rule of [Conventional Commits specification](https://www.conventionalcommits.org/).
- [ ] Considered the docs impact and opened a new docs issue or PR with docs changes if needed.



Patch:
diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONWriter.java b/core/src/main/java/com/alibaba/fastjson2/JSONWriter.java
index e0606f3dc..67d24fd0f 100644
--- a/core/src/main/java/com/alibaba/fastjson2/JSONWriter.java
+++ b/core/src/main/java/com/alibaba/fastjson2/JSONWriter.java
@@ -1616,47 +1616,11 @@ public abstract class JSONWriter
 
     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;
@@ -1667,10 +1631,6 @@ public abstract class JSONWriter
             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);
@@ -2756,4 +2716,8 @@ public abstract class JSONWriter
     public void setAttachment(Object attachment) {
         this.attachment = attachment;
     }
+
+    protected final void overflowLevel() {
+        throw new JSONException("level too large : " + level);
+    }
 }
diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java b/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java
index 6e41769c8..7045af4b3 100644
--- a/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java
+++ b/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java
@@ -2658,21 +2658,6 @@ final class JSONWriterJSONB
         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);
diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java b/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java
index a8add297e..9224f6d76 100644
--- a/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java
+++ b/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF16.java
@@ -130,11 +130,10 @@ class JSONWriterUTF16
 
     @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;
@@ -190,11 +189,10 @@ class JSONWriterUTF16
 
     @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;
@@ -1533,7 +1531,7 @@ class JSONWriterUTF16
     @Override
     public final void writeRaw(char ch) {
         if (off == chars.length) {
-            ensureCapacity(off + 1);
+            grow0(off + 1);
         }
         chars[off++] = ch;
     }
@@ -1996,12 +1994,17 @@ class JSONWriterUTF16
 
     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) {
@@ -2941,7 +2944,7 @@ class JSONWriterUTF16
     }
 
     @Override
-    public final void write(JSONObject map) {
+    public final void write(Map<?, ?> map) {
         if (pretty != PRETTY_NON) {
             super.write(map);
             return;
@@ -2958,10 +2961,7 @@ class JSONWriterUTF16
             return;
         }
 
-        if (off == chars.length) {
-            grow(off + 1);
-        }
-        chars[off++] = '{';
+        writeRaw('{');
 
         boolean first = true;
         for (Map.Entry entry : map.entrySet()) {
@@ -2971,10 +2971,7 @@ class JSONWriterUTF16
             }
 
             if (!first) {
-                if (off == chars.length) {
-                    grow(off + 1);
-                }
-                chars[off++] = ',';
+                writeRaw(',');
             }
 
             first = false;
@@ -2985,10 +2982,7 @@ class JSONWriterUTF16
                 writeAny(key);
             }
 
-            if (off == chars.length) {
-                grow(off + 1);
-            }
-            chars[off++] = ':';
+            writeRaw(':');
 
             if (value == null) {
                 writeNull();
@@ -3035,7 +3029,7 @@ class JSONWriterUTF16
             objectWriter.write(this, value, null, null, 0);
         }
 
-        endObject();
+        writeRaw('}');
     }
 
     @Override
diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java b/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java
index dc0e848ea..8c0b5c02f 100644
--- a/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java
+++ b/core/src/main/java/com/alibaba/fastjson2/JSONWriterUTF8.java
@@ -233,11 +233,10 @@ class JSONWriterUTF8
 
     @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;
@@ -290,11 +289,10 @@ class JSONWriterUTF8
 
     @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;
@@ -2930,7 +2928,7 @@ class JSONWriterUTF8
     }
 
     @Override
-    public final void write(JSONObject map) {
+    public final void write(Map<?, ?> map) {
         if (pretty != PRETTY_NON) {
             super.write(map);
             return;
diff --git a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderCreatorASM.java b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderCreatorASM.java
index 8d93714d3..d0ef4fed6 100644
--- a/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderCreatorASM.java
+++ b/core/src/main/java/com/alibaba/fastjson2/reader/ObjectReaderCreatorASM.java
@@ -265,7 +265,7 @@ public class ObjectReaderCreatorASM
                 }
 
                 Class fieldClass = fieldReader.fieldClass;
-                if (!Modifier.isPublic(fieldClass.getModifiers())) {
+                if (fieldClass != null && !Modifier.isPublic(fieldClass.getModifiers())) {
                     match = false;
                     break;
                 }
diff --git a/core/src/main/java/com/alibaba/fastjson2/writer/FieldWriter.java b/core/src/main/java/com/alibaba/fastjson2/writer/FieldWriter.java
index 36d48201d..3fb9ebdaa 100644
--- a/core/src/main/java/com/alibaba/fastjson2/writer/FieldWriter.java
+++ b/core/src/main/java/com/alibaba/fastjson2/writer/FieldWriter.java
@@ -483,6 +483,14 @@ public abstract class FieldWriter<T>
             return 1;
         }
 
+        if (this.method != null && other.method == null) {
+            return -1;
+        }
+
+        if (this.method == null && other.method != null) {
+            return 1;
+        }
+
         return nameCompare;
     }
 


Test patch:
diff --git a/core/src/test/java/com/alibaba/fastjson2/issues_1500/Issue1543_1544.java b/core/src/test/java/com/alibaba/fastjson2/issues_1500/Issue1543_1544.java
index 149142788..69bb24789 100644
--- a/core/src/test/java/com/alibaba/fastjson2/issues_1500/Issue1543_1544.java
+++ b/core/src/test/java/com/alibaba/fastjson2/issues_1500/Issue1543_1544.java
@@ -19,7 +19,7 @@ public class Issue1543_1544 {
             JSON.toJSONString(map);
         } catch (Exception e) {
             assertTrue(e instanceof JSONException);
-            assertEquals("level too large : 2048", e.getMessage());
+            assertEquals("level too large : 2049", e.getMessage());
         }
     }
 
@@ -30,8 +30,9 @@ public class Issue1543_1544 {
         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());
         }
     }
 }