@ -64,7 +64,7 @@ public class MappingJackson2JsonView extends AbstractView {
private JsonEncoding encoding = JsonEncoding . UTF8 ;
private JsonEncoding encoding = JsonEncoding . UTF8 ;
private boolean prefixJson = false ;
private String jsonPrefix ;
private Boolean prettyPrint ;
private Boolean prettyPrint ;
@ -122,6 +122,15 @@ public class MappingJackson2JsonView extends AbstractView {
return this . encoding ;
return this . encoding ;
}
}
/ * *
* Specify a custom prefix to use for this view ' s JSON output .
* Default is none .
* @see # setPrefixJson
* /
public void setJsonPrefix ( String jsonPrefix ) {
this . jsonPrefix = jsonPrefix ;
}
/ * *
/ * *
* Indicates whether the JSON output by this view should be prefixed with < tt > "{} && " < / tt > .
* Indicates whether the JSON output by this view should be prefixed with < tt > "{} && " < / tt > .
* Default is { @code false } .
* Default is { @code false } .
@ -129,9 +138,10 @@ public class MappingJackson2JsonView extends AbstractView {
* The prefix renders the string syntactically invalid as a script so that it cannot be hijacked .
* The prefix renders the string syntactically invalid as a script so that it cannot be hijacked .
* This prefix does not affect the evaluation of JSON , but if JSON validation is performed
* This prefix does not affect the evaluation of JSON , but if JSON validation is performed
* on the string , the prefix would need to be ignored .
* on the string , the prefix would need to be ignored .
* @see # setJsonPrefix
* /
* /
public void setPrefixJson ( boolean prefixJson ) {
public void setPrefixJson ( boolean prefixJson ) {
this . prefixJson = prefixJson ;
this . jsonPrefix = "{} && " ;
}
}
/ * *
/ * *
@ -243,7 +253,7 @@ public class MappingJackson2JsonView extends AbstractView {
OutputStream stream = ( this . updateContentLength ? createTemporaryOutputStream ( ) : response . getOutputStream ( ) ) ;
OutputStream stream = ( this . updateContentLength ? createTemporaryOutputStream ( ) : response . getOutputStream ( ) ) ;
Object value = filterModel ( model ) ;
Object value = filterModel ( model ) ;
writeContent ( stream , value , this . prefixJson ) ;
writeContent ( stream , value , this . jsonPrefix ) ;
if ( this . updateContentLength ) {
if ( this . updateContentLength ) {
writeToResponse ( response , ( ByteArrayOutputStream ) stream ) ;
writeToResponse ( response , ( ByteArrayOutputStream ) stream ) ;
}
}
@ -272,11 +282,11 @@ public class MappingJackson2JsonView extends AbstractView {
* Write the actual JSON content to the stream .
* Write the actual JSON content to the stream .
* @param stream the output stream to use
* @param stream the output stream to use
* @param value the value to be rendered , as returned from { @link # filterModel }
* @param value the value to be rendered , as returned from { @link # filterModel }
* @param prefixJson whether the JSON output by this view should be prefixed
* @param jsonPrefix the prefix for this view ' s JSON output
* with < tt > "{} && " < / tt > ( as indicated through { @link # setPrefixJson } )
* ( as indicated through { @link # setJsonPrefix } / { @link # setPrefixJson } )
* @throws IOException if writing failed
* @throws IOException if writing failed
* /
* /
protected void writeContent ( OutputStream stream , Object value , boolean prefixJson ) throws IOException {
protected void writeContent ( OutputStream stream , Object value , String jsonPrefix ) throws IOException {
// The following has been deprecated as late as Jackson 2.2 (April 2013);
// The following has been deprecated as late as Jackson 2.2 (April 2013);
// preserved for the time being, for Jackson 2.0/2.1 compatibility.
// preserved for the time being, for Jackson 2.0/2.1 compatibility.
@SuppressWarnings ( "deprecation" )
@SuppressWarnings ( "deprecation" )
@ -288,8 +298,8 @@ public class MappingJackson2JsonView extends AbstractView {
generator . useDefaultPrettyPrinter ( ) ;
generator . useDefaultPrettyPrinter ( ) ;
}
}
if ( prefixJson ) {
if ( jsonPrefix ! = null ) {
generator . writeRaw ( "{} && " ) ;
generator . writeRaw ( jsonPrefix ) ;
}
}
this . objectMapper . writeValue ( generator , value ) ;
this . objectMapper . writeValue ( generator , value ) ;
}
}