GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

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 #62

Copy link
Copy link
Open
Open
Allow builder object to be reused for DRY creation of Json#62
Copy link
Assignees
glassfishrobot
Labels
ERR: AssigneePriority: MajorType: Improvementbuilderjson

Description

@glassfishrobot
glassfishrobot
opened on Aug 26, 2014
Issue body actions

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

Activity

  1. glassfishrobot commented on Aug 26, 2014

    @glassfishrobot
    glassfishrobot
    on Aug 26, 2014
    ContributorAuthor
    More actions

    Reported by tirams

  2. glassfishrobot commented on Aug 26, 2014

    @glassfishrobot
    glassfishrobot
    on Aug 26, 2014
    ContributorAuthor
    More actions

    Was assigned to jitu

  3. glassfishrobot commented on Sep 2, 2014

    @glassfishrobot
    glassfishrobot
    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.

  4. glassfishrobot commented on Apr 25, 2017

    @glassfishrobot
    glassfishrobot
    on Apr 25, 2017
    ContributorAuthor
    More actions

    This issue was imported from java.net JIRA JSON_PROCESSING_SPEC-62

  5. glassfishrobot
    added
    Type: Improvement
    Priority: Major
    builder
    json
    ERR: Assignee
    on Apr 25, 2017
  6. glassfishrobot
    self-assigned this
    on Apr 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @glassfishrobot
    glassfishrobot

Labels

ERR: AssigneePriority: MajorType: Improvementbuilderjson

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402503 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.