You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@struts.apache.org by "Matthew Payne (JIRA)" <ji...@apache.org> on 2010/12/14 18:01:26 UTC

[jira] Created: (WW-3545) JSONValidationInterceptor Returns Invalid "true" json. Do not put javascript comments around json

JSONValidationInterceptor Returns Invalid "true" json.  Do not put javascript comments around json
--------------------------------------------------------------------------------------------------

                 Key: WW-3545
                 URL: https://issues.apache.org/jira/browse/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.

see http://jquery14.com/day-01/jquery-14

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 
http://stackoverflow.com/questions/244777/can-i-comment-a-json-file

http://third-bit.com/blog/archives/1749.html
 a little off topic
http://blog.getify.com/2010/06/json-comments/

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)
	@SuppressWarnings("unchecked")
	protected String buildResponse(ValidationAware validationAware) {
		// should we use FreeMarker here?
		StringBuilder sb = new StringBuilder();
		sb.append("{ ");

		if (validationAware.hasErrors()) {
			// action errors
			if (validationAware.hasActionErrors()) {
				sb.append("\"errors\":");
				sb.append(buildArray(validationAware.getActionErrors()));
			}

			// field errors
			if (validationAware.hasFieldErrors()) {
				if (validationAware.hasActionErrors())
					sb.append(",");
				sb.append("\"fieldErrors\": {");
				Map<String, List<String>> fieldErrors = validationAware.getFieldErrors();
				for (Map.Entry<String, List<String>> fieldError : fieldErrors.entrySet()) {
					sb.append("\"");
					// 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);
					sb.append("\":");
					sb.append(buildArray(fieldError.getValue()));
					sb.append(",");
				}
				// remove trailing comma, IE creates an empty object, duh
				sb.deleteCharAt(sb.length() - 1);
				sb.append("}");
			}
		}

		sb.append("}");
		/*
		 * 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.


[jira] [Commented] (WW-3545) JSONValidationInterceptor Returns Invalid "true" json. Do not put javascript comments around json

Posted by "Hudson (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/WW-3545?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13061921#comment-13061921 ] 

Hudson commented on WW-3545:
----------------------------

Integrated in Struts2 #331 (See [https://builds.apache.org/job/Struts2/331/])
    WW-3545 - remove comments for JSON output

lukaszlenart : 
Files : 
* /struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/validation/JSONValidationInterceptorTest.java
* /struts/struts2/trunk/core/src/test/resources/org/apache/struts2/interceptor/validation/json-1.txt
* /struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/validation/JSONValidationInterceptor.java


> JSONValidationInterceptor Returns Invalid "true" json.  Do not put javascript comments around json
> --------------------------------------------------------------------------------------------------
>
>                 Key: WW-3545
>                 URL: https://issues.apache.org/jira/browse/WW-3545
>             Project: Struts 2
>          Issue Type: Bug
>          Components: XML Validators
>    Affects Versions: 2.2.1
>            Reporter: Matthew Payne
>            Assignee: Lukasz Lenart
>            Priority: Critical
>             Fix For: 2.3
>
>
> 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.
> see http://jquery14.com/day-01/jquery-14
> 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 
> http://stackoverflow.com/questions/244777/can-i-comment-a-json-file
> http://third-bit.com/blog/archives/1749.html
>  a little off topic
> http://blog.getify.com/2010/06/json-comments/
> 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)
> 	@SuppressWarnings("unchecked")
> 	protected String buildResponse(ValidationAware validationAware) {
> 		// should we use FreeMarker here?
> 		StringBuilder sb = new StringBuilder();
> 		sb.append("{ ");
> 		if (validationAware.hasErrors()) {
> 			// action errors
> 			if (validationAware.hasActionErrors()) {
> 				sb.append("\"errors\":");
> 				sb.append(buildArray(validationAware.getActionErrors()));
> 			}
> 			// field errors
> 			if (validationAware.hasFieldErrors()) {
> 				if (validationAware.hasActionErrors())
> 					sb.append(",");
> 				sb.append("\"fieldErrors\": {");
> 				Map<String, List<String>> fieldErrors = validationAware.getFieldErrors();
> 				for (Map.Entry<String, List<String>> fieldError : fieldErrors.entrySet()) {
> 					sb.append("\"");
> 					// 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);
> 					sb.append("\":");
> 					sb.append(buildArray(fieldError.getValue()));
> 					sb.append(",");
> 				}
> 				// remove trailing comma, IE creates an empty object, duh
> 				sb.deleteCharAt(sb.length() - 1);
> 				sb.append("}");
> 			}
> 		}
> 		sb.append("}");
> 		/*
> 		 * response should be something like: { "errors": ["this", "that"],
> 		 * "fieldErrors": { field1: "this", field2: "that" } }
> 		 */
> 		return sb.toString();
> 	}

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Closed] (WW-3545) JSONValidationInterceptor Returns Invalid "true" json. Do not put javascript comments around json

Posted by "Lukasz Lenart (Closed) (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/WW-3545?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Lukasz Lenart closed WW-3545.
-----------------------------

    
> JSONValidationInterceptor Returns Invalid "true" json.  Do not put javascript comments around json
> --------------------------------------------------------------------------------------------------
>
>                 Key: WW-3545
>                 URL: https://issues.apache.org/jira/browse/WW-3545
>             Project: Struts 2
>          Issue Type: Bug
>          Components: XML Validators
>    Affects Versions: 2.2.1
>            Reporter: Matthew Payne
>            Assignee: Lukasz Lenart
>            Priority: Critical
>             Fix For: 2.3.1
>
>
> 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.
> see http://jquery14.com/day-01/jquery-14
> 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 
> http://stackoverflow.com/questions/244777/can-i-comment-a-json-file
> http://third-bit.com/blog/archives/1749.html
>  a little off topic
> http://blog.getify.com/2010/06/json-comments/
> 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)
> 	@SuppressWarnings("unchecked")
> 	protected String buildResponse(ValidationAware validationAware) {
> 		// should we use FreeMarker here?
> 		StringBuilder sb = new StringBuilder();
> 		sb.append("{ ");
> 		if (validationAware.hasErrors()) {
> 			// action errors
> 			if (validationAware.hasActionErrors()) {
> 				sb.append("\"errors\":");
> 				sb.append(buildArray(validationAware.getActionErrors()));
> 			}
> 			// field errors
> 			if (validationAware.hasFieldErrors()) {
> 				if (validationAware.hasActionErrors())
> 					sb.append(",");
> 				sb.append("\"fieldErrors\": {");
> 				Map<String, List<String>> fieldErrors = validationAware.getFieldErrors();
> 				for (Map.Entry<String, List<String>> fieldError : fieldErrors.entrySet()) {
> 					sb.append("\"");
> 					// 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);
> 					sb.append("\":");
> 					sb.append(buildArray(fieldError.getValue()));
> 					sb.append(",");
> 				}
> 				// remove trailing comma, IE creates an empty object, duh
> 				sb.deleteCharAt(sb.length() - 1);
> 				sb.append("}");
> 			}
> 		}
> 		sb.append("}");
> 		/*
> 		 * response should be something like: { "errors": ["this", "that"],
> 		 * "fieldErrors": { field1: "this", field2: "that" } }
> 		 */
> 		return sb.toString();
> 	}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators: https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] Updated: (WW-3545) JSONValidationInterceptor Returns Invalid "true" json. Do not put javascript comments around json

Posted by "Matthew Payne (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/WW-3545?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Matthew Payne updated WW-3545:
------------------------------


Should be fairly trival fix

> JSONValidationInterceptor Returns Invalid "true" json.  Do not put javascript comments around json
> --------------------------------------------------------------------------------------------------
>
>                 Key: WW-3545
>                 URL: https://issues.apache.org/jira/browse/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.
> see http://jquery14.com/day-01/jquery-14
> 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 
> http://stackoverflow.com/questions/244777/can-i-comment-a-json-file
> http://third-bit.com/blog/archives/1749.html
>  a little off topic
> http://blog.getify.com/2010/06/json-comments/
> 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)
> 	@SuppressWarnings("unchecked")
> 	protected String buildResponse(ValidationAware validationAware) {
> 		// should we use FreeMarker here?
> 		StringBuilder sb = new StringBuilder();
> 		sb.append("{ ");
> 		if (validationAware.hasErrors()) {
> 			// action errors
> 			if (validationAware.hasActionErrors()) {
> 				sb.append("\"errors\":");
> 				sb.append(buildArray(validationAware.getActionErrors()));
> 			}
> 			// field errors
> 			if (validationAware.hasFieldErrors()) {
> 				if (validationAware.hasActionErrors())
> 					sb.append(",");
> 				sb.append("\"fieldErrors\": {");
> 				Map<String, List<String>> fieldErrors = validationAware.getFieldErrors();
> 				for (Map.Entry<String, List<String>> fieldError : fieldErrors.entrySet()) {
> 					sb.append("\"");
> 					// 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);
> 					sb.append("\":");
> 					sb.append(buildArray(fieldError.getValue()));
> 					sb.append(",");
> 				}
> 				// remove trailing comma, IE creates an empty object, duh
> 				sb.deleteCharAt(sb.length() - 1);
> 				sb.append("}");
> 			}
> 		}
> 		sb.append("}");
> 		/*
> 		 * 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.


[jira] Updated: (WW-3545) JSONValidationInterceptor Returns Invalid "true" json. Do not put javascript comments around json

Posted by "Lukasz Lenart (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/WW-3545?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Lukasz Lenart updated WW-3545:
------------------------------

    Affects Version/s: 2.2.1
        Fix Version/s: 2.2.2

> JSONValidationInterceptor Returns Invalid "true" json.  Do not put javascript comments around json
> --------------------------------------------------------------------------------------------------
>
>                 Key: WW-3545
>                 URL: https://issues.apache.org/jira/browse/WW-3545
>             Project: Struts 2
>          Issue Type: Bug
>          Components: XML Validators
>    Affects Versions: 2.2.1
>            Reporter: Matthew Payne
>            Assignee: Lukasz Lenart
>            Priority: Critical
>             Fix For: 2.2.2
>
>
> 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.
> see http://jquery14.com/day-01/jquery-14
> 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 
> http://stackoverflow.com/questions/244777/can-i-comment-a-json-file
> http://third-bit.com/blog/archives/1749.html
>  a little off topic
> http://blog.getify.com/2010/06/json-comments/
> 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)
> 	@SuppressWarnings("unchecked")
> 	protected String buildResponse(ValidationAware validationAware) {
> 		// should we use FreeMarker here?
> 		StringBuilder sb = new StringBuilder();
> 		sb.append("{ ");
> 		if (validationAware.hasErrors()) {
> 			// action errors
> 			if (validationAware.hasActionErrors()) {
> 				sb.append("\"errors\":");
> 				sb.append(buildArray(validationAware.getActionErrors()));
> 			}
> 			// field errors
> 			if (validationAware.hasFieldErrors()) {
> 				if (validationAware.hasActionErrors())
> 					sb.append(",");
> 				sb.append("\"fieldErrors\": {");
> 				Map<String, List<String>> fieldErrors = validationAware.getFieldErrors();
> 				for (Map.Entry<String, List<String>> fieldError : fieldErrors.entrySet()) {
> 					sb.append("\"");
> 					// 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);
> 					sb.append("\":");
> 					sb.append(buildArray(fieldError.getValue()));
> 					sb.append(",");
> 				}
> 				// remove trailing comma, IE creates an empty object, duh
> 				sb.deleteCharAt(sb.length() - 1);
> 				sb.append("}");
> 			}
> 		}
> 		sb.append("}");
> 		/*
> 		 * 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.


[jira] [Resolved] (WW-3545) JSONValidationInterceptor Returns Invalid "true" json. Do not put javascript comments around json

Posted by "Lukasz Lenart (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/WW-3545?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Lukasz Lenart resolved WW-3545.
-------------------------------

       Resolution: Fixed
    Fix Version/s:     (was: 2.2.x)
                   2.3

Done, thanks for the solution!

> JSONValidationInterceptor Returns Invalid "true" json.  Do not put javascript comments around json
> --------------------------------------------------------------------------------------------------
>
>                 Key: WW-3545
>                 URL: https://issues.apache.org/jira/browse/WW-3545
>             Project: Struts 2
>          Issue Type: Bug
>          Components: XML Validators
>    Affects Versions: 2.2.1
>            Reporter: Matthew Payne
>            Assignee: Lukasz Lenart
>            Priority: Critical
>             Fix For: 2.3
>
>
> 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.
> see http://jquery14.com/day-01/jquery-14
> 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 
> http://stackoverflow.com/questions/244777/can-i-comment-a-json-file
> http://third-bit.com/blog/archives/1749.html
>  a little off topic
> http://blog.getify.com/2010/06/json-comments/
> 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)
> 	@SuppressWarnings("unchecked")
> 	protected String buildResponse(ValidationAware validationAware) {
> 		// should we use FreeMarker here?
> 		StringBuilder sb = new StringBuilder();
> 		sb.append("{ ");
> 		if (validationAware.hasErrors()) {
> 			// action errors
> 			if (validationAware.hasActionErrors()) {
> 				sb.append("\"errors\":");
> 				sb.append(buildArray(validationAware.getActionErrors()));
> 			}
> 			// field errors
> 			if (validationAware.hasFieldErrors()) {
> 				if (validationAware.hasActionErrors())
> 					sb.append(",");
> 				sb.append("\"fieldErrors\": {");
> 				Map<String, List<String>> fieldErrors = validationAware.getFieldErrors();
> 				for (Map.Entry<String, List<String>> fieldError : fieldErrors.entrySet()) {
> 					sb.append("\"");
> 					// 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);
> 					sb.append("\":");
> 					sb.append(buildArray(fieldError.getValue()));
> 					sb.append(",");
> 				}
> 				// remove trailing comma, IE creates an empty object, duh
> 				sb.deleteCharAt(sb.length() - 1);
> 				sb.append("}");
> 			}
> 		}
> 		sb.append("}");
> 		/*
> 		 * response should be something like: { "errors": ["this", "that"],
> 		 * "fieldErrors": { field1: "this", field2: "that" } }
> 		 */
> 		return sb.toString();
> 	}

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] Assigned: (WW-3545) JSONValidationInterceptor Returns Invalid "true" json. Do not put javascript comments around json

Posted by "Lukasz Lenart (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/WW-3545?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Lukasz Lenart reassigned WW-3545:
---------------------------------

    Assignee: Lukasz Lenart

> JSONValidationInterceptor Returns Invalid "true" json.  Do not put javascript comments around json
> --------------------------------------------------------------------------------------------------
>
>                 Key: WW-3545
>                 URL: https://issues.apache.org/jira/browse/WW-3545
>             Project: Struts 2
>          Issue Type: Bug
>          Components: XML Validators
>            Reporter: Matthew Payne
>            Assignee: Lukasz Lenart
>            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.
> see http://jquery14.com/day-01/jquery-14
> 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 
> http://stackoverflow.com/questions/244777/can-i-comment-a-json-file
> http://third-bit.com/blog/archives/1749.html
>  a little off topic
> http://blog.getify.com/2010/06/json-comments/
> 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)
> 	@SuppressWarnings("unchecked")
> 	protected String buildResponse(ValidationAware validationAware) {
> 		// should we use FreeMarker here?
> 		StringBuilder sb = new StringBuilder();
> 		sb.append("{ ");
> 		if (validationAware.hasErrors()) {
> 			// action errors
> 			if (validationAware.hasActionErrors()) {
> 				sb.append("\"errors\":");
> 				sb.append(buildArray(validationAware.getActionErrors()));
> 			}
> 			// field errors
> 			if (validationAware.hasFieldErrors()) {
> 				if (validationAware.hasActionErrors())
> 					sb.append(",");
> 				sb.append("\"fieldErrors\": {");
> 				Map<String, List<String>> fieldErrors = validationAware.getFieldErrors();
> 				for (Map.Entry<String, List<String>> fieldError : fieldErrors.entrySet()) {
> 					sb.append("\"");
> 					// 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);
> 					sb.append("\":");
> 					sb.append(buildArray(fieldError.getValue()));
> 					sb.append(",");
> 				}
> 				// remove trailing comma, IE creates an empty object, duh
> 				sb.deleteCharAt(sb.length() - 1);
> 				sb.append("}");
> 			}
> 		}
> 		sb.append("}");
> 		/*
> 		 * 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.


[jira] Updated: (WW-3545) JSONValidationInterceptor Returns Invalid "true" json. Do not put javascript comments around json

Posted by "Lukasz Lenart (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/WW-3545?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Lukasz Lenart updated WW-3545:
------------------------------

    Fix Version/s:     (was: 2.2.2)
                   2.2.x

Postponed till next major release

> JSONValidationInterceptor Returns Invalid "true" json.  Do not put javascript comments around json
> --------------------------------------------------------------------------------------------------
>
>                 Key: WW-3545
>                 URL: https://issues.apache.org/jira/browse/WW-3545
>             Project: Struts 2
>          Issue Type: Bug
>          Components: XML Validators
>    Affects Versions: 2.2.1
>            Reporter: Matthew Payne
>            Assignee: Lukasz Lenart
>            Priority: Critical
>             Fix For: 2.2.x
>
>
> 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.
> see http://jquery14.com/day-01/jquery-14
> 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 
> http://stackoverflow.com/questions/244777/can-i-comment-a-json-file
> http://third-bit.com/blog/archives/1749.html
>  a little off topic
> http://blog.getify.com/2010/06/json-comments/
> 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)
> 	@SuppressWarnings("unchecked")
> 	protected String buildResponse(ValidationAware validationAware) {
> 		// should we use FreeMarker here?
> 		StringBuilder sb = new StringBuilder();
> 		sb.append("{ ");
> 		if (validationAware.hasErrors()) {
> 			// action errors
> 			if (validationAware.hasActionErrors()) {
> 				sb.append("\"errors\":");
> 				sb.append(buildArray(validationAware.getActionErrors()));
> 			}
> 			// field errors
> 			if (validationAware.hasFieldErrors()) {
> 				if (validationAware.hasActionErrors())
> 					sb.append(",");
> 				sb.append("\"fieldErrors\": {");
> 				Map<String, List<String>> fieldErrors = validationAware.getFieldErrors();
> 				for (Map.Entry<String, List<String>> fieldError : fieldErrors.entrySet()) {
> 					sb.append("\"");
> 					// 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);
> 					sb.append("\":");
> 					sb.append(buildArray(fieldError.getValue()));
> 					sb.append(",");
> 				}
> 				// remove trailing comma, IE creates an empty object, duh
> 				sb.deleteCharAt(sb.length() - 1);
> 				sb.append("}");
> 			}
> 		}
> 		sb.append("}");
> 		/*
> 		 * response should be something like: { "errors": ["this", "that"],
> 		 * "fieldErrors": { field1: "this", field2: "that" } }
> 		 */
> 		return sb.toString();
> 	}

-- 
This message is automatically generated by JIRA.
-
For more information on JIRA, see: http://www.atlassian.com/software/jira