I'm using java-17 and the following code snippet breaks because MessageFormat gets JSON symbols such as {
and }
and interprets them as its own formatters preventing the String to be formatted:
import java.math.BigDecimal;
import java.text.MessageFormat;
public class Test {
public static void main(final String[] args) {
System.out.println(MessageFormat.format("""
{
"a": "{0}",
{
"b": "{0}"
"c": "{0}"
"d": {1,number,#.##}
}
}""", "Test", new BigDecimal("10.123")));
}
}
Here's the stack trace:
Exception in thread "main" java.lang.IllegalArgumentException: can't parse argument number:
"a": "{0}"
at java.base/java.text.MessageFormat.makeFormat(MessageFormat.java:1454)
at java.base/java.text.MessageFormat.applyPattern(MessageFormat.java:492)
at java.base/java.text.MessageFormat.<init>(MessageFormat.java:371)
at java.base/java.text.MessageFormat.format(MessageFormat.java:860)
at Test.main(Test.java:9)
Caused by: java.lang.NumberFormatException: For input string: "
"a": "{0}""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:654)
at java.base/java.lang.Integer.parseInt(Integer.java:786)
at java.base/java.text.MessageFormat.makeFormat(MessageFormat.java:1452)
... 4 more
What's the correct way to escape these symbols in order to MessageFormat actually formats the String?