'Can't deserialize YAML string array with Jackson
I am trying to map a yaml file into my java object. To achieve this I'm using the jackson ObjectMapper with the YamlFactory.
private JsonNode extractContent(final String url) throws Exception {
ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
// gitlabHttpService.getAndDecodeFileData(url); -> gets decoded yaml file from gitlab repo as string
String yamlString = gitlabHttpService.getAndDecodeFileData(url);
JsonNode jsonNode = yamlMapper.readTree(yamlString);
return jsonNode;
}
The following code shows the input Yaml String which needs to be converted into a JsonNode or similar.
version: '2.0'
nlu:
- intent: Generell, wie melde ich mich an?
examples: |-
- [mycompany]{"entity":"category","value":"mycompany"} [hr]{"entity":"subcategory1","value":"hr"} [covid]{"entity":"subcategory2","value":"covid"} Wie kann ich mich anmelden?
- [mycompany]{"entity":"category","value":"mycompany"} [hr]{"entity":"subcategory1","value":"hr"} [covid]{"entity":"subcategory2","value":"covid"} Wo melde ich mich an?
- [mycompany]{"entity":"category","value":"mycompany"} [hr]{"entity":"subcategory1","value":"hr"} [covid]{"entity":"subcategory2","value":"covid"} Ich will mich anmelden
- [mycompany]{"entity":"category","value":"mycompany"} [hr]{"entity":"subcategory1","value":"hr"} [covid]{"entity":"subcategory2","value":"covid"} wie anmelden?
But unfortunatly the result looks like the following code snippet and it seems like the ObjectMapper can't identify the YamlArray correctly. It returns just a simple string instead of a list as the examples attribute.
{
"version":"2.0",
"nlu":[
{
"intent":"Generell, wie melde ich mich an?",
"examples":"- [mycompany]{\"entity\":\"category\",\"value\":\"postfinance\"} [hr]{\"entity\":\"subcategory1\",\"value\":\"hr\"} [covid]{\"entity\":\"subcategory2\",\"value\":\"covid\"} Wie kann ich mich anmelden?\n- [postfinance]{\"entity\":\"category\",\"value\":\"postfinance\"} [hr]{\"entity\":\"subcategory1\",\"value\":\"hr\"} [covid]{\"entity\":\"subcategory2\",\"value\":\"covid\"} Wo melde ich mich an?\n- [postfinance]{\"entity\":\"category\",\"value\":\"postfinance\"} [hr]{\"entity\":\"subcategory1\",\"value\":\"hr\"} [covid]{\"entity\":\"subcategory2\",\"value\":\"covid\"} Ich will mich anmelden\n- [postfinance]{\"entity\":\"category\",\"value\":\"postfinance\"} [hr]{\"entity\":\"subcategory1\",\"value\":\"hr\"} [covid]{\"entity\":\"subcategory2\",\"value\":\"covid\"} wie anmelden?"
}
]
}
The expected outcome should look like this:
{
"version":"2.0",
"nlu":[
{
"intent":"Generell, wie melde ich mich an?",
"examples":[
"[mycompany]{\"entity\":\"category\",\"value\":\"mycompany\"} [hr]{\"entity\":\"subcategory1\",\"value\":\"hr\"} [covid]{\"entity\":\"subcategory2\",\"value\":\"covid\"} Wie kann ich mich anmelden?\n",
"[mycompany]{\"entity\":\"category\",\"value\":\"mycompany\"} [hr]{\"entity\":\"subcategory1\",\"value\":\"hr\"} [covid]{\"entity\":\"subcategory2\",\"value\":\"covid\"} Wo melde ich mich an?\n",
"[mycompany]{\"entity\":\"category\",\"value\":\"mycompany\"} [hr]{\"entity\":\"subcategory1\",\"value\":\"hr\"} [covid]{\"entity\":\"subcategory2\",\"value\":\"covid\"} Ich will mich anmelden\n",
"[mycompany]{\"entity\":\"category\",\"value\":\"mycompany\"} [hr]{\"entity\":\"subcategory1\",\"value\":\"hr\"} [covid]{\"entity\":\"subcategory2\",\"value\":\"covid\"} wie anmelden?"
]
}
]
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
