function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PiranPiran 

Post an Idea, but it removed my formatting

I posted an idea to JSON tools on VF page but even after previewing correctly, the code in angle brackets got mangled.

 

Can I correct and/or delete the Idea? There seems to be no way.

 

The correct sample of code should look like this:

 

<json: object>
  <json:array name="aList">
    <apex:repeat value="{!myList}" var="v">
      <json:value value="{!v} />
    </apex:repeat>
  </json:array>
  <json:value name="name">{!FirstName} {!LastName}</json:value>
</json: object>

XactiumBenXactiumBen

What's wrong with making this up yourself?  Sure, apex:repeat can't get you what you want because you need comma's in the correct places, but the Apex Controller could construct the JSON string and pass that back to the page.

 

Could you perhaps think up a use case where this would be useful.  At the minute I don't see it.

PiranPiran
Yes, it could be generated by the controller. However:

- it keeps the JSON formatting in the same place as the javascript code that calls it
- it's shorter (my psuedo code is more than twice as long as my brief example)
- it's more flexible
- the syntax is easier for the page (in the same way that generating HTML is easier in visualforce pages than in Apex)
- it puts the display choices (e.g., how to present someone's name) on the visualforce page
- there isn't a EncodingUtils.jsEncode

So here's my equivalent pseudo code:

public String getJsonString() {
return
'{'+
    'aList: ' + joinJsonEncode(getMyList()) +
    ',' +
    'name: "' + EncodingUtils.jsEncode(getFirstName() + ' ' + getLastName()) + '"' +
    // no comma here
'}';
}

private String joinJsonEncode(List<String> a) {
    String out = '';
    Boolean first = true;
    for (String o : a) {
        if (first) {
            first = false;
        } else {
            out = out + ',';
        }
        out = out + '"' + EncodingUtils.jsEncode(o) + '"';
    }
    return '[' + out + ']';
}

<script>
var j = {!HTMLENCODE(jsonString)};
</script>

So, it's possible, but easier. (Hence why it's contributed as an idea).
XactiumBenXactiumBen

Some good points.  Sure, it's shorter for the user if these tags were available but it would also be shorter if there were apex functions and classes that would create a json object and all you had to do was:

 

obj = new JSONObject();

obj.addAttribute('aList', getMyList());

obj.addAttribute('name', getFirstName() + ' ' + getLastName());

return obj.toJSONString();

I even think the GoogleViz class may do something similar to this... (Google Visualizations Code Share project).  You may even be able to build your own JSON Helper class to do this too and just keep reusing that! I also think the tag suggestion would cause more problems - What if you added an apex outputPanel inside the repeat that wrapped around the json tags?  You'd have something like:

 

{

<span>

aList:

[

...

]

</span>

}

 

 

Just so you know, you don't need an EncodingUtil.JSEncode apex method.  You could encode the JSON String on the page side - {!JSENCODE(JsonString)}.  This should escape all the bad characters in the string. :)

Message Edited by XactiumBen on 09-15-2009 03:22 PM