branaway / Japid

A Java-based statically-typed fast template engine that can be used in any Java code. It has special adapter for use with the Play! Framework.
113 stars 18 forks source link

[optimization] add util method to escape/encode special charaters when redering JSON #26

Open tunggad opened 13 years ago

tunggad commented 13 years ago

I have a template that renders JSON like following:

`import com.mongodb.DBObject;

`(  String msg, List<DBObject> dtdList )

{ "statusMsg" : "<span style="color: #ff0000;">${msg}</span>", // style=\"color: #ff0000;\" also does not work
  "dtdList" : "`tag _dtdList(dtdList)`" }

this JSON String is returned for a AJAX request to partial-updade two fragment of the page. The problem is, when this JSON String arrives at client side, jQuery can't parse/evaluate it to a proper JavaScript JSON object, because the values within "..." cantain special charaters (", \, etc.).

What i want to recommend is to add a util method for direct usage AND an option when invoking a tag which escape the special characters or HTML-encode the content so that the JSON String can be properly evaluated at client side.

As workaround for meanwhile i can write a util method by myself which does this job and give raw strings which i want to being HTML-encoded. But i will stuck, if i want to use template, because the render method will be called and it write the contents direct into stringbuilder, i dont get a chance to do some pre-processing before it gets written.

BTW: I was always doing this style of AJAX partical-update, when multiple page-fragments should be updated per AJAX. With Grails, i dont know, what for auto-magic the framework does underneath, but it always worked properly and i must not call escapse/encode explicitely. May be, because Grails has its own JSON builder, which handles all this job internally.

branaway commented 13 years ago

Can you use single quote for the style attribute? Can you show me what the acceptable raw string you'd like to see?

branaway commented 13 years ago

BTW, all method in the JavaExtension class is available in the news. It has a method named escapeJavaScript(). See if that is what you want.

{ 
"statusMsg" : "${escapeJavaScript("<span style=\"color: #ff0000;\">$[msg]$</span>")},   
}
tunggad commented 13 years ago

Hi Bing, you have right, normally, replacing all quotes by single quotes would be sufficient, if the content does not contain futher special character. But sometime you render a piece of content which is generated by lib like the following:

{ "statusMsg" : "<span style='color: #ff0000;'>successful saved</span>", "dtdList" : "    
<ul>
          <li>
      <h5 class="hideshowDTD {index:1}">playshop.commons.null</h5>
      <p id="json_1" class="hide">{ "_id" : { "$oid" : "4e009f816e25de6c4b22b4c6"} , "package" : "playshop.commons" ,      "class" : "General"}</p>
      <div class="align_right">
        <a href="#" class="loadDTD {index:1}">load</a> |
        <a href="#" class="hideshowDTD {index:1}">hide/show</a>
      </div>
    </li>
          <li>
      <h5 class="hideshowDTD {index:2}">playshop.commons.null</h5>
      <p id="json_2" class="hide">{ "_id" : { "$oid" : "4dff0f94214504366fa79660"} , "package" : "playshop.commons" ,  "class" : "Meta"}</p>
      <div class="align_right">
        <a href="#" class="loadDTD {index:2}">load</a> |
        <a href="#" class="hideshowDTD {index:2}">hide/show</a>
      </div>
    </li>
      </ul>" }

You see the JSON String whin the <p> elements? They are not handcoded by me, they are MongoDB documents, and serialized in JSON by Mongo Lib, i want to show them as text on UI. But i have no control over whether quote or single quote should be used.

I will try escapeJavaScript() right now, is it only available in newest version of Japid? But apart from this method we still have the problem when we want to invoke tag/template right?

branaway commented 13 years ago

The method is there from day one, since it's part of Play's own library, JavaExtensions. For tags, I guess you'll need to fix the problem in the tag itself. If you're using local method as opposed to tag, then the escapeJavaScript() can be used as well.

tunggad commented 13 years ago

ok, for the meantime we must satisfy ourself with consequent using of single quotes and manual escaping of special character. But i wish a comfortable and sotisphicated solution, like Grails JSON builder.

Using single quotes lead to other error, which i will report in next ticket.