GeezerVison is showing github.com — Allow builder object to be reused for DRY creation of Json · Issue #62 · javaee/json-processing-spec
This repository was archived by the owner on Aug 10, 2023. It is now read-only.
This repository was archived by the owner on Aug 10, 2023. It is now read-only.
Allow builder object to be reused for DRY creation of Json #62DescriptionThe current builder object is not reusable once build() is called the map is cleared
Want to be able to do something like:
//a base builder JsonObjectBuilder builder = Json.createObjectBuilder().
add("name", "Peter").
add("required", false).
add("unique", true);
boolean keepSettings = true;
JsonObject baseObj = builder.build(keepSettings);
//note: if you add a new keepSettings parameter to build method to optionally // retain the current elements in the current map we can we extend the base // properties and add some more to the internal map, keepSettings for build // tells it to not clear out the map (as it does now) so we can go on using the // same builder to construct with.
//now we can tweak the current builder by adding a new property or overriding one builder.add("addedField", "extra");
// and make a object from the current base and tweaked map of prop/vals. JsonObject builtOnObj = builder.build();
Current implementation doesn't allow for reuse of a built builder you'd have to create another object and copy / paste everything set that was set above
— OR —
Another option to implement this need would be to be able to construct a new builder by passing in an existing one so that the map values can be reused for example:
//start with a base object JsonObjectBuilder baseBuilder = Json.createObjectBuilder().
add("name", "Peter").
add("required", false).
add("unique", true);
//pass the base object into the constructor of a new builder object JsonObjectBuilder specialBuilder = Json.createObjectBuilder(*baseBuilder*).
add("addedMember", "addtionInfoOnBase").
add("moreStuff", "alsoThis");
// again pass on the starting elements for the map of this builder JsonObjectBuilder extraSpecialBuilder = Json.createObjectBuilder(specialBuilder).
add("supa", "awesome");
//then we can create the objects , without having to copy/paste all the prior settings // for each ones builder JsonObject baseObject = baseBuilder.build();
JsonObject specialObject = specialBuilder.build();
JsonObject extraSpecialObject = extraSpecialBuilder.build();
Activityglassfishrobot commented on Aug 26, 2014 ContributorAuthor More actions glassfishrobot commented on Aug 26, 2014 ContributorAuthor More actions glassfishrobot commented on Sep 2, 2014 ContributorAuthor More actions kchung said:
Thanks for the suggestion. However, I must say it does not agree with the basic builder pattern used by the current API. Extending the life span of a builder may lead to memory leaks, and sharing implementations in two builders may cause confusion.
Currently, there are some efforts to add editing operations (including appending to an array) to immutable JSON structures after they are built. The idea is similar to your 2nd suggesting. It goes like this.
JsonArray orig = Json.createJsonArrayBuilder(). ... .build();
// create a new builder based on the original array
JsonArrayBuilder ab = Json.createJsonArrayBuilder(orig);
// do some editing and get a new array
JsonArray new = ab.add(...).build();
Of course the operations on ab are not restricted to append, but can include insert, replace, and remove.
Of course this change can only happen in the next release of the API, so nothing has been committed. glassfishrobot commented on Apr 25, 2017 ContributorAuthor More actions This issue was imported from java.net JIRA JSON_PROCESSING_SPEC-62
MetadataMetadataDevelopmentNo branches or pull requestsIssue actions
|