简体   繁体   English

将Swagger Java对象转换为JSON / YAML

[英]Convert Swagger Java Object into JSON/YAML

I need to read, modify and re-generate a JSON/YAML swagger file documentation. 我需要阅读,修改和重新生成JSON / YAML swagger文件文档。 I have deserialized a JSON file with Swagger Parser and I have a Swagger Java Object with the original JSON data mapped correctly. 我用Swagger Parser反序列化了一个JSON文件,我有一个Swagger Java对象,原始的JSON数据映射正确。

Now, I need to modify the Swagger Java object, and generate a JSON or YAML file with the done modifications. 现在,我需要修改Swagger Java对象,并生成带有完成修改的JSON或YAML文件。

Is there a way to do that? 有没有办法做到这一点?

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

Thanks. 谢谢。

To generate JSON: 要生成JSON:

import io.swagger.util.Json;

String jsonOutput = Json.pretty(swagger);

To generate YAML: 要生成YAML:

import io.swagger.util.Yaml;

String yamlOutput = Yaml.pretty().writeValueAsString(swagger);

The io.swagger.util package is part of Swagger Core , which is one of the dependencies of Swagger Parser. io.swagger.util包是Swagger Core的一部分, Swagger Core是Swagger Parser的依赖项之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM