jakartaee / jsonp-api

Jakarta JSON Processing
https://eclipse.org/ee4j/jsonp
Other
141 stars 61 forks source link

Add JsonArrayBuilder getJsonArrayBuilder()/JsonObjectBuilder getJsonObjectBuilder() to JsonArray/JsonObject #157

Closed julianomqs closed 5 years ago

julianomqs commented 5 years ago

As far as I know there is no way to modify a JsonArrayor JsonObjectafter reading it with JsonReader.

I would like to read some JSON from strings and change them:

JsonObject jsonObject = Json.createReader(new StringReader("{ \"name\": \"John\", \"age\": \"31\" }")).readObject();
JsonArray jsonArray = Json.createReader(new StringReader("[ \"a\", \"b\", \"c\" ]")).readArray();

But JsonArrayand JsonObjectare immutable, so I can't do that.

So my suggestion is to add two utilities methods to achieve that:

public interface JsonArray extends JsonStructure, List<JsonValue> {
    JsonArrayBuilder getJsonArrayBuilder();
}
public interface JsonObject extends JsonStructure, Map<String, JsonValue> {
    JsonObjectBuilder getJsonObjectBuilder();
}

With that two methods would be easier to write and read a JSON code and reducing the verbosity and workarounds.

leadpony commented 5 years ago

Hello @julianomqs. You can use the API JsonBuilderFactory#createArrayBuilder and JsonBuilderFactory#createObjectBuilder to modify the existing JSON array/object.

julianomqs commented 5 years ago

I'm really sorry, I forgot to mention I'm using Java EE 7.

I see in Java EE 8 those methods you mentioned are already present in Json class.

Closing this issue.

Thanks a lot.