Skip to content

Section 4.2 is unclear - #371

Merged
KyleAure merged 2 commits into
jakartaee:mainfrom
njr-11:section-4-2-is-unclear
Sep 17, 2026
Merged

KyleAure merged 2 commits into
jakartaee:mainfrom
njr-11:section-4-2-is-unclear

Conversation

@njr-11

@njr-11 njr-11 commented Apr 28, 2025 •

Copy link
Copy Markdown
Member

While answering a question from a user, I read through section 4.2 on Customizing Property Order and JsonbPropertyOrder Javadoc regarding whether original property names vs customized property names are used in determining the order.

Section 4.2 is unclear because in one place it defers to JsonbPropertyOrder,

To customize the order of serialized properties only for one specific type, JSON Binding provides jakarta.json.bind.annotation.JsonbPropertyOrder annotation. Order specified by JsonbPropertyOrder annotation overrides order specified by PropertyOrderStrategy.

but immediately after that states that The order is applied to already renamed properties as stated in 4.1, which would contradict JsonbPropertyOrder's statement that Names must correspond to original names defined in Java class before any customization applied.

After rereading these seemingly conflicting statements several times over, I finally realized that the line at the end of section 4.2 isn't meant to apply to JsonbPropertyOrder that was discussed immediately before it. Instead, it is intended only for PropertyOrderStrategy.

The PR adds clarification to that line of section 4.2 (first commit). A second commit adds a few grammar corrections that I noticed in that same section. If anyone doesn't want the grammar corrections part, just let me know and I'll remove that commit.

@njr-11 njr-11 added the documentation Involves the spec or javadocs label Apr 28, 2025
@njr-11 njr-11 added this to the 3.1 milestone Apr 28, 2025
@sean-leichtle

Copy link
Copy Markdown

I'm quite new to Jsonb, so apologies in advance if I've misunderstood something, but annotating a record with @JsonbPropertyOrder doesn't seem to work unless the individual fields in the record are annotated with @JsonbProperty.

That is, the following is deserialized correctly in the order specified by @JsonbPropertyOrder:

@JsonbPropertyOrder({
        "getCommand",
        "getUserName",
})
public record NetworkMessage(
        @JsonbProperty("getCommand") Command getCommand,
        @JsonbProperty("getUserName") String getUserName
) {
    @JsonbCreator
    public NetworkMessage { }
}

The following is not correctly deserialized:

@JsonbPropertyOrder({
        "getCommand",
        "getUserName",
})
public record NetworkMessage(
        Command getCommand,
        String getUserName
) {
    @JsonbCreator
    public NetworkMessage { }
}

I realize records are a special case, but it seems either the end of 4.2 should be amended to read A PropertyOrderStrategy as well as a PropertyOrder used to annotate a record determines the order based on the already renamed properties that have been customized according to the rules outlined under section 4.1. or the JsonbPropertyOrderstatement should be changed to Except in the case of records, names must correspond to original names defined in Java class before any customization applied.

@KyleAure

KyleAure commented May 26, 2026 •

Copy link
Copy Markdown
Member

@sean-leichtle

I am a bit confused by your proposed changes for how records should behave vs classes.
I believe thus far we have tried to make it so that records and classes perform the same in all the same use cases.

Example case

So for example, if we configure jsonb to order fields / record components in lexicographical order:

JsonbConfig config = new JsonbConfig()
        .withPropertyOrderStrategy(PropertyOrderStrategy.LEXICOGRAPHICAL);
Jsonb jsonb = JsonbBuilder.create(config);

And have a class that wants to use a different order like:

@JsonbPropertyOrder({
  "username",
  "password",
  "active" //uses field name
})
public class User {
  public String username;
  public String password;

  @JsonbProperty("zzzActive")
  public boolean active;
}

Without @JsonbPropertyOrder the order should be password, username, zzzActive (uses final name)
with @JsonbPropertyOrder the order should be username, password, zzzActive

Therefore, I would expect the following to be object to be serialized:

User admin = new User();
admin.username = "admin";
admin.password = "p@ssw0rd";
admin.active = true;

String json = jsonb.toJson(admin);

to:

{
  "username": "admin",
  "password": "p@ssw0rd",
  "zzzActive": true
}

And then de serialized back to the original.

The equivalent for a record should be:

@JsonbPropertyOrder({
  "username",
  "password",
  "active"
})
public record User ( String username, String password, @JsonbProperty("zzzActive") boolean active) {
}

and should perform the same.

Clarification

What happens when you attempt to de serialize

@JsonbPropertyOrder({
        "getCommand",
        "getUserName",
})
public record NetworkMessage(
        Command getCommand,
        String getUserName
) {
    @JsonbCreator
    public NetworkMessage { }
}

What is not working?
What provider are you using?

@KyleAure KyleAure left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been looking deeper into PropertyOrderStategy and @JsonbPropertyOrder annotation for records support and while it is weird that one uses the renamed property name and the other uses the original property name that is how it has been documented and tested in previous releases. Therefore, the clarification in this PR is accurate and we will likely follow suite when it comes to records support.

I think in a future release we could update the @JsonbPropertyOrder annotation to use renamed properties instead if the community would like that change.
As such I have added #190 to Milestone 4.0 for consideration next release.

@KyleAure
KyleAure merged commit 0d05acd into jakartaee:main Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Involves the spec or javadocs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants