ICEfaces
  1. ICEfaces
  2. ICE-5831

Sparkle: New utility method for building parameter strings

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0-Alpha2
    • Fix Version/s: 2.0-Beta2, 2.0.0
    • Component/s: ICE-Components
    • Labels:
      None
    • Environment:
      ICEfaces 2.0, Sparkle component platform
    • Affects:
      Documentation (User Guide, Ref. Guide, etc.)

      Description

      Should have a utility API that builds the parameter strings, performs param escaping.

      - Yip suggests that the JSON "stringify" function would suffice, if we can implement one.
      - param builder API should not send parameters that have the default value
      - param builder API should not send parameters for disabled features
      - API should perform escaping for security and correctness

        Activity

        Hide
        Mark Collette added a comment - - edited

        http://www.json.org/

        After discussing the pros and cons of (A) creating a Map of parameters and serialising that to a JSON string; and (B) programmatically calling methods on a ResponseWriter-esque helper object to generate the JSON string; we've chosen (B). Here is the suggestion for how to implement that idea.

        Say we want to output a string that creates a javascript map, with a nested map, as well as String, integer and boolean values:

        {a:

        {b:true,c:'hi'}

        ,d:25}

        Re-written with more white-space, for greater clarity:

        {
        a:

        { b:true, c:'hi' }

        ,
        d:25
        }

        We could make use of a utility object, that provides an API similar to StringBuilder, but javascript variable oriented, instead of char oriented:

        JSONBuilder builder = new JSONBuilder();
        builder.beginMap().
        beginMap("a").
        entry("b", true).
        entry("c", "hi").
        endMap().
        entry("d", 25).
        endMap();
        String json = builder.toString();

        Or, slightly more succinctly:

        String json = JSONBuilder.create().
        beginMap().
        beginMap("a").
        entry("b", true).
        entry("c", "hi").
        endMap().
        entry("d", 25).
        endMap().toString();

        All the methods in JSONBuilder return the reference to itself, to simplify further operations on it. JSONBuilder.create() just makes a new instance. Every JSONBuilder internally uses a StringBuilder, whose toString() is returned by JSONBuilder.toString().

        beginMap() just appends: "{"
        beginMap(String key) appends: key + "

        {" endMap() appends: "}

        "

        There would be versions of entry(String key, X value) that take: int, long, float, double, boolean, String. They basically append: key + ":" + value. The String one adds quotes and does JSON string escaping, like how the link above describes.

        beginMap(String key) and entry(String key, X value) need to use some internal state for managing when to append a comma.

        Show
        Mark Collette added a comment - - edited http://www.json.org/ After discussing the pros and cons of (A) creating a Map of parameters and serialising that to a JSON string; and (B) programmatically calling methods on a ResponseWriter-esque helper object to generate the JSON string; we've chosen (B). Here is the suggestion for how to implement that idea. Say we want to output a string that creates a javascript map, with a nested map, as well as String, integer and boolean values: {a: {b:true,c:'hi'} ,d:25} Re-written with more white-space, for greater clarity: { a: { b:true, c:'hi' } , d:25 } We could make use of a utility object, that provides an API similar to StringBuilder, but javascript variable oriented, instead of char oriented: JSONBuilder builder = new JSONBuilder(); builder.beginMap(). beginMap("a"). entry("b", true). entry("c", "hi"). endMap(). entry("d", 25). endMap(); String json = builder.toString(); Or, slightly more succinctly: String json = JSONBuilder.create(). beginMap(). beginMap("a"). entry("b", true). entry("c", "hi"). endMap(). entry("d", 25). endMap().toString(); All the methods in JSONBuilder return the reference to itself, to simplify further operations on it. JSONBuilder.create() just makes a new instance. Every JSONBuilder internally uses a StringBuilder, whose toString() is returned by JSONBuilder.toString(). beginMap() just appends: "{" beginMap(String key) appends: key + " {" endMap() appends: "} " There would be versions of entry(String key, X value) that take: int, long, float, double, boolean, String. They basically append: key + ":" + value. The String one adds quotes and does JSON string escaping, like how the link above describes. beginMap(String key) and entry(String key, X value) need to use some internal state for managing when to append a comma.
        Hide
        Mark Collette added a comment -

        We should consider putting this class in glimmer, for us to use beyond sparkle, since glimmer core is hand-rolling similar code in places like org.icefaces.event.BridgeSetup.

        Show
        Mark Collette added a comment - We should consider putting this class in glimmer, for us to use beyond sparkle, since glimmer core is hand-rolling similar code in places like org.icefaces.event.BridgeSetup.
        Hide
        Mark Collette added a comment -

        In ICE-5830, Mircea describes a new utility class he added called ScriptWriter. In theory, we could augment that, or just use this in conjunction with that, for outputting our escaped javascript.

        Show
        Mark Collette added a comment - In ICE-5830 , Mircea describes a new utility class he added called ScriptWriter. In theory, we could augment that, or just use this in conjunction with that, for outputting our escaped javascript.
        Hide
        yip.ng added a comment - - edited

        Usage:

        import org.icefaces.component.utils.JSONBuilder;

        JSONBuilder.create().
        beginMap().
        entry("dateStr", dateStr).
        beginMap("hrMin").
        entry("selectedHour", selectedHour).
        entry("selectedMinute", selectedMinute).
        endMap().
        entry("hourField", hourField).
        beginMap("amPm").
        entry("amPmStr", amPmStr).
        entry("amStr", amPmStrings[0]).
        entry("pmStr", amPmStrings[1]).
        endMap().
        entry("renderAsPopup", renderAsPopup).
        entry("renderInputField", renderInputField).
        entry("singleSubmit", singleSubmit).
        entry("ariaEnabled", ariaEnabled).
        endMap().toString();

        Output:

        {"dateStr":"Jul\/07\/2010 01:41 PM","hrMin":

        {"selectedHour":"01","selectedMinute":"41"}

        ,"hourField":"HOUR1","amPm":

        {"amPmStr":"PM","amStr":"AM","pmStr":"PM"}

        ,"renderAsPopup":false,"renderInputField":false,"singleSubmit":false,"ariaEnabled":true}

        Requirements for default values and disabled features in separate JIRA: ICE-5847.

        Show
        yip.ng added a comment - - edited Usage: import org.icefaces.component.utils.JSONBuilder; JSONBuilder.create(). beginMap(). entry("dateStr", dateStr). beginMap("hrMin"). entry("selectedHour", selectedHour). entry("selectedMinute", selectedMinute). endMap(). entry("hourField", hourField). beginMap("amPm"). entry("amPmStr", amPmStr). entry("amStr", amPmStrings [0] ). entry("pmStr", amPmStrings [1] ). endMap(). entry("renderAsPopup", renderAsPopup). entry("renderInputField", renderInputField). entry("singleSubmit", singleSubmit). entry("ariaEnabled", ariaEnabled). endMap().toString(); Output: {"dateStr":"Jul\/07\/2010 01:41 PM","hrMin": {"selectedHour":"01","selectedMinute":"41"} ,"hourField":"HOUR1","amPm": {"amPmStr":"PM","amStr":"AM","pmStr":"PM"} ,"renderAsPopup":false,"renderInputField":false,"singleSubmit":false,"ariaEnabled":true} Requirements for default values and disabled features in separate JIRA: ICE-5847.
        Hide
        Ken Fyten added a comment -

        This class requires JavaDoc.

        Show
        Ken Fyten added a comment - This class requires JavaDoc.
        Hide
        yip.ng added a comment -

        Done. See screenshots 1 and 2.

        Show
        yip.ng added a comment - Done. See screenshots 1 and 2.

          People

          • Assignee:
            yip.ng
            Reporter:
            Ken Fyten
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: