How to make JSON-lib JSONObject.put (..) escape a line containing JSON?

When using JSON-lib JSONObject , how can I stop the put method from storing a String that contains JSON as JSON and not as an escaped string?

For instance:

 JSONObject obj = new JSONObject(); obj.put("jsonStringValue","{\"hello\":\"world\"}"); obj.put("naturalStringValue", "\"hello world\""); System.out.println(obj.toString()); System.out.println(obj.getString("jsonStringValue")); System.out.println(obj.getString("naturalStringValue")); 

prints:

 {"jsonStringValue":{"hello":"world"},"naturalStringValue":"\"hello world\""} {"hello":"world"} "hello world" 

and I want it printed:

 {"jsonStringValue":"{\"hello\":\"world\"}","naturalStringValue":"\"hello world\""} {"hello":"world"} "hello world" 

Yes, I understand that this is unpleasant. However, this does support the JSON serialization pipeline, for which, for interoperability, this is the expected behavior. There are cases when we will serialize user input, which may be / contain valid JSON. We do not want user input to become part of the JSON object that we serialize the specified input.

Manual escaping does not work because JSON-lib avoids the \ characters:

 JSONObject obj = new JSONObject(); obj.put("naturalJSONValue","{\"hello\":\"world\"}"); obj.put("escapedJSONValue", "{\\\"hello\\\":\\\"world\\\"}"); System.out.println(obj.toString()); System.out.println(obj.getString("naturalJSONValue")); System.out.println(obj.getString("escapedJSONValue")); 

Conclusion:

 {"naturalJSONValue":{"hello":"world"},"escapedJSONValue":"{\\\"hello\\\":\\\"world\\\"}"} {"hello":"world"} {\"hello\":\"world\"} 

At this point, any workarounds to enable manual selective escaping of a complex JSON object completely negate the importance of using JSON-lib in the first place.

In addition, I understand that this question has been asked before , but, unfortunately, I cannot easily accept his answer. JSON-lib is a heavily used dependency in many areas of my project, and sharing it will be a big deal. I need to be absolutely sure that there is no way to achieve this goal with JSON-lib before I can entertain the exchange for Jackson, just-json or Gson.

+6
source share
2 answers

Use single quotes to quote a string. From the doc:

Strings can be specified using '(single quote).

Lines do not have to be quotation marks at all if they do not start with a quote or a single quote, and if they do not contain leading or trailing spaces and if they do not contain any of these characters: {} [] the sign is #, and if they do not look like numbers and if they are not reserved words true, false or null.

So, changing your example:

 net.sf.json.JSONObject obj = new net.sf.json.JSONObject(); obj.put("jsonStringValue","{\"hello\":\"world\"}"); obj.put("quotedJsonStringValue","\'{\"hello\":\"world\"}\'"); obj.put("naturalStringValue", "\"hello world\""); System.out.println(obj.toString()); System.out.println(obj.getString("jsonStringValue")); System.out.println(obj.getString("quotedJsonStringValue")); System.out.println(obj.getString("naturalStringValue")); 

It produces:

 {"jsonStringValue":{"hello":"world"},"quotedJsonStringValue":"{\"hello\":\"world\"}","naturalStringValue":"\"hello world\""} {"hello":"world"} {"hello":"world"} "hello world" 

Note that quotedJsonStringValue treated as a string value, not JSON, and appears in the JSON output.

+2
source

This worked for me with json-lib 2.4:

 System.out.println( new JSONStringer() .object() .key("jsonStringValue") .value("{\"hello\":\"world\"}") .key("naturalStringValue") .value("\"hello world\"") .endObject() .toString()); 

Conclusion:

 {"jsonStringValue":"{\"hello\":\"world\"}","naturalStringValue":"\"hello world\""} 

Is this a possible solution for you?

UPDATE:

Revised my answer with a possible solution

+5
source

Source: https://habr.com/ru/post/889224/


All Articles