struts-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Matthew Payne (JIRA)" <>
Subject [jira] Created: (WW-3545) JSONValidationInterceptor Returns Invalid "true" json. Do not put javascript comments around json
Date Tue, 14 Dec 2010 17:01:26 GMT
JSONValidationInterceptor Returns Invalid "true" json.  Do not put javascript comments around

                 Key: WW-3545
             Project: Struts 2
          Issue Type: Bug
          Components: XML Validators
            Reporter: Matthew Payne
            Priority: Critical

Many ui frameworks(JQuery for example) to a direct eval on the json.   Jquery will report
json response is invalid if the comments are left in.


Strict JSON parsing, using native JSON.parse (jQuery.ajax() Documentation, Commit 1, Commit
2, Commit 3)

jQuery 1.3 and earlier used JavaScript's eval to evaluate incoming JSON. jQuery 1.4 uses the
native JSON parser if available. It also validates incoming JSON for validity, so malformed
JSON (for instance {foo: "bar"}) will be rejected by jQuery in jQuery.getJSON and when specifying
"json" as the dataType of an Ajax request.

support material though
 a little off topic

For fix: 
See lines  sb.append("/* { ");
..... and 
  sb.append("} */");  In method JSONValidationInterceptor.buildResponse and 

Also line  response.getWriter().print("/* {} */") in doIntercept  change that line to response.getWriter().print("{}");

changed buildResponse Method below.
Build response without comments (really a 2 line change)
	protected String buildResponse(ValidationAware validationAware) {
		// should we use FreeMarker here?
		StringBuilder sb = new StringBuilder();
		sb.append("{ ");

		if (validationAware.hasErrors()) {
			// action errors
			if (validationAware.hasActionErrors()) {

			// field errors
			if (validationAware.hasFieldErrors()) {
				if (validationAware.hasActionErrors())
				sb.append("\"fieldErrors\": {");
				Map<String, List<String>> fieldErrors = validationAware.getFieldErrors();
				for (Map.Entry<String, List<String>> fieldError : fieldErrors.entrySet())
					// if it is model driven, remove "model." see WW-2721
					String fieldErrorKey = fieldError.getKey();
					sb.append(((validationAware instanceof ModelDriven) && fieldErrorKey.startsWith("model."))
? fieldErrorKey
							.substring(6) : fieldErrorKey);
				// remove trailing comma, IE creates an empty object, duh
				sb.deleteCharAt(sb.length() - 1);

		 * response should be something like: { "errors": ["this", "that"],
		 * "fieldErrors": { field1: "this", field2: "that" } }
		return sb.toString();

This message is automatically generated by JIRA.
You can reply to this email to add a comment to the issue online.

View raw message