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

Sequence of adding a JsonArrayBuilder into a JsonObjectBuilder interferes in the final result #6

Open
glassfishrobot opened this issue Oct 27, 2013 · 7 comments

Comments

@glassfishrobot
Copy link

While creating some Json content using the Object Model API, I realised that the sequence of adding a JsonArrayBuilder into a JsonObjectBuilder interferes in the final result. The following code won't include the categories in the resulting Json because the line, indicated with an arrow, adds the JsonArrayBuilder to the JsonObjectBuilder before JsonArrayBuilder is loaded with data :

List<CategoryMenu> categories = categoryMenuBean.findCategoriesByMenu(menu);
JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
JsonArrayBuilder arrayBuilderCategories = Json.createArrayBuilder();

objectBuilder.add("categories", arrayBuilderCategories); // <-- 
for(CategoryMenu category: categories) {
     arrayBuilderCategories.add(Json.createObjectBuilder()
 .add("id", category.getId())
 .add("name", category.getName()));
}

JsonObject model = objectBuilder.build();
StringWriter strWriter = new StringWriter();
try (JsonWriter jsonWriter = Json.createWriter(strWriter)) {
     jsonWriter.writeObject(model);
}

System.out.println(strWriter.toString()); // output = []

To work around with the problem, I had to move that highlighted line to after the iteration:

List<CategoryMenu> categories = categoryMenuBean.findCategoriesByMenu(menu);
JsonObjectBuilder objectBuilder = Json.createObjectBuilder();
JsonArrayBuilder arrayBuilderCategories = Json.createArrayBuilder();

for(CategoryMenu category: categories) {
     arrayBuilderCategories.add(Json.createObjectBuilder()
 .add("id", category.getId())
 .add("name", category.getName()));
}

objectBuilder.add("categories", arrayBuilderCategories); // <-- 
JsonObject model = objectBuilder.build();
StringWriter strWriter = new StringWriter();
try (JsonWriter jsonWriter = Json.createWriter(strWriter)) {
     jsonWriter.writeObject(model);
}
System.out.println(strWriter.toString()); // output = {"categories":[{"id":1,"name":"Plat du Jour"},{"id":2,"name":"Permanent"}]}

Apparently, the build is performed right away, while data is added to those builder objects. In fact, it should be built only when the method build() is finally invoked. In both examples, it always happen when all data is added to the builders and it is time to build the final JsonObject model. Therefore, both examples above should work normally.

Environment

JDK 1.7, Glassfish 3.1.2.2, Jersey 1.8

Affected Versions

[1.0.3]

@glassfishrobot
Copy link
Author

@glassfishrobot Commented
Reported by hbeto

@glassfishrobot
Copy link
Author

@glassfishrobot Commented
This issue was imported from java.net JIRA JSONP-24

@glassfishrobot
Copy link
Author

@keilw Commented
Any idea why this is not closed with 1.1 Final? Was it overlooked or is it still unsolved?

@glassfishrobot
Copy link
Author

@m0mus Commented
@keilw We need to check, possibly it has been solved already.

@glassfishrobot
Copy link
Author

@keilw Commented
Sure, but why the 1.1.2 Milestone, does that mean anything that's still relevant won't be done before a MR2?

@glassfishrobot
Copy link
Author

@tjquinno
Copy link

Same question for add(String, JsonObjectBuilder).

Ideally, regardless of any change (or not) in the implementation, the JavaDoc for these methods should be explicit about when the sub-builder's build() is invoked: when it is added to its parent or when the parent builder is built.

@lukasj lukasj transferred this issue from jakartaee/jsonp-api Jun 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants