Hi Jitu,
> one has to walk down the tree and build those objects from the associated
builders
well, that's the principle of builders, isn't it? You should not build
anything until you actually call the build() method - and in case pointed
by Hildeberto jsonp is building / fixing stuff way before calling build()
method.
Either the specification or implementation is buggy, IMHO.
@Hildeberto - I would suggest you to open a bug report in JIRA for this.
This is definitely a bug, unless proven that it's not by Jitu (spec lead).
@Jitu - can you point out exact specification paragraph where it is defined
that you have to initialize jsonp builders in a specific order?
Cheers,
Przemyslaw
On Thu, Oct 24, 2013 at 9:18 PM, Jitendra Kotamraju <
jitendra.kotamraju_at_oracle.com> wrote:
> On 10/23/13 1:18 PM, Hildeberto Mendonça wrote:
>
> Hello,
>
> I'm creating some Json content using the Object Model API and I realized
> that the sequence of creating JsonObjectBuilder and JsonArrayBuilder
> actually interferes in the final result. The following code won't include
> the categories in the resulting Json:
>
> 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 fix the problem, I have to move the 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"}]}
>
> In my humble opinion it should work in both cases because it gives more
> flexibility to the programmer. Is it supposed to work like that or is it an
> issue?
>
> I don't consider it as an issue. What you are suggesting is a lazy
> construction of JsonObject/JsonArray from the associated builders when the
> final build happens. This may be ok for one level, but if the constructed
> JsonObject is big tree, then one has to walk down the tree and build those
> objects from the associated builders.
>
> Jitu
>
>
> Regards,
>
> Hildeberto
>
>
>