使用snakeyaml将JSON字符串转化为yaml格式字符串

2,383 阅读2分钟

1. 参考网址

snakeyaml官方网站:bitbucket.org/asomov/snak…

2. 本次需求

工作中需要将一个JSON转化为yaml格式的字符串,并存储为文件以.flow格式结尾(Azkaban flow 2.0文件,其内格式为yaml),因此考虑使用snakeyaml将JSON字符串转化为yaml字符串并将其优化为工具类,方便后续遇到相同问题可直接使用。

3. 测试数据

3.1 测试JSON字符串

{

  "nodes": [

    {

      "name": "tesA",

      "type": "command",

      "config": {

        "command": "echo \"tesA\""

      }

    },

    {

      "name": "testB",

      "type": "command",

      "config": {

        "command": "echo \"tesB\""

      },

      "dependsOn": [

        "tesA"

      ]

    },

    {

      "name": "testC",

      "type": "command",

      "config": {

        "command": "echo \"tesC\""

      },

      "dependsOn": [

        "testB"

      ]

    }

  ]

}

3.2 预期yaml格式字符串

yml格式化工具类:[YAML 格式化工具 | 345Tool.com](https://www.345tool.com/zh-hans/formatter/yaml-formatter)

#command.job

nodes:

  - name: tesA

    type: command

    config:

      command: echo "tesA"


  - name: testB

    type: command

    config:

      command: echo "tesB"

    dependsOn:

      - tesA


  - name: testC

    type: command

    config:

      command: echo "tesC"

    dependsOn:

      - testB

image.png

4.编写JSON字符串转yaml字符串工具类

4.1 Maven引入依赖

<!--fastjson-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.70</version>
</dependency>
<dependency>
    <groupId>org.yaml</groupId>
    <artifactId>snakeyaml</artifactId>
    <version>1.25</version>
</dependency>

4.2 工具类编写

package com.ahsj.base.utils;

import org.yaml.snakeyaml.Yaml;

import java.util.Map;

/**
 * @Description: Yaml工具类
 * @Author Z先生
 * @Date 2021/10/12 8:54
 */
public class YamlUtil {

    public static void main(String[] args) {
        String jsonStr = "{"nodes":[{"name":"tesA","type":"command","config":{"command":"echo \"tesA\""}},{"name":"testB","type":"command","config":{"command":"echo \"tesB\""},"dependsOn":["tesA"]},{"name":"testC","type":"command","config":{"command":"echo \"tesC\""},"dependsOn":["testB"]}]}";
        String yamlStr = JsonToYamlStr(jsonStr);
        System.out.println(yamlStr);
    }
    /**
     * JSON字符串转化为yaml格式字符串
     * @param jsonStr
     * @return
     */
    public static String JsonToYamlStr(String jsonStr){
        Yaml yaml = new Yaml();
        // 将JSON字符串转化为map
        Map<String,Object> map = yaml.load(jsonStr);
        //转换成 YAML 字符串
        String yamlStr = yaml.dumpAsMap(map);
        return yamlStr;
    }
}

测试输出结果

image.png

该方式先将 JSON 字符串转通过 load 方法转成 map,再转成 YAML 格式

5.写入.flow文件

使用FileWriter写入

package com.ahsj.base.utils;

import java.io.FileWriter;
import java.io.IOException;

/**
 * @Description: 文件工具类
 * @Author Z先生
 * @Date 2021/10/12 9:31
 */
public class FileUtil {

    public static void main(String[] args) {
        String str = "nodes:\n" +
                "- name: tesA\n" +
                "  type: command\n" +
                "  config:\n" +
                "    command: echo "tesA"\n" +
                "- name: testB\n" +
                "  type: command\n" +
                "  config:\n" +
                "    command: echo "tesB"\n" +
                "  dependsOn:\n" +
                "  - tesA\n" +
                "- name: testC\n" +
                "  type: command\n" +
                "  config:\n" +
                "    command: echo "tesC"\n" +
                "  dependsOn:\n" +
                "  - testB";
        String path = strWriteToFile(str);
        System.out.println("文件地址:" + path);
    }

    /**
     * 字符串写入文件
     * @param str
     * @return
     */
    public static String strWriteToFile(String str){
        String path = "D:/test.flow";
        try {
            FileWriter writer = new FileWriter(path);
            writer.write("");//清空原文件内容
            writer.write(str);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("文件写入成功!");
        return path;
    }
}

写入结果

image.png

文件内容查看

image.png

转化成功!