Gson will deserialize non-conforming single quoted JSON perfectly. Unfortunately I need to serialize a random interior object that was passed in with single quotes instead of double quotes. The output should look like the object below - see the key "second":
{
"first":{"normal":1}
"second":{'single_quoted': 'random object'}
}
Assume I already have the "second" object as a JsonObject. I know there are ways to customize the serializers, but I haven't found what I'm looking for.
Related
I am using GSON to convert a stringified JSON object into a POJO I have defined. The JSON is very large but the POJO is a small subset of those properties. The original JSON has some properties whose values themselves are stringified JSON objects, so you have something like
{
"someAttribute": "{\"foo\": [{\"bar\"}]}"
}
This is a very simplified example. The actual JSON is much more nested and much larger.
Now when testing, I tried passing in a stringified version of the large json using JSON.stringify from my browser, which resulted in all \" sequences becoming \\". I the subsequently pasted the string into IntelliJ, which then converted the \\" sequences to \\\\\". When GSON tried to serialize this, it threw a malformed JSON exception.
Although I believe this to be an intellij issue of adding extra escape characters (once I changed \\\\\" to \\\" it worked fine), It occurred to me that if my function were to receive improperly escaped JSON due to possible differences in stringification, GSON would throw an exception. I don't know how any serialization package would know what is the correct or incorrect number of escape characters, but given that the actual JSON structure itself is not malformed, what can I do to avoid "false positive" JSON serialization issues?
My application receives an encrypted json from front end, I decrypt it on backend but it is formatted like this:
"{\"firstname\":\"JOHN\",\"lastname\":\"DOE\"}"
yes, there are leading and trailing double quotes on the decrypted json.
GSON and object mapper doesnt work since they detect the quotes on the first character.
Is there way to properly convert this without manually removing the enclosing quotes?
It looks like your data has been double-encoded, so the solution is to double-decode. Specifically: thanks to the backslashes, this is actually a valid JSON string value that represents a stringified JSON object. So if you ask your JSON library to decode this into a String, you should get a string whose value is
{"firstname":"JOHN","lastname":"DOE"}
And then you can ask your JSON library to decode that string into the type you actually want.
How to escape double quotes inside a JSON given the data in the JSON will be obtained dynamically in Java.
Example:
{
"key": "I ask silly questions on "https://www.stackoverflow.com". "
}
In this case, the value in the JSON is populated from dynamically let's say from a user input.
I have tried StringEscapeUtils provided by apache-commons-lang (https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html), but this escapes the entire JSON where as the double quotes inside the JSON needs to escape again.
Possible solution will be to use regex and filter out the value in JSON and escape once before escaping the overall JSON.
But, is there any JAR to escape the inner contents of a JSON?
If doing json processing in java I would use Jackson databind and let it handle json generation. You will probably want to turn some knobs eventually, but simplest possible usage will probably do for this use case:
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(singletonMap("key", "I ask silly questions..."));
I try design a simple all purpose Data Structure that must be convertable to JSON and back. Since I have names and types I need to find an expression for that.
So I look for something like name+type or name<type> or name:type (which i like) or name|type or type[name].
Are there any problems with that? I mean the : is already taken so I need to enclose the name and type (which is always a good idea).
Anything I need to know?
The colon : is part of the JSON syntax so you must enclose a name that contains a colon (as any name) in double quotes ". This
{
"foo:bar": "BAR",
"foo:baz": "BAZ"
}
is valid JSON. Check it at http://jsonlint.com/
The very simple JSON syntax can be read on the JSON.org site.
I would like to output json as a string with double quotes surrounding it and all double quotes inside the string escaped. Does Jackson Json provide any function to do it? What I want to do is exactly opposite of How to parse a JSON string into JsonNode in Jackson?.
Hm, I think doing that would make your output a string (formatted like JSON), as opposed to valid JSON.
I tested it with JSONLint, worth having a go yourself just in case I'm being a dunce.