Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import org.codehaus.jackson.SerializableString
import org.codehaus.jackson.io.CharacterEscapes
/**
* Answer to:
* http://stackoverflow.com/questions/34409083/jackson-object-mapper-with-custom-escapes-have-limit-on-field-length
*
* Created by Marc Nuri on 2015-12-23.
*/
@Grab('org.codehaus.jackson:jackson-core-asl:1.9.13')
@Grab('org.codehaus.jackson:jackson-mapper-asl:1.9.13')
@Grab('org.apache.commons:commons-lang3:3.4')
Map<Object,Object> specialMap = new HashMap<>();
specialMap.put("id", "<EscapedTag>482863</EscapedTag>,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863,482863");
specialMap.put("name", "<body ng-app=\"test-app\">Body</body>")
String result = null;
org.codehaus.jackson.map.ObjectMapper objectMapper = new org.codehaus.jackson.map.ObjectMapper();
try {
//Escaping Special Characters
objectMapper.getJsonFactory().setCharacterEscapes(new CharacterEscapes() {
private static final int[] esc;
static{
esc = CharacterEscapes.standardAsciiEscapesForJSON();
esc[(int)'<'] = CharacterEscapes.ESCAPE_CUSTOM;
esc[(int)'>'] = CharacterEscapes.ESCAPE_CUSTOM;
esc[(int)'&'] = CharacterEscapes.ESCAPE_CUSTOM;
esc[(int)'\''] = CharacterEscapes.ESCAPE_CUSTOM;
esc[(int)'\"'] = CharacterEscapes.ESCAPE_CUSTOM;
}
@Override
int[] getEscapeCodesForAscii() {
return esc
}
@Override
SerializableString getEscapeSequence(final int i) {
return new SerializableString() {
private final String stringValue = Character.toString((char) i);
@Override
public String getValue() {
return org.apache.commons.lang3.StringEscapeUtils.escapeXml11(stringValue);
}
@Override
public int charLength() {
return stringValue.length();
}
@Override
public char[] asQuotedChars() {
return new char[0];
}
@Override
public byte[] asUnquotedUTF8() {
return new byte[0];
}
@Override
public byte[] asQuotedUTF8() {
return new byte[0];
}
}
}
});
result = objectMapper.writeValueAsString(specialMap);
println result;
} catch (IOException e) {
e.printStackTrace();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.