I'm having a problem with unprintable unicode characters breaking a Visualforce page. The problem is that some of my data contains the unicode character 8232 (\u2028 - LINE SEPARATOR), which it probably shouldn't, but does (it gets added when users paste from certain MS Word files into text fields).
I'm trying to pass JSON data to a javascript function in a Visualforce page:
/* In my Apex controller */
public String getDataJSON () {
List<Custom__c> records = [SELECT Id, Additional_Details__c FROM Custom__c WHERE Id = 'Etc'];
return JSON.serialize(records);
}
<!-- In my Visualforce page -->
<script>
App.initialize({
data: {!dataJSON},
other: 'etc.'
});
</script>
This works fine on 99% of page loads, but for some data, the Javascript parser dies with an Unexpected token ILLEGAL
syntax error.
Is there a way to get the JSON serializer to replace this character with a Javascript-safe representation (such as "\u2028")?