From c22b0fd4dfe6d4d55124ebc55c977e949c8e4191 Mon Sep 17 00:00:00 2001 From: "shaojin.wensj" Date: Wed, 17 Apr 2024 05:59:54 +0800 Subject: [PATCH] improved compatible for 1.x, for issue #2450 --- .../com/alibaba/fastjson2/JSONWriter.java | 39 ++++++++++++++++ .../alibaba/fastjson2/JSONWriterJSONB.java | 4 ++ .../fastjson/serializer/JSONSerializer.java | 19 ++++++++ .../fastjson/serializer/SerializeWriter.java | 4 ++ .../alibaba/fastjson/v2issues/Issue2450.java | 46 +++++++++++++++++++ 5 files changed, 112 insertions(+) create mode 100644 fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2450.java diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONWriter.java b/core/src/main/java/com/alibaba/fastjson2/JSONWriter.java index 08fbd0cece..d87fcfc4d9 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONWriter.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONWriter.java @@ -2466,4 +2466,43 @@ public String toString() { protected static IllegalArgumentException illegalYear(int year) { return new IllegalArgumentException("Only 4 digits numbers are supported. Provided: " + year); } + + /** + * @deprecated + */ + public final void incrementIndent() { + indent++; + } + + /** + * @deprecated + */ + public final void decrementIdent() { + indent--; + } + + /** + * @deprecated + */ + public void println() { + writeChar('\n'); + for (int i = 0; i < indent; ++i) { + writeChar('\t'); + } + } + + /** + * @deprecated + * @param object + */ + public final void writeReference(Object object) { + if (refs == null) { + return; + } + + Path path = refs.get(object); + if (path != null) { + writeReference(path.toString()); + } + } } diff --git a/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java b/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java index 2244ebcd1b..f151afe29d 100644 --- a/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java +++ b/core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java @@ -2507,4 +2507,8 @@ public String toString() { return JSONB.typeName(bytes[0]) + ", bytes length " + off; } } + + @Override + public void println() { + } } diff --git a/fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/JSONSerializer.java b/fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/JSONSerializer.java index 0d9c98eb4f..ee97c5cc7f 100644 --- a/fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/JSONSerializer.java +++ b/fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/JSONSerializer.java @@ -171,4 +171,23 @@ public SerialContext getContext() { public void setContext(SerialContext context) { this.context = context; } + + public final boolean containsReference(Object value) { + return out.raw.containsReference(value); + } + + public final void writeReference(Object object) { + out.raw.writeReference(object); + } + + public final void incrementIndent() { + out.raw.incrementIndent(); + } + + public final void decrementIdent() { + out.raw.decrementIdent(); + } + + public void println() { + } } diff --git a/fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/SerializeWriter.java b/fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/SerializeWriter.java index 2677c12da8..d8a541ef1a 100644 --- a/fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/SerializeWriter.java +++ b/fastjson1-compatible/src/main/java/com/alibaba/fastjson/serializer/SerializeWriter.java @@ -247,4 +247,8 @@ public SerializeWriter append(char c) { raw.writeRaw(c); return this; } + + public JSONWriter getJSONWriter() { + return raw; + } } diff --git a/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2450.java b/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2450.java new file mode 100644 index 0000000000..0b97286641 --- /dev/null +++ b/fastjson1-compatible/src/test/java/com/alibaba/fastjson/v2issues/Issue2450.java @@ -0,0 +1,46 @@ +package com.alibaba.fastjson.v2issues; + +import com.alibaba.fastjson.serializer.JSONSerializer; +import com.alibaba.fastjson.serializer.SerializeConfig; +import com.alibaba.fastjson2.JSONWriter; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +public class Issue2450 { + @Test + public void test() { + Bean bean = new Bean(); + bean.self = bean; + + JSONSerializer serializer = new JSONSerializer(SerializeConfig.global); + assertFalse(serializer.containsReference(bean)); + + serializer.incrementIndent(); + serializer.decrementIdent(); + + serializer.write(bean); + + assertEquals("{\"name\":\"\",\"self\":{\"$ref\":\"..\"}}", serializer.toString()); + serializer.println(); + serializer.writeReference(bean); + } + + @Test + public void test1() { + Bean bean = new Bean(); + bean.self = bean; + + JSONSerializer serializer = new JSONSerializer(SerializeConfig.global); + JSONWriter jsonWriter = serializer.out.getJSONWriter(); + jsonWriter.setPath("$", bean); + jsonWriter.writeReference(bean); + assertEquals("{\"$ref\":\"$\"}", serializer.toString()); + } + + public class Bean { + public String name; + + public Bean self; + } +}