Convert Java Swagger Object to JSON / YAML
I need to read, modify and re-create the JAON / YAML documentation for swagger. I have deserialized a JSON file with Swagger Parser and I have a Swagger Java object with the original JSON data being displayed correctly.
Now I need to modify the Swagger Java object and generate a JSON or YAML file with the changes made.
Is there a way to do this?
Summary:
File fileJSON = FileUtils.toFile(getClass().getResource("example-api-rest.json"));
Swagger swagger = new SwaggerParser().read(fileJSON.getPath()); //Got it!
...
swagger.editWhatever
...
//Here I need to generate the JSON or YAML again
Thank.
+3
source to share
1 answer
To create JSON:
import io.swagger.util.Json;
String jsonOutput = Json.pretty(swagger);
To create YAML:
import io.swagger.util.Yaml;
String yamlOutput = Yaml.pretty().writeValueAsString(swagger);
The package io.swagger.util
is part of Swagger Core , which is one of the Swagger Parser dependencies.
+8
source to share