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

improved compatible for 1.x, for issue #2450 #2453

Merged
merged 1 commit into from
Apr 16, 2024
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
39 changes: 39 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
4 changes: 4 additions & 0 deletions core/src/main/java/com/alibaba/fastjson2/JSONWriterJSONB.java
Original file line number Diff line number Diff line change
Expand Up @@ -2507,4 +2507,8 @@ public String toString() {
return JSONB.typeName(bytes[0]) + ", bytes length " + off;
}
}

@Override
public void println() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,8 @@ public SerializeWriter append(char c) {
raw.writeRaw(c);
return this;
}

public JSONWriter getJSONWriter() {
return raw;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading