You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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");
}
}
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.
changed the title [-]Controllable Order of Json Responses when returning classes[/-][+]Controllable Order of Json Properties based on line numbers[/+]on Jun 14, 2021
Activity
x7airworker commentedon Mar 2, 2021
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.
TimothyGillespie commentedon Mar 2, 2021
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.
x7airworker commentedon Apr 3, 2021
Any updates about the sort by line solution you mentioned?
TimothyGillespie commentedon Apr 3, 2021
Nope, haven't looked at it since I am prioritizing the ORM.
JanHolger commentedon Jun 14, 2021
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.
[-]Controllable Order of Json Responses when returning classes[/-][+]Controllable Order of Json Properties based on line numbers[/+][-]Controllable Order of Json Properties based on line numbers[/-][+]Controllable Order of Json Properties[/+]x7airworker commentedon Jan 4, 2022
I think this one is fixed, through the new
orderproperty of theMapperOptionsannotation.