0

I'm writing some unit tests in Java 8 (corporate environment; no control over the version) that need different variations of JSON strings. These are a PITA to write without text blocks. Is there a way to at least avoid all the escape characters?

"{\"field_name\": \"value\"...and on and on and on.

gph
  • 1,045
  • 8
  • 25
  • tl;dr It's been proposed as a feature in Java but apparently folks didn't like it. So no, not really, unfortunately. – Silvio Mayolo Nov 10 '21 at 15:26
  • There is a trick that may or may not work, depending on which JSON library you are using. For instance `{"field_name" : "value"}` is a valid JSON, but you will need to escape the quotes in your java code as you are already painfully aware. However, `{'field_name' : 'value'}` is (strictly speaking) NOT a valid JSON, but many libraries would accept it as valid. If it is accepted as valid you won't have to escape anything – Michael Gantman Nov 10 '21 at 15:36

0 Answers0