依赖
< dependency>
< groupId> com. fasterxml. jackson. core< / groupId>
< artifactId> jackson- databind< / artifactId>
< version> 2.15 .1 < / version>
< / dependency>
# new ObjectMapper
< dependency>
< groupId> com. fasterxml. jackson. dataformat< / groupId>
< artifactId> jackson- dataformat- yaml< / artifactId>
< version> 2.14 .2 < / version>
< / dependency>
# new YAMLFactory ( )
< dependency>
< groupId> org. yaml< / groupId>
< artifactId> snakeyaml< / artifactId>
< version> 2.0 < / version>
< / dependency>
# new Yaml
yaml转map
yaml格式的字符串(Yaml)
public static Map< String, Object> yamlToMap2 ( String yamlStr) {
if ( StringUtils. isBlank ( yamlStr) ) {
return new HashMap< > ( ) ;
}
# 启用安全构造器
Yaml yaml = new Yaml ( new SafeConstructor ( new LoaderOptions ( ) ) ) ;
return yaml. load ( yamlStr) ;
}
@Test
public void testYamlToMap2 ( ) {
String str = "---\nkey1: value1\nkey2: value2\n...\n" ;
Map< String, Object> map = YamlUtils. yamlToMap2 ( str) ;
System. out . println ( map) ;
Assert. assertEquals ( 2 , map. size ( ) ) ;
Assert. assertEquals ( "{key1=value1, key2=value2}" , map. toString ( ) ) ;
}
yaml格式的字符串(ObjectMapper)
public static Map < String , Object > yamlToMap ( String yamlStr) {
if ( StringUtils . isBlank ( yamlStr) ) {
return new HashMap < > ( ) ;
}
Map map = new HashMap < > ( ) ;
try {
ObjectMapper objectMapper = new ObjectMapper ( new YAMLFactory ( ) ) ;
map = objectMapper. readValue ( yamlStr, Map . class ) ;
} catch ( JsonProcessingException e) {
log. info ( "yamlToMap occur error {}" , e. getMessage ( ) ) ;
}
return map;
}
@Test
public void testYamlToMap ( ) {
String str = "---\nkey1: value1\nkey2: value2\n...\n" ;
Map < String , Object > map = YamlUtils . yamlToMap ( str) ;
Assert . assertEquals ( 2 , map. size ( ) ) ;
Assert . assertEquals ( "{key1=value1, key2=value2}" , map. toString ( ) ) ;
}
已知yaml文件的stream(ObjectMapper)
public static Map < String , Object > yamlStreamToMap ( InputStream yamlInputStream) {
if ( Objects . isNull ( yamlInputStream) ) {
return new HashMap < > ( ) ;
}
Map < String , Object > map = new HashMap < > ( ) ;
try {
map = objectMapper. readValue ( yamlInputStream, Map . class ) ;
} catch ( IOException e) {
log. info ( "yamlStreamToMap occur error {}" , e. getMessage ( ) ) ;
}
return map;
}
已知yaml文件的路径(ObjectMapper)
public static Map < String , Object > yamlFileToMap ( String yamlFilePath) {
FileIUtils . checkFileExist ( yamlFilePath) ;
Map < String , Object > map = new HashMap < > ( ) ;
try ( FileInputStream inputStream = new FileInputStream ( yamlFilePath) ) {
map = yamlStreamToMap ( inputStream) ;
} catch ( IOException e) {
log. info ( "yamlFileToMap occur error {}" , e. getMessage ( ) ) ;
}
return map;
}
已知yaml文件的File对象(ObjectMapper)
public static Map < String , Object > yamlFileToMap ( File yamlFile) {
Map < String , Object > map = new HashMap < > ( ) ;
try ( FileInputStream inputStream = FileUtils . openInputStream ( yamlFile) ) {
map = yamlStreamToMap ( inputStream) ;
} catch ( IOException e) {
log. info ( "yamlFileToMap occur error {}" , e. getMessage ( ) ) ;
}
return map;
}
map转yaml
public static String mapToYaml ( Map < String , Object > map) {
if ( MapUtils . isEmpty ( map) ) {
return EMPTY ;
}
DumperOptions dumperOptions = new DumperOptions ( ) ;
dumperOptions. setExplicitStart ( true ) ;
dumperOptions. setExplicitEnd ( true ) ;
Yaml yaml = new Yaml ( new SafeConstructor ( new LoaderOptions ( ) ) , new Representer ( dumperOptions) , dumperOptions) ;
return yaml. dumpAsMap ( map) ;
}
@Test
public void testMapToYaml ( ) {
Map < String , Object > map = new HashMap < > ( ) ;
map. put ( "key1" , "value1" ) ;
map. put ( "key2" , "value2" ) ;
String resStr = YamlUtils . mapToYaml ( map) ;
Assert . assertEquals ( "---\nkey1: value1\nkey2: value2\n...\n" , resStr) ;
}