Skip to content
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 #62

@glassfishrobot

Description

@glassfishrobot

The 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();

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions