Skip to main content

[json-processing-spec issues] [JIRA] Closed: (JSON_PROCESSING_SPEC-36) JSON builder improvements

  • From: "jitu (JIRA)" < >
  • To:
  • Subject: [json-processing-spec issues] [JIRA] Closed: (JSON_PROCESSING_SPEC-36) JSON builder improvements
  • Date: Mon, 10 Dec 2012 20:30:00 +0000 (GMT+00:00)
  • Auto-submitted: auto-generated


     [ 
http://java.net/jira/browse/JSON_PROCESSING_SPEC-36?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

jitu closed JSON_PROCESSING_SPEC-36.
------------------------------------

      Assignee: jitu
    Resolution: Fixed

Incorporating the suggested proposal.

> JSON builder improvements
> -------------------------
>
>                 Key: JSON_PROCESSING_SPEC-36
>                 URL: http://java.net/jira/browse/JSON_PROCESSING_SPEC-36
>             Project: json-processing-spec
>          Issue Type: Improvement
>    Affects Versions: 1.0-pr
>            Reporter: jitu
>            Assignee: jitu
>             Fix For: 1.0-pr
>
>
> Feedback from Mark Reinhold
> --------------------------------------
> The EDR javadoc for JsonObject contains this builder-pattern example:
> {code}
>     JsonObject object = new JsonBuilder()
>         .beginObject()
>             .add("firstName", "John")
>             .add("lastName", "Smith")
>             .add("age", 25)
>             .beginObject("address")
>                 .add("streetAddress", "21 2nd Street")
>                 .add("city", "New York")
>                 .add("state", "NY")
>                 .add("postalCode", "10021")
>             .endObject()
>             .beginArray("phoneNumber")
>                 .beginObject()
>                     .add("type", "home")
>                     .add("number", "212 555-1234")
>                 .endObject()
>                 .beginObject()
>                     .add("type", "home")
>                     .add("number", "646 555-4567")
>                 .endObject()
>             .endArray()
>         .endObject()
>     .build();
> {code}
> One problem with this style is that the structure of the object being
> constructed is not reflected in the syntax of the expression.  A reader
> must visually scan for "begin" and "end" methods in order to understand
> the resulting object structure, or even just to indent the code.
> Here's a variant of the above example written against a (hypothetical)
> alternative API:
> {code}
>     JsonObject value = new JsonObjectBuilder()
>         .add("firstName", "John")
>         .add("lastName", "Smith")
>         .add("age", 25)
>         .add("address", new JsonObjectBuilder()
>              .add("streetAddress", "21 2nd Street")
>              .add("city", "New York")
>              .add("state", "NY")
>              .add("postalCode", "10021"))
>         .add("phoneNumber", new JsonArrayBuilder()
>              .add(new JsonObjectBuilder()
>                   .add("type", "home")
>                   .add("number", "212 555-1234"))
>              .add(new JsonObjectBuilder()
>                   .add("type", "home")
>                   .add("number", "646 555-4567")))
>         .build();
> {code}
> Note how the structure of the resulting object is obvious from the
> structure of the expression, which (by the way) is indented correctly
> (at least by Emacs) without any manual effort at all.  You also don't
> need the "end" methods.
> Have you considered a design that would support this style?  I think
> it's much more readable.
> --------------------------

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://java.net/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


[json-processing-spec issues] [JIRA] Created: (JSON_PROCESSING_SPEC-36) JSON builder improvements

jitu (JIRA) 12/07/2012

[json-processing-spec issues] [JIRA] Updated: (JSON_PROCESSING_SPEC-36) JSON builder improvements

jitu (JIRA) 12/07/2012

[json-processing-spec issues] [JIRA] Updated: (JSON_PROCESSING_SPEC-36) JSON builder improvements

jitu (JIRA) 12/07/2012

[json-processing-spec issues] [JIRA] Closed: (JSON_PROCESSING_SPEC-36) JSON builder improvements

jitu (JIRA) 12/10/2012
 
 
Close
loading
Please Confirm
Close