Java 操作yaml和json之间的互相转换

需求:
Java 操作,通过标准的json格式,生成yaml文件,然后再读取yaml文件修改里面的某(多)个变量值(gte,lte,interval,min,max),然后再生成新的json,json格式前后都一样,就是为了修改里面的某些变量值,所以用yaml作为中间桥梁

通过yaml生成的json格式:

{
    "size": 0,
    "query": {
        "constant_score": {
            "filter": {
                "range": {
                    "last_updated": { "gte": 1497283200000, "lte": 1497928996980 } }
            }
        }
    },
    "aggs": {
        "by_time": {
            "date_histogram": {
                "field": "last_updated",
                "interval": "21526566ms",
                "extended_bounds": {
                    "min": "1497283200000",
                    "max": "1497928996980" }
            },
            "aggs": {
                "event_status_group": {
                    "filters": { "filters": { "info": { "match_phrase": { "alert_status": "info" } }, "warning": { "match_phrase": { "alert_status": "warning" } }, "error": { "match_phrase": { "alert_status": "error" } }, "success": { "match_phrase": { "alert_status": "success" } } } } }
            }
        }
    }
}

生成的yaml格式:

size: 0
query:
  constant_score:
    filter:
      range:
        last_updated: {gte: 1497283200000, lte: 1497928996980}
aggs:
  by_time:
    date_histogram:
      field: last_updated
      interval: 21526566ms
      extended_bounds: {min: '1497283200000', max: '1497928996980'}
    aggs:
      event_status_group:
        filters:
          filters:
            info:
              match_phrase: {alert_status: info}
            warning:
              match_phrase: {alert_status: warning}
            error:
              match_phrase: {alert_status: error}
            success:
              match_phrase: {alert_status: success}

在控制台打印出来的yaml格式是这种,但是写入到yaml文件的格式是上面那种(可能是语法问题),但是两种yaml格式生成的json都是一样的

size: 0
query:
  constant_score:
    filter:
      range:
        last_updated:
          gte: 1234567899999
          lte: 1111111111111
aggs:
  by_time:
    date_histogram:
      field: "last_updated"
      interval: "21526566ms"
      extended_bounds:
        min: "1497283200000"
        max: "1497928996980"
    aggs:
      event_status_group:
        filters:
          filters:
            info:
              match_phrase:
                alert_status: "info"
            warning:
              match_phrase:
                alert_status: "warning"
            error:
              match_phrase:
                alert_status: "error"
            success:
              match_phrase:
                alert_status: "success"

工具类和main函数

package com.mdl.yaml;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;

import org.yaml.snakeyaml.Yaml;

import com.esotericsoftware.yamlbeans.YamlException;
import com.esotericsoftware.yamlbeans.YamlReader;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
import com.google.gson.Gson;
import com.mdl.yaml.bean.Aggs;
import com.mdl.yaml.bean.Query;

public class TestYaml {

    public static void main(String[] args) {

//      readJsonAndCreateYaml("conf/param.json","conf/patam123.yaml");

//      yamlToJson("conf/file.yaml");

        String result = getJsonParam("conf/file.yaml", 111111, 22222222, "333333", "4444444", "1000");

        System.out.println("\n" + "所有数据修改之后--1" + "\n" + result);

    }


    /**
     * 獲取并設置yaml的值
     * 
     * @param url
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static String getJsonParam(String yaml_url, long lte, long gte, String max, String min, String interval) {
        Map map = returnMapFromYaml(yaml_url);

        Gson gs = new Gson();
        // 设置最后一次更新的起止时间
        String queryStr = gs.toJson((Map) map.get("query"));
        // size的默认为0
        map.put("size", 0);
        Query q = (Query) gs.fromJson(queryStr, Query.class);
        q.getConstant_score().getFilter().getRange().getLast_updated().setLte(lte);
        q.getConstant_score().getFilter().getRange().getLast_updated().setGte(gte);
        ((Map) ((Map) map).get("query")).put("constant_score", q.getConstant_score());

        // 设置bounds的最大最小时间
        String aggstr = gs.toJson((Map) map.get("aggs"));
        Aggs a = (Aggs) gs.fromJson(aggstr, Aggs.class);
        a.getBy_time().getDate_histogram().getExtended_bounds().setMax(max);
        a.getBy_time().getDate_histogram().getExtended_bounds().setMin(min);
        a.getBy_time().getDate_histogram().setInterval(interval);
        ((Map) ((Map) ((Map) map).get("aggs")).get("by_time")).put("date_histogram",
                a.getBy_time().getDate_histogram());

        return gs.toJson(map);
    }

    /**
     * 讀取json并生成yaml
     */
    public static void readJsonAndCreateYaml(String json_url,String yaml_url) {
        try {
            String param = readJson(json_url);
            createYaml(yaml_url,param);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 將json轉化為yaml格式并生成yaml文件
     * @param jsonString
     * @return
     * @throws JsonProcessingException
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public static void createYaml(String yaml_url,String jsonString) throws JsonProcessingException, IOException {
        // parse JSON
        JsonNode jsonNodeTree = new ObjectMapper().readTree(jsonString);
        // save it as YAML
        String jsonAsYaml = new YAMLMapper().writeValueAsString(jsonNodeTree);

        Yaml yaml = new Yaml();
        Map map = (Map) yaml.load(jsonAsYaml);

        createYamlFile(yaml_url, map);
    }

    /**
     * 将数据写入yaml文件
     * @param url yaml文件路径
     * @param data 需要写入的数据
     */
    public static void createYamlFile(String url,Map data){
        Yaml yaml = new Yaml();
        FileWriter writer;
        try {
            writer = new FileWriter(url);
            yaml.dump(data, writer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 讀取json文件并返回字符串
     * 
     * @param url
     * @return
     * @throws Exception
     */
    @SuppressWarnings("resource")
    public static String readJson(String url) throws Exception {
        File file = new File(url);
        FileReader fileReader = new FileReader(file);
        BufferedReader bufReader = new BufferedReader(fileReader);
        String message = new String();
        String line = null;
        while ((line = bufReader.readLine()) != null) {
            message += line;
        }
        return message;
    }

    /**
     * 方法一
     * 
     * 讀取yaml生成Map并返回
     * 
     * @param file
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Map yamlToMap(String yaml_url) {
        Map loaded = null;
        try {
            FileInputStream fis = new FileInputStream(yaml_url);
            Yaml yaml = new Yaml();
            loaded = (Map) yaml.load(fis);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        return loaded;
    }

    /**
     * 方法二
     * 
     * 读取yaml的内容并转为map
     * @param url
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static Map returnMapFromYaml(String yaml_url) {
        YamlReader reader;
        Object object = null;
        Map map = null;
        try {
            reader = new YamlReader(new FileReader(yaml_url));
            object = reader.read();
            map = (Map) object;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (YamlException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 讀取yaml生成json并返回
     * 
     * @param file
     * @return
     */
    @SuppressWarnings("unchecked")
    public static String yamlToJson(String file) {
        Gson gs = new Gson();
        Map loaded = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            Yaml yaml = new Yaml();
            loaded = (Map) yaml.load(fis);
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        }
        return gs.toJson(loaded);
    }



//  /**
//   * demo
//   * 生成yaml格式的string,并寫入yaml文件
//   */
//  public static void testDumpWriterforyaml() {
//      Map data = new HashMap();
//      data.put("name", "Silenthand Olleander");
//      data.put("race", "Human");
//      data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
//
//      Yaml yaml = new Yaml();
//      FileWriter writer;
//      try {
//          writer = new FileWriter("conf/param123.yaml");
//          yaml.dump(data, writer);
//      } catch (IOException e) {
//          e.printStackTrace();
//      }
//  }
//
//  /**
//   * 打印生成yaml格式的string
//   */
//  public static void testDump() {
//      Map data = new HashMap();
//      data.put("name", "Silenthand Olleander");
//      data.put("race", "Human");
//      data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
//      Yaml yaml = new Yaml();
//      String output = yaml.dump(data);
//      System.out.println(output);
//  }
//  
//  /**
//   * 打印生成yaml格式的string
//   */
//  public static void printDumpWriter() {
//      Map data = new HashMap();
//      data.put("name", "Silenthand Olleander");
//      data.put("race", "Human");
//      data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
//      Yaml yaml = new Yaml();
//      StringWriter writer = new StringWriter();
//      yaml.dump(data, writer);
//      System.out.println(writer.toString());
//  }
}

Aggs.java的实体

package com.mdl.yaml.bean;

public class Aggs {

    private ByTime by_time;

    public ByTime getBy_time() {
        return by_time;
    }

    public void setBy_time(ByTime by_time) {
        this.by_time = by_time;
    }


    public class ByTime{
        private DateHistogram date_histogram;

        public DateHistogram getDate_histogram() {
            return date_histogram;
        }

        public void setDate_histogram(DateHistogram date_histogram) {
            this.date_histogram = date_histogram;
        }

    }


    public class DateHistogram {
        private String field;
        private String interval;
        private ExtendedBounds extended_bounds;

        public String getField() {
            return field;
        }

        public void setField(String field) {
            this.field = field;
        }

        public String getInterval() {
            return interval;
        }

        public void setInterval(String interval) {
            this.interval = interval;
        }

        public ExtendedBounds getExtended_bounds() {
            return extended_bounds;
        }

        public void setExtended_bounds(ExtendedBounds extended_bounds) {
            this.extended_bounds = extended_bounds;
        }

    }

    public class ExtendedBounds{
        private String min;
        private String max;

        public String getMin() {
            return min;
        }
        public void setMin(String min) {
            this.min = min;
        }
        public String getMax() {
            return max;
        }
        public void setMax(String max) {
            this.max = max;
        }
    }


}

Query.java的实体

package com.mdl.yaml.bean;

public class Query {

    private ConstantScore constant_score;

    public ConstantScore getConstant_score() {
        return constant_score;
    }

    public void setConstant_score(ConstantScore constant_score) {
        this.constant_score = constant_score;
    }


    public class ConstantScore{
        private Filter filter;

        public Filter getFilter() {
            return filter;
        }

        public void setFilter(Filter filter) {
            this.filter = filter;
        }
    }

    public class Filter{
        private Range range;

        public Range getRange() {
            return range;
        }

        public void setRange(Range range) {
            this.range = range;
        }
    }

    public class Range{
        private LastUpdated last_updated;

        public LastUpdated getLast_updated() {
            return last_updated;
        }

        public void setLast_updated(LastUpdated last_updated) {
            this.last_updated = last_updated;
        }
    }

    public class LastUpdated{
        private long gte;
        private long lte;
        public long getGte() {
            return gte;
        }
        public void setGte(long gte) {
            this.gte = gte;
        }
        public long getLte() {
            return lte;
        }
        public void setLte(long lte) {
            this.lte = lte;
        }
    }
}

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>me.elegroupId>
    <artifactId>elastic.util.giraffeartifactId>
    <version>1.0.9-SNAPSHOTversion>
    <packaging>jarpackaging>

    <name>elastic.util.giraffename>

    <properties>
        
        <java-version>1.8java-version>
        
        <elasticsearch-version>2.3.0elasticsearch-version>
        
        
        <spring-framework.version>4.2.1.RELEASEspring-framework.version>
    properties>

    <dependencies>
        <dependency>
             <groupId>org.elasticsearchgroupId>
            <artifactId>elasticsearchartifactId>
            <version>${elasticsearch-version}version>
        dependency>
        <dependency>
           <groupId>org.slf4jgroupId>
           <artifactId>slf4j-apiartifactId>
           <version>1.7.5version>
        dependency>
        <dependency>
           <groupId>org.slf4jgroupId>
           <artifactId>slf4j-simpleartifactId>
           <version>1.6.4version>
        dependency>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>RELEASEversion>
            <scope>testscope>
        dependency>


        <dependency>
            <groupId>org.yamlgroupId>
            <artifactId>snakeyamlartifactId>
            <version>1.18version>
        dependency>

        <dependency>  
            <groupId>com.esotericsoftware.yamlbeansgroupId>  
            <artifactId>yamlbeansartifactId>  
            <version>1.08version>  
        dependency>  


        <dependency>
            <groupId>com.fasterxml.jackson.dataformatgroupId>
            <artifactId>jackson-dataformat-yamlartifactId>
            <version>2.3.0version>
        dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.2.3version>
        dependency>

        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
            <version>3.4version>
        dependency>

        
        <dependency>
            <groupId>com.google.code.gsongroupId>
            <artifactId>gsonartifactId>
            <version>2.7version>
        dependency>

        <dependency>
            <groupId>com.fasterxml.jackson.dataformatgroupId>
            <artifactId>jackson-dataformat-yamlartifactId>
            <version>2.9.0.pr4version>
        dependency>

    dependencies>
    <build>
        <plugins>

            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-source-pluginartifactId>
                <executions>
                    <execution>
                        <id>attach-sourcesid>
                        <goals>
                            <goal>jargoal>
                        goals>
                    execution>
                executions>
            plugin>

            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>2.3version>
                <configuration>
                    <source>${java-version}source>
                    <target>${java-version}target>
                    <encoding>UTF-8encoding>
                    <compilerArgument>-Xlint:allcompilerArgument>
                    <compilerArguments>
                        <parameters/>
                    compilerArguments>
                    <showWarnings>trueshowWarnings>
                    <showDeprecation>trueshowDeprecation>
                configuration>
            plugin>

        plugins>
    build>



    <distributionManagement>
        <repository>
            <id>ele-bpm-releaseid>
            <url>http://maven.dev.elenet.me/nexus/content/repositories/ele-bpm-release/url>
        repository>
        <snapshotRepository>
            <id>ele-bpm-snapshotsid>
            <url>http://maven.dev.elenet.me/nexus/content/repositories/ele-bpm-snapshots/url>
        snapshotRepository>
    distributionManagement>


project>

你可能感兴趣的:(yaml,java)