You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@johnzon.apache.org by "Reinhard Sandtner (JIRA)" <ji...@apache.org> on 2018/10/25 10:41:00 UTC
[jira] [Commented] (JOHNZON-193) Johnzon is keeping the reference
for map/values from JsonObjectBuilder.
[ https://issues.apache.org/jira/browse/JOHNZON-193?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16663565#comment-16663565 ]
Reinhard Sandtner commented on JOHNZON-193:
-------------------------------------------
[~danielsoro] imo this is correct according the java doc of object builder
{code:java}
/**
* Add the given String value to the JsonObject to be created.
* If a value with that name already exists it will be replaced by the new value.
* @param name the JSON attribute name
* @param value the String value to add
* @return the current JsonObjectBuilder
*/
{code}
it is not specified if the {{JsonObjectBuilder}} can be reused or not and johnzon doesn't clear the builder so you can not reuse it.
for your usecase the following code should work
{code:java}
JsonArrayBuilder builder = Json.createArrayBuilder();
IntStream.range(1, 10)
.forEach(i -> builder.add(Json.createObjectBuilder().add("key", String.format("Key %d", i)).add("value", String.format("Value %d", i))));
{code}
> Johnzon is keeping the reference for map/values from JsonObjectBuilder.
> -----------------------------------------------------------------------
>
> Key: JOHNZON-193
> URL: https://issues.apache.org/jira/browse/JOHNZON-193
> Project: Johnzon
> Issue Type: Bug
> Reporter: Daniel Cunha
> Assignee: Reinhard Sandtner
> Priority: Major
>
> {color:#333333}If you try the follow code:{color}
>
> {code:java}
> final JsonObjectBuilder jsonObjectBuilder = Json.createObjectBuilder();
> final JsonArrayBuilder jsonArrayBuilder = Json.createArrayBuilder();
> IntStream.range(1, 10).forEach(numer -> {
> jsonObjectBuilder.add("key", String.format("Key %d", numer));
> jsonObjectBuilder.add("value", String.format("Value %d", numer));
> jsonArrayBuilder.add(jsonObjectBuilder);
> });
> final String message = jsonArrayBuilder.build().toString();{code}
> {color:#333333}it is retuning:{color}
>
> {code:java}
> [{"key":"Key 9","value":"Value 9"},{"key":"Key 9","value":"Value 9"},{"key":"Key 9","value":"Value 9"},{"key":"Key 9","value":"Value 9"},{"key":"Key 9","value":"Value 9"},{"key":"Key 9","value":"Value 9"},{"key":"Key 9","value":"Value 9"},{"key":"Key 9","value":"Value 9"},{"key":"Key 9","value":"Value 9"}]{code}
> {color:#333333} But we expect to see:{color}
> {code:java}
> [{"key":"Key 1","value":"Value 1"},{"key":"Key 2","value":"Value 2"},{"key":"Key 3","value":"Value 3"},{"key":"Key 4","value":"Value 4"},{"key":"Key 5","value":"Value 5"},{"key":"Key 6","value":"Value 6"},{"key":"Key 7","value":"Value 7"},{"key":"Key 8","value":"Value 8"},{"key":"Key 9","value":"Value 9"}]{code}
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)