Skip to content

Controllable Order of Json Properties #2

Closed

Description

@TimothyGillespie

Is your feature request related to a problem? Please describe.
There is no way to configure the order that objects return their attributes in JSON Response. This leads to unintuitive responses though they are functionally the same. The order depends on the attribute names and their alphanumerical value and length it seems.

Describe the solution you'd like
I would like the order to be the same order in which the attributes are defined.

Describe alternatives you've considered
Otherwise adding annotations could achieve more fine-grained control, but seems too much and to verbose of a solution.

Additional context
To see the effect you can try:

public class Nonsense {
    public int a;
    public String b;

    public Nonsense(int test, String test2) {
        this.a = test;
        this.b = test2;
    }
}

and then

public class Nonsense {
    public int c;
    public String b;

    public Nonsense(int test, String test2) {
        this.c = test;
        this.b = test2;
    }
}

with a controller like this:

public class RootController extends HttpController {

    @Inject
    public WebApplication webApplication;

    @Get("/")
    public Nonsense getRoot() {
        return new Nonsense(23, "HET");
    }

}

Activity

  1. x7airworker commented on Mar 2, 2021

    @x7airworker
    Member

    The order of the fields acquired by the reflection api isn't guaranteed, as seen here: https://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredFields--
    A solution would be an annotation, but I think this would rather be an enhancement for the AbstractData library.

  2. TimothyGillespie commented on Mar 2, 2021

    @TimothyGillespie
    Author

    I wasn't aware of this, but I could find this which seems to implement sorting by line number it seem, though I didn't dig much into this yet.

  3. x7airworker commented on Apr 3, 2021

    @x7airworker
    Member

    Any updates about the sort by line solution you mentioned?

  4. TimothyGillespie commented on Apr 3, 2021

    @TimothyGillespie
    Author

    Nope, haven't looked at it since I am prioritizing the ORM.

  5. JanHolger commented on Jun 14, 2021

    @JanHolger
    Member

    Unfortunately sorting by line number isn't that trivial as the Reflection API does not offer line numbers. You are probably refering to this answer on a stackoverflow question regarding this problem but this is reading and analyzing the bytecode from the .class file. This solution is pretty horrible performance and stability wise. It depends on the way the classloader accesses the classfiles which depends on the runtime environment. At the moment we are internally using gson for class mapping aswell as json parsing/dumping. We have plans to replace the class mapping part with our own solution but we intended to still use gson as json parser/dumper and i'm not sure if the json dumper respects order as per JSON specification properties should have no specific order. We should keep this issue open and look into it, when we build our own class mapping.

  6. changed the title [-]Controllable Order of Json Responses when returning classes[/-] [+]Controllable Order of Json Properties based on line numbers[/+] on Jun 14, 2021
  7. changed the title [-]Controllable Order of Json Properties based on line numbers[/-] [+]Controllable Order of Json Properties[/+] on Jun 14, 2021
  8. x7airworker commented on Jan 4, 2022

    @x7airworker
    Member

    I think this one is fixed, through the new order property of the MapperOptions annotation.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions