1

Given

  • A JSON string with Apex reserved words (like "currency":"USD" and otherwise undeserializable-into-Date type (like "someDate":"0001-01-01T00:00:00Z" )
  • An apex method that uses

    String s = s.replaceAll('"currency":','"currencyX":')
                .replaceAll('"0001\\-01\\-01T00:00:00Z"','null');
    

before deserializing into a custom Apex type with property String currencyX and Date someDate

When

The incoming Apex string is very large (> 1MB)

Then

Uncatchable error: Regex Too Complicated

What to do?

3

This is basically teaching an old dog (me) new tricks

A cursory reading of the Apex String class documentation shows two methods

  • replace(target,replacement)
  • replaceAll(regExp,replacement)

The naive developer (me) assumes that the first method, replace, only replaces a SINGLE instance of the target string and since the use case assumes you need to replace ALL occurrences of some pattern, you should use the more powerful replaceAll(..)

But epistemic arrogance crept in and I did not realize replace(target,replacement) replaces ALL occurrences of target with replacement

Replaces each substring of a string that matches the literal target sequence target with the specified literal replacement sequence replacement.

So, if you have large strings and you are just doing simple text substitution, avoid replaceAll

String s = s.replace('"currency":','"currencyX":')
                .replace('"0001-01-01T00:00:00Z"','null');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.