Java: Text Blocks + String#formatted = JSON

JEP 378: Text Blocks combined with String#formatted allow inline creation of JSON objects with plain Java:


@Test
public void generateJson() {
    var message = """
        {
            "message":"%s"
        }
        """.formatted("hello,duke");
    System.out.println(message);
}

The code above prints:


{
    "message":"hello,duke"
}

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments
License