Skip to content
Open
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
21 changes: 14 additions & 7 deletions core/src/main/java/com/taobao/arthas/core/view/ObjectView.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,20 @@ protected void setDefaultValue(List<FieldWriter> fieldWriters, Class objectClass
});

public static String toJsonString(Object object) {
JSONWriter.Context context = new JSONWriter.Context(JSON_OBJECT_WRITER_PROVIDER);
context.setMaxLevel(4097);
context.config(JSONWriter.Feature.IgnoreErrorGetter,
JSONWriter.Feature.ReferenceDetection,
JSONWriter.Feature.IgnoreNonFieldGetter,
JSONWriter.Feature.WriteNonStringKeyAsString);
return JSON.toJSONString(object, context);
try {
JSONWriter.Context context = new JSONWriter.Context(JSON_OBJECT_WRITER_PROVIDER);
context.setMaxLevel(4097);
context.config(JSONWriter.Feature.IgnoreErrorGetter,
JSONWriter.Feature.ReferenceDetection,
JSONWriter.Feature.IgnoreNonFieldGetter,
JSONWriter.Feature.WriteNonStringKeyAsString);
return JSON.toJSONString(object, context);
} catch (StackOverflowError e) {
logger.error("ObjectView JSON serialization stackoverflow, object class: {}", object == null ? "null" : object.getClass(), e);
return "ERROR DATA!!! object class: " + (object == null ? "null" : object.getClass())
+ ", StackOverflowError: circular reference or object graph too deep. "
+ "Try disabling json mode: options json false";
}
}

private final Object object;
Expand Down
23 changes: 23 additions & 0 deletions core/src/test/java/com/taobao/arthas/core/view/ObjectViewTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,27 @@ public static JsonFormatSingleton getInstance() {
return INSTANCE;
}
}

@Test
public void testJsonModeCircularReference() {
GlobalOptions.isUsingJson = true;
try {
CircularRef a = new CircularRef("a");
CircularRef b = new CircularRef("b");
a.ref = b;
b.ref = a;
ObjectView objectView = new ObjectView(a, 3);
String result = objectView.draw();
// Should not throw StackOverflowError
Assert.assertNotNull(result);
} finally {
GlobalOptions.isUsingJson = false;
}
}

private static class CircularRef {
String name;
CircularRef ref;
CircularRef(String name) { this.name = name; }
}
}
Loading