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

Consider detecting cycles when constructing JSONObjects #632

Closed
cushon opened this issue Oct 13, 2021 · 7 comments
Closed

Consider detecting cycles when constructing JSONObjects #632

cushon opened this issue Oct 13, 2021 · 7 comments

Comments

@cushon
Copy link
Contributor

cushon commented Oct 13, 2021

JSONObject.populateMap mentions that "the bean can not be recursive", and attempting to pass a recursive bean to JSONObject results in a StackOverflowError. The StackOverflowError can be hard to debug, because the stack trace is usually truncated before the frames that show where new JSONObject(bean) was called.

It might be worth detecting cycles when constructing JSONObjects to report a better error message for beans containing cycles.

import org.json.JSONObject;

class Json {

  public static void main(String[] args) {
    new JSONObject(new Foo());
  }

  public static class Foo {

    public Bar getBar() {
      return new Bar(this);
    }
  }

  public static class Bar {

    private final Foo foo;

    Bar(Foo foo) {
      this.foo = foo;
    }

    public Foo getFoo() {
      return foo;
    }
  }
}
Exception in thread "main" java.lang.StackOverflowError
	at java.base/java.lang.reflect.Method.getRoot(Method.java:68)
	at java.base/java.lang.reflect.Executable.declaredAnnotations(Executable.java:600)
	at java.base/java.lang.reflect.Executable.getAnnotation(Executable.java:572)
	at java.base/java.lang.reflect.Method.getAnnotation(Method.java:695)
	at java.base/java.lang.reflect.AnnotatedElement.isAnnotationPresent(AnnotatedElement.java:274)
	at java.base/java.lang.reflect.AccessibleObject.isAnnotationPresent(AccessibleObject.java:525)
	at org.json.JSONObject.getAnnotationDepth(JSONObject.java:1674)
	at org.json.JSONObject.getKeyNameFromMethod(JSONObject.java:1568)
	at org.json.JSONObject.populateMap(JSONObject.java:1538)
	at org.json.JSONObject.<init>(JSONObject.java:365)
	at org.json.JSONObject.wrap(JSONObject.java:2475)
	at org.json.JSONObject.populateMap(JSONObject.java:1543)
	at org.json.JSONObject.<init>(JSONObject.java:365)
	at org.json.JSONObject.wrap(JSONObject.java:2475)
	at org.json.JSONObject.populateMap(JSONObject.java:1543)
	at org.json.JSONObject.<init>(JSONObject.java:365)
	at org.json.JSONObject.wrap(JSONObject.java:2475)
	at org.json.JSONObject.populateMap(JSONObject.java:1543)
	at org.json.JSONObject.<init>(JSONObject.java:365)
	at org.json.JSONObject.wrap(JSONObject.java:2475)
	at org.json.JSONObject.populateMap(JSONObject.java:1543)
	at org.json.JSONObject.<init>(JSONObject.java:365)
	at org.json.JSONObject.wrap(JSONObject.java:2475)
	at org.json.JSONObject.populateMap(JSONObject.java:1543)
	at org.json.JSONObject.<init>(JSONObject.java:365)
	at org.json.JSONObject.wrap(JSONObject.java:2475)
	at org.json.JSONObject.populateMap(JSONObject.java:1543)
	at org.json.JSONObject.<init>(JSONObject.java:365)
	at org.json.JSONObject.wrap(JSONObject.java:2475)
	at org.json.JSONObject.populateMap(JSONObject.java:1543)
@stleary
Copy link
Owner

stleary commented Oct 14, 2021

No objections if anyone would like to try to implement this.

@guptnis
Copy link

guptnis commented Oct 14, 2021

Hi @stleary I would like to try to implement this.

@stleary
Copy link
Owner

stleary commented Oct 14, 2021

Sure @guptnis, please go ahead

guptnis pushed a commit to guptnis/JSON-java that referenced this issue Oct 14, 2021
guptnis pushed a commit to guptnis/JSON-java that referenced this issue Oct 14, 2021
guptnis pushed a commit to guptnis/JSON-java that referenced this issue Oct 14, 2021
@stleary
Copy link
Owner

stleary commented Oct 14, 2021

See #633

guptnis pushed a commit to guptnis/JSON-java that referenced this issue Oct 14, 2021
guptnis pushed a commit to guptnis/JSON-java that referenced this issue Oct 14, 2021
guptnis pushed a commit to guptnis/JSON-java that referenced this issue Oct 14, 2021
@gdmanandamohon
Copy link

Hi, @stleary I was just wondering what is the depth fo the bean? if it's multilevel parent-child relationship or just single level.
I am thinking of solving this problem with DFS traversal therefore wondering how is the depth level.

To clarify more, this is providing the 1st level functions of the class
Method[] methods = includeSuperClass ? klass.getMethods() : klass.getDeclaredMethods();

do we have to consider the child level of the methods? I am asking because through the populateMap function I assume you have just arranged the bean in the first level. Could you please clarify? Thanks!

@stleary
Copy link
Owner

stleary commented Oct 16, 2021

@gdmanandamohon another developer is working on this issue, let's give him a chance to finish his work.

@stleary
Copy link
Owner

stleary commented Nov 18, 2021

Closing this issue since a solution has been accepted. See #645

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants