Jackson YAML Parser Deleted Special characters - jackson

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.

Related

Kotlin // Solve the example in a line

How to solve an example in a string?
Let's say we have val example : String = "3+5"
So how do I solve this example? Or if val example : String = "3*5/3"
Two ways to achieve it:
Keval - 3rd party dependency
You can either use Keval.eval("(3+4)(2/8 * 5) % PI") or as an extension function on String, "(3+4)(2/8 * 5) % PI".keval(). This will return the calculated value as Double. For your example, "3*5/3".keval().
To use it, add implementation("com.notkamui.libs:keval:0.8.0") in dependencies in app level build.gradle file and sync gradle. Then, use it in any file as mentioned in the above para, put the cursor on the line and press Alt + Enter (or hover for suggestions) to import the necessary imports.
Look into its Readme.md on the provided link for more usage and implementation details.
Custom String parsing using BODMAS rule
You can split the string using the BODMAS rule, parse the split array as int/double, if it throws exception, it means the substring is an expression too, again split it using BODMAS, parse and perform the calculation.

How to pass a string to a var without escape symbol in Kotlin?

I hope to pass the string {"name":"My Settings 1"} to a var aa
I have to write it using the code var aa=" {\"name\":\"My Settings 1\"} "
Is there a simple way in Kotlin when I use Android Studio 3.0 ?
I know <![CDATA[...]]> is good for XML content
The simplest thing you can do in Kotlin is use raw strings with triple quote marks:
val a = """{"name":"My Settings 1"}"""
For a tooling solution instead of a language solution (so this works both in Kotlin and Java), you can use language injection in Android Studio or IntelliJ.
Create a string, and invoke intention actions on it with Alt + Enter. Select Inject language or reference, and then JSON.
Use Alt + Enter again, and choose Edit JSON Fragment.
Edit the raw JSON in the panel that appears on the bottom, and IntelliJ will automatically mirror its contents into the string, escaping any characters that have to be escaped.
Escaping special chars in regular Strings, like in your example, is what has to be done with Java and also Kotlin:
"{\"name\":\"My Settings 1\"}"
Kotlin offers raw Strings to evade this. In these raw Strings there's no need to escape special chars, which shows the following:
"""{"name":"My Settings 1"}"""
Raw Strings are delimited by a triple quote (""").
Read the docs here.

Modify JsonNode of unknown JSON dynamically in Java

I am trying to modify a JSON (of an unknown structure) where the JsonPath and its equivalent XML Xpath is known to me.
I have tired using com.jayway.jsonpath.JsonPath library for the same.
The problem with JsonPath is, it returns me the value but I am not able to modify the Target Node.
Follows is my code snippet for the same
JsonPath.read(jsonFile, jsonPath);
JsonPath.parse(jsonPath);
System.out.println("Author: "+JsonPath.read(jsonFile, jsonPath));
I tried using Jackson as mentioned in previously asked quetion, But it needs to be traversed node by node as follows
((ObjectNode) parent).put(fieldName, newValue);
which I cannot do due to unknown structure.
I have tried the answer given to the question recursively parse JSON object but it says how to parse not modify
I need to do the follows
JsonNode root = mapper.readTree("Json in form of String");
((JsonNode)(root.get("JsonPath")).set("New Value");
Is there any way in which this can be achieved?
JsonNode objects are immutable so you can't modify them. What you can do is replace a JsonNode with another one. A cast to ObjectNode is also required to expose the required methods. First find the parent of the node you want to replace :
JsonNode node = root.findParent("JsonPath");
Then use either of these 2 methods to replace it with a new one:
((ObjectNode) node).remove("JsonPath"); // remove current node
((ObjectNode) node).put("JsonPath", "New Value"); // add new one with new value
or
((ObjectNode) node).replace("JsonPath", new TextNode("New Value"));

How to configure formatting rules in Google Dart Editor

I want configure the Dart Editor for the formatting a such source code without a breaking the first line on parameter list.
Result of formatting.
String _expand(
List fragments, Map<String, MacroDefinition> definitions, String defaultValue, Set<String> processed) {
//
}
Is this possible in Dart Editor?
String _expand(List fragments, Map<String, MacroDefinition> definitions, String defaultValue,
Set<String> processed) {
//
}
P.S.
The above source code formatted with a 120 characters per line.
You can't configure anything but the line length in DartFormat. It was a deliberate decision to not support formats which deviate from the default format.

Can WCF accept JSON encoded using single quotes and non-quoted identifiers?

Is there a way that I can instruct WCF to accept JSON that is formatted using either single quotes (as opposed to double quotes):
{
'foo': 'bar'
}
Or using non-quoted identifiers like so:
{
foo: 'bar'
}
As it is, it seems like JSON will only be accepted if it is formatted like so:
{
"foo": "bar"
}
Using either of the first two example results in a 400 (bad request).
The first two examples are invalid JSON texts.
http://www.ietf.org/rfc/rfc4627.txt
object = begin-object [ member *( value-separator member ) ]
end-object
member = string name-separator value
string = quotation-mark *char quotation-mark
quotation-mark = %x22 ; "
DataContractJsonSerializer always writes strict JSON.
At various points during deserialization (generally missing end tags for arrays or objects, or improper escaping, or improperly formatted numbers), it will accept incorrect, non-strict JSON.
However, I can tell you definitively that this is not one of those cases. DataContractJsonSerializer always requires double-quoted strings for JSON.
Hope this helps!