-
-
Notifications
You must be signed in to change notification settings - Fork 52
JSON to or from YAML
If you have a javax.json.JsonObject
or javax.json.JsonArray
, you can easily create a YamlMapping
or YamlSequence
, by using the methods Yaml.fromJsonObject(...)
and Yaml.fromJsonArray(...)
respectively.
Assuming that variable jsonTeam
holds the following JSON:
{
"architect": "amihaiemil",
"devops": [
"rultor",
"0pdd"
],
"developers": [
"amihaiemil",
"salikjan",
"SherifWally"
]
}
final YamlMapping team = Yaml.fromJsonObject(jsonTeam);
System.out.println(team);
The following YAML will be printed:
architect: amihaiemil
devops:
- rultor
- 0pdd
developers:
- amihaiemil
- salikjan
- SherifWally
YamlMapping
offers the method toJsonObject()
which will turn it into a javax.json.JsonObject
.
- mappings containing keys which are not strings cannot be converted to JSON, because JSON does not support complex keys. You will get an exception if you'll try to convert a mapping which has non-scalar keys.
YamlSequence
offers the method toJsonArray()
which will turn it into a javax.json.JsonArray
.
YamlStream
offers the method toJsonArray()
which will turn it into a javax.json.JsonArray
.
YamlNode
offers the method toJsonValue()
which will turn it into a javax.json.JsonValue
.
Pay attention: to use this functionality, you will need to have an implementation of the JSON Pocessing (JSON-P)/JSR 374 Specification in your classpath (eo-yaml only offers the API of the spec, you need to add the implementation as dependency).