JYaml -> Yaml Java implementation

JYaml -> Yaml Java implementation

JYaml supported data types:

  • Primitive data types and wrapper classes (int, java.lang.Integer)
  • JavaBean compatibles (support structure)
  • collection (sequence support)
    • List
    • Set
  • Map (map support)
  • Arrays(sequence支持)
  • BigInteger和BigDecimal
  • Date

JYaml Example:
The entity Java class file written yaml

//创建实体类
package com.richfit.example;

import java.util.List;

public class General {
    private String name;
    private String address;
    private List<String> family;
    //省略getter和setter
}
package com.richfit.example;

import org.ho.yaml.Yaml;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

public class demo {

    //为实体类赋值
    public static General set(){
        General general=new General();
        general.setName("许褚");
        general.setAddress("安徽亳州");
        List<String> family=new ArrayList<String>();
        family.add("父亲");
        family.add("母亲");
        general.setFamily(family);
        return general;
    }
    
    //将实体类直接写入yaml文件
    public static void main(String[] args) {
        File dumpfile = new File("E:\\workspace_java\\JYaml\\src\\com\\richfit\\example\\general_dump.yaml");
        General general=set();
        try {
            Yaml.dump(general, dumpfile);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Results are as follows:

--- !com.richfit.example.General
address: 安徽亳州
family: 
  - 父亲
  - 母亲
name: 许褚

The load yaml file to the target pojo

package com.richfit.example;
import org.ho.yaml.Yaml;
import java.io.File;
import java.io.FileNotFoundException;
public class Load {
    public static void main(String[] args) {
        General general= new General();
        try {
            general = (General) Yaml.load(new File("E:\\workspace_java\\JYaml\\src\\com\\richfit\\example\\general_dump.yaml"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println(general.getName()+" "+general.getAddress()+" "+general.getFamily());
    }
}

Note:
JYaml dependent from the following:

<!-- https://mvnrepository.com/artifact/org.jyaml/jyaml -->
<dependency>
    <groupId>org.jyaml</groupId>
    <artifactId>jyaml</artifactId>
    <version>1.3</version>
</dependency>

Hair may occur automatically import dependent jar package, the package can be downloaded self-introduction jar.

Streaming

package com.richfit.example;

import org.ho.yaml.Yaml;
import org.ho.yaml.YamlEncoder;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

public class Flow {
    public static void main(String[] args) {
        File dumpfile=new File("E:\\workspace_java\\JYaml\\src\\com\\richfit\\example\\general_dump.yaml");
        File dumpfile1=new File("E:\\workspace_java\\JYaml\\src\\com\\richfit\\example\\general1_dump.yaml");
        try {
            General general=(General) Yaml.loadType(dumpfile,General.class);
            YamlEncoder encoder=new YamlEncoder(new FileOutputStream(dumpfile1));
            for (int i=0;i<3;i++){
                general.setName(general.getName()+i);
                encoder.writeObject(general);
                encoder.flush();
            }
            encoder.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}

mongodb->pojo->yaml

public void read1(String name) {
        Query query = new Query(Criteria.where("name").is(name));//条件查询
        List<Book> books = this.mongoTemplate.find(query, Book.class);
        File dumpfile = new File("E:\\workspace_java\\JYaml\\src\\main\\java\\com\\richfit\\modules\\mongo\\test\\book.yaml");
        try {
            for (Book book : books) {
                Yaml.dump(book, dumpfile);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

Guess you like

Origin www.cnblogs.com/adderhuang/p/11524975.html