Jackson serialize and deserialize objects without double quotes - serialization

I want to serialize and deserialize objects like java Date or UUID to and from String.
The problem is that it surrounds the String itself with double quotes:
String s = objectMapper.writeValueAsString(date);
System.out.println("String: ##" + s + "##"); // String: ##"2017-12-06T04:50:30Z"##
...
Date d = objectMapper.readValue(s, Date.class); // Expects the String to be "\"2017-12-06T04:50:30Z\""
Is there a way to configure the object mapper to not use/expect the redundant double quotes?

If anyone faces the same problem: I didn't find any other way but an ugly workaround. I just remove/add the quotes to the string after/before passing it to the object mapper.

Related

How do I convert a JSON array with a string value into a string

I am using set-variable to save one field from the payload into a variable. I would like to save this variable as a string, but it ends up as a JSON array. I then need to concatenate this value with a value from my local configuration file.
I need to either save the payload field as a string or convert the array into a string during concatenation. However, I can't seem to do either.
This was a misunderstanding on my part. The incoming payload was an array so this field was an array. I wrapped it in a foreach and everything worked.

Jackson YAML Parser Deleted Special characters

I have issue while using ObjectMapper with YAMLFactory to Parse a YAML File
The YAML file I’m trying to parse : https://drive.google.com/open?id=1Q85OmjH-IAIkordikLTsC1oQVTg8ggc8
Parsing the File using readValue as shown here :
ObjectMapper mapper = new ObjectMapper(new YAMLFactory().enable(Feature.MINIMIZE_QUOTES)//
.disable(Feature.WRITE_DOC_START_MARKER)//
.disable(YAMLGenerator.Feature.SPLIT_LINES));
TypeReference<HashMap<String, Object>> typeRef = new TypeReference<HashMap<String, Object>>() {};
HashMap<String, Object> obj = mapper.readValue(responseBuffer.toString(), typeRef);
Converting the Obj to json then to YAML again by :
JsonElement jsonElem = wrapJacksonObject(obj);
String cloudTemplateJsonString = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting()//
.create()//
.toJson(jsonElem);
JsonNode jsonNode = mapper.readTree(cloudTemplateJsonString);
String yaml = new YAMLMapper().enable(Feature.MINIMIZE_QUOTES)//
.disable(Feature.WRITE_DOC_START_MARKER)//
.writeValueAsString(jsonNode);
After checking the last String, I see that these Special Characters are Changed/Deleted (they are Changed exactly after Point 2) :
a. ‘ transferred to “ or Deleted
b. ! : Regarding the exclamation mark : the whole string after it until first space is deleted totally
Examples :
Version: !Join ['-', [!Ref GatewayVersion, GW]]
After Parsing
Version:
- '-'
- - GatewayVersion
- GW
Also single Quotes sometimes Deleted / Converted to double Quote
AllowedPattern: '^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})$'
After Parsing Single quotes Deleted :
AllowedPattern: ^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})$
I try to use Escape Characters Customization By customizing Implementation for CharacterEscapes class but it didn’t help
In YAML, a value such as a string literal can be prepended by tokens indicating metadata about the node, known as node properties. Tokens beginning with a bang ! are considered to be 'node tags', and tokens begining with an ampersand & are 'node anchors'.
https://yaml.org/spec/1.2/spec.html#id2783797
JSON does not have an equivalent capability. Because Jackson is primarily a JSON parsing library its internal representation of structured data nodes do not have fields for metadata, and so its YAMLFactory parser implementation simply discards them.
Looking at your YAML file I expect the intended parser of the file (aws' cloudwatch cli tool?) would have known how to use those !Join and !Ref node tags to construct an internal representation of the Version field.
Similarly, single or double quotes surrounding a text value are considered to be part of the markup (i.e., used the parser) rather than part of the value. Thus the parser discards these characters (after using them as guides on how to consume the value). Quotes (double or single) may be added or not as neccessary when reserializing an internal representation back into YAML or JSON.

Redis return value for a string object

When I save a string to redis using ServiceStack.Redis:
client.Add("key1","abc");
While fetching the value, it returns:
client.GetValue("key1");
it returns
"\"abc\""
How do I get the complete string?
Thanks
It appears as though the client.Add() method converts the value to a string (even strings) and wraps them in quotes. The client.SetValue() method only accepts strings and does not wrap them in quotes.
One option would be to convert the value into a string yourself. Either via the common ToString() method, or another method to get the needed string from the object.
If the Add() method is necessary however. You could opt to check if the string is wrapped in quotes when you get it via GetValue() and if so, remove them.
Redis converts string to JSON when saving, that's why it's wrapped in quotes.
So you have to treat this string as JSON object and parse it afterwards manually or using deserialization.

Difference between replaceCharacterInRange and stringByReplacingOccurrenceOfString

I m very confused with the string replacing methods of objective c.
Please tell where to use ReplaceCharacterInRange method and Where to use
stringByReplacingOccurrenceOfString Method.
These two methods differ a lot.
replaceStringWithCharactersInRange: withString:replaces all characters in the given range with the new string. It works on a NSMutableString and changes the string object you call it on.
In contrast stringByReplacingOccurrencesOfString:withString: replaces all occurrences of a given string, but returns a new string object. So it does work with immutable string as well.
So you use the first method if you want to keep your string but change parts of it while you use the second when you want to replace certain substrings within the string without changing the original string.

Adding quotes(single or double) to the json string

I have a string in json format but the string does not have quotes.
[{field1:value1,field2:value2}].
Now I have to convert this string into a ArrayNode using Jackson API. Because the string does not contain any quotes, it is throwing error that it is expecting double quotes. Also, I have already tried
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
but it is throwing error for values are not inside quotes.
You should add #JsonRawValue annotation to the fields which you want to take into consideration for parsing, so it will quotes that fields accordingly.