Escape forward slash in Jackson

10,457

Solution 1

In addition to other suggestions, Jackson 1.8 also has "character escapes" feature, which allows redefining escaping rules. Documentation is lacking, but basically you need to implement CharacterEscapes (see http://jackson.codehaus.org/1.8.2/javadoc/org/codehaus/jackson/io/CharacterEscape), register with JsonFactory (or directly JsonGenerator), and then escaping will be done according to whatever rules you want. In this case you could just change settings for '/' to use ESCAPE_STANDARD.

Additionally you could also add a feature request to add simple on/off feature, as this specific thing sounds like it might be useful for others as well. But has not yet been requested specifically as far as I know.

Solution 2

Using StaxMan's answer, I ended up with the following code:

   public class CustomCharacterEscapes extends CharacterEscapes {

     private static final Logger log = Logger.getLogger(CustomCharacterEscapes.class);

     private final int[] _asciiEscapes;

     public CustomCharacterEscapes() {
       _asciiEscapes = standardAsciiEscapesForJSON();
       _asciiEscapes['/'] = CharacterEscapes.ESCAPE_STANDARD;
     }

     @Override
     public int[] getEscapeCodesForAscii() {
       return _asciiEscapes;
     }

     @Override
     public SerializableString getEscapeSequence(int i) {
       return null;
    }
  }


    public class CustomObjectMapper extends ObjectMapper {

     public CustomObjectMapper() {
       this.getJsonFactory().setCharacterEscapes(new CustomCharacterEscapes());
     }

    }

Solution 3

Thanks to StaxMan and Infeligo's answers here (cheers guys) I found a way to provide escaping for / to match the (IMHO horrible) WCF DataContractJsonSerializer date standard format:

/Date(1328053610008+1100)/

which requires the / to be escaped with a backslash resulting in the following across the wire:

\/Date(1328053610008+1100)\/

Just in case it may help someone else here is my CustomCharacterEscapes code that I used:

public class CustomCharacterEscapes extends CharacterEscapes {

    private final int[] _asciiEscapes;

    public CustomCharacterEscapes() {
        _asciiEscapes = standardAsciiEscapesForJSON();
        _asciiEscapes['/'] = CharacterEscapes.ESCAPE_CUSTOM;
    }

    @Override
    public int[] getEscapeCodesForAscii() {
        return _asciiEscapes;
    }

    @Override
    public SerializableString getEscapeSequence(int i) {
        if(i == '/'){
            return new SerializableString() {

                @Override
                public String getValue() {
                    return "\\/";
                }

                @Override
                public int charLength() {
                    return 2;
                }

                @Override
                public char[] asQuotedChars() {
                    return new char[]{'\\','/'};
                }

                @Override
                public byte[] asUnquotedUTF8() {
                    return new byte[]{'\\','/'};
                }

                @Override
                public byte[] asQuotedUTF8() {
                    return new byte[]{'\\','/'};
                }
            };
        }
        else{
            return null;
        }
    }
}
Share:
10,457
Infeligo
Author by

Infeligo

Updated on June 18, 2022

Comments

  • Infeligo
    Infeligo about 2 years

    I use Jackson to generate JSON objects and write them directly into HTML's tag, like this:

       <script>
         var data = $SomeJacksonWrapper.toJson($data);
       </script>
    

    This code breaks if some string contains '</script>' in it. Escaping forward slash (/) would solve the problem and it is alowed by JSON's spec.

    How do I enable it in Jackson?

  • linqu
    linqu over 11 years
    good blog entry explaining how to escape specific characters in jackson: cowtowncoder.com/blog/archives/2012/08/entry_476.html
  • Moebius
    Moebius almost 10 years
    Jackson really sucks. This library has a way to make things easy so complex !