irpas技术客

Yaml转Json_JAVA序码

未知 3820

缘起

年前,因为项目需要进行配置的优化和架构的升级,领导给我来了个任务,让我去进行技术调研

需要将配置中心的yaml配置文件里面的配置转为Json的形式,以便后面可以通过写配置文件的方式,让数据源变的可拔插

调研过程

本着有轮子就直接用,绝不自己造轮子的态度,我成功找到了 snakeyaml 这个依赖

<dependency> ?<groupId>org.yaml</groupId> ?<artifactId>snakeyaml</artifactId> ?<version>1.30</version> </dependency> 复制代码 1.0 入口点 Yaml yaml = new Yaml(); 复制代码

注意📢:此实现并不是一个线程安全的

1.1 基本用法

yml 文件:

common.cloud: discovery: consul config: consul 复制代码

Java 代码:

? ?@Test ? ?public void YAMLToJSON01() { ? ? ? ?Yaml yaml = new Yaml(); ? ? ? ?InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("yamlToJson.yml"); ? ? ? ?// 解析唯一的 yaml 文档转为 Java 对象 ? ? ? ?Map<String, Object> load = yaml.load(resourceAsStream); ? ? ? ?JSONObject jsonObject = JSONObject.parseObject(load); ? ? ? ?System.out.println(jsonObject.toString()); ? } 复制代码

结果

{"common.cloud":{"discovery":"consul","config":"consul"}} 复制代码 1.2 自定义类型解析

yml 文件:

cloud: ? discovery: consul ? config: consul extension: ? - data: application-oracle-public.yml ? ? refresh: true 复制代码

Java 代码:

首先是创建对应的实体类

@Data public class Cloud { ? ?private Contact cloud; ? ?private List<Contact02> extension; } @Data public class Contact { ? ?private String discovery; ? ?private String config; } @Data public class Contact02 { ? ?private String data; ? ?private String refresh; } 复制代码 ? ?@Test ? ?public void YAMLToJSON02() { ? ? ? ?Yaml yaml = new Yaml(new Constructor(Cloud.class)); ? ? ? ?InputStream resourceAsStream = this.getClass() ? ? ? ? ? ? ? .getClassLoader() ? ? ? ? ? ? ? .getResourceAsStream("yamlToJson.yml"); ? ? ? ?// 解析 yaml 文档转为 Cloud 对象 ? ? ? ?Cloud cloud = yaml.load(resourceAsStream); ? ? ? ?JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(cloud)); ? ? ? ?System.out.println(jsonObject.toString()); ? } 复制代码

结果

{"cloud":{"discovery":"consul","config":"consul"},"extension":[{"data":"application-oracle-public.yml","refresh":"true"}]} 复制代码 1.3 实战

1.3.1 从本地读配置文件

在项目中,假如领导只需要 spring.cloud.nacos 这一块的配置内容,但我们把整个配置文件的内容全部取出来了,那肯定是不符合领导需求的

于是就有了接下来的一些代码,一些关键的注释也标注在了代码中

yml 文件:

spring.cloud.nacos: discovery: ? server-addr: xxx config: ? server-addr: xxx ? file-extension: yml ? extension-configs: ? ? - data-id: application-oracle-public.yml ? ? ? refresh: true eureka: instance: ? preferIpAddress: true ? instanceId: ${spring.application.name}-${spring.cloud.client.ip-address}-${server.port} client: ? serviceUrl: ? ? defaultZone: http://xxx/eureka/ 复制代码

Java 代码:

? ?@Test ? ?public void YAMLToJSON01() { ? ? ? ?Yaml yaml = new Yaml(); ? ? ? ?InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("bootstrap.yml"); ? ? ? ?// 解析 yaml 文档转为 Map ? ? ? ?Map<String, Object> load = yaml.load(resourceAsStream); ? ? ? ?HashMap<String, Object> objectObjectHashMap = new HashMap<>(); ? ? ? ?// 通过 key 取到对应的 value 然后放到新的 Map 中 ? ? ? ?objectObjectHashMap.put("spring.cloud.nacos", load.get("spring.cloud.nacos")); ? ? ? ?JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(objectObjectHashMap)); ? ? ? ?System.out.println(jsonObject.toString()); ? } 复制代码

结果

{"spring.cloud.nacos":{"discovery":{"server-addr":"xxx"},"config":{"file-extension":"yml","server-addr":"xxx","extension-configs":[{"refresh":true,"data-id":"application-oracle-public.yml"}]}}} 复制代码

下面的代码是使用静态内部类的方式去保证线程的安全

public class YamlToJsonUtil implements Serializable { ? ?private final static String BOOTSTRAP = "bootstrap-test.yml"; ? ? ?private final Yaml yaml = new Yaml(new SafeConstructor()); ? ? ?/** ? ? * Yaml to json. ? ? * ? ? * @param key the key ? ? * @return the json object ? ? */ ? ?public JSONObject YamlToJson(String key) { ? ? ? ?YamlToJsonUtil instance = YamlToJsonInner.instance; ? ? ? ?Yaml yaml = instance.getYaml(); ? ? ? ?InputStream resourceAsStream = null; ? ? ? ?try { ? ? ? ? ? ?resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(BOOTSTRAP); ? ? ? } catch (Exception e) { ? ? ? ? ? ?throw new RuntimeException(e); ? ? ? } ? ? ? ?Map<String, Object> load = null; ? ? ? ?Map<String, Object> result = new HashMap<>(); ? ? ? ?load = yaml.load(resourceAsStream); ? ? ? ?result.put(key, load.get(key)); ? ? ? ?return JSONObject.parseObject(JSONObject.toJSONString(result)); ? } ? ? ?private YamlToJsonUtil() { ? ? } ? ? ?/** ? ? * Gets instance. ? ? * ? ? * @return the instance ? ? */ ? ?public static YamlToJsonUtil getInstance() { ? ? ? ?return YamlToJsonInner.instance; ? } ? ? ?private static class YamlToJsonInner { ? ? ? ?private static final YamlToJsonUtil instance = new YamlToJsonUtil(); ? } ? ? ?/** ? ? * Gets yaml. ? ? * ? ? * @return the yaml ? ? */ ? ?public Yaml getYaml() { ? ? ? ?return yaml; ? } } 复制代码 @Test public void YAMLToJSON03() { ? ?String key = "spring.cloud.nacos"; ? ?YamlToJsonUtil yamlToJsonUtil = YamlToJsonUtil.getInstance(); ? ?JSONObject jsonObject = yamlToJsonUtil.YamlToJson(key); ? ?System.out.println(jsonObject); } 复制代码

到了这一步领导的需求基本已经完成了一大半,接下来只需要再从配置中心中将这些配置取出来就可以摸鱼下班了

那么问题来了:该如何从配置中心中找到那一份配置文件呢?

就在这时,我想起来了 Spring 里面的 org.springframework.core.env.PropertySource 这个类。 org.springframework.core.env.PropertySource 这一个类顾名思义就是属性源的意思,再具体一点就是对获取键值对资源的抽象类。我们可以从org.springframework.core.env.AbstractEnvironment#getPropertySources 这个方法中得到配置中心里面的配置以及环境配置,再通过过滤即可得到我们想到的数据

(ps:上面提到的这个类以及对应的方法大家可以自行百度,因为篇幅等原因就不在这里详解了)

1.3.2 从配置中心读配置文件

接下来直接看代码:

public Map<String, Object> getPropertyMap(Environment environment) { // 得到当前环境中所有的属性集合 List<?> propertySourceCollection = ((StandardEnvironment) environment) .getPropertySources().stream() .map(PropertySource::getSource) .collect(Collectors.toList()); List<LinkedHashMap<String, Object>> list = new ArrayList<>(); for (Object source : propertySourceCollection) { // 判断属性类型是否为 LinkedHashMap if (source instanceof LinkedHashMap) { list.add((LinkedHashMap<String, Object>) source); } } // 将集合中多个 LinkedHashMap 合并为一个 Map return list.stream() .flatMap(map -> map.entrySet().stream()) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (value1, value2) -> value1)); } 复制代码

经过测试,上面的代码可以将配置中心中的所有配置都存储在一个 Map<String, Object> 中。

但是由于 yaml.load() 并没有入参为 Map<String, Object> 的重载方法,所以我们还得引入这个依赖

<dependency> <groupId>pl.jalokim.propertiestojson</groupId> <artifactId>java-properties-to-json</artifactId> <version>5.1.0</version> </dependency> 复制代码

这个依赖里面的方法可以将 Map 转为 properties,再转为 json,刚好满足我们的需求。

Map<String, Object> propertyMap = getPropertyMap(environment); // 将 Map 转为 Json String json = new PropertiesToJsonConverter().convertFromValuesAsObjectMap(propertyMap); 复制代码

直接上整个从配置中心读yaml配置文件里面的配置转为Json的全部代码

线程安全单例:

/** * The type Yaml to json util. */ public class YamlToJsonForCloudUtil implements Serializable { private final Yaml yaml = new Yaml(new SafeConstructor()); /** * Yaml to json. * * @param key the key of the configuration file in Consul or Nacos * @return the json object */ public JSONObject YamlToJson(String key) { YamlToJsonForCloudUtil instance = YamlToJsonInner.instance; Environment environment = instance.getEnvironment(); if (environment == null) { throw new RuntimeException("Environment 为空!"); } Map<String, Object> propertyMap = getPropertyMap(environment); // 将 Map 转为 Json String json = new PropertiesToJsonConverter().convertFromValuesAsObjectMap(propertyMap); Yaml yaml = instance.getYaml(); Map<String, Object> load = null; Map<String, Object> result = new HashMap<>(); // 解析 String 中唯一的 YAML 文档并转为 Map load = yaml.load(json); result.put(key, load.get(key)); return JSONObject.parseObject(JSONObject.toJSONString(result)); } /** * Gets property map. * * @param environment the environment * @return the property map */ public Map<String, Object > getPropertyMap(Environment environment) { // 得到当前环境中所有的属性集合 List<?> propertySourceCollection = ((StandardEnvironment) environment) .getPropertySources().stream() .map(PropertySource::getSource) .collect(Collectors.toList()); List<LinkedHashMap<String, Object>> list = new ArrayList<>(); for (Object source : propertySourceCollection) { // 判断属性类型是否为 LinkedHashMap if (source instanceof LinkedHashMap) { list.add((LinkedHashMap<String, Object>) source); } } // 将集合中多个 LinkedHashMap 合并为一个 Map return list.stream() .flatMap(map -> map.entrySet().stream()) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue, (value1, value2) -> value1)); } private YamlToJsonForCloudUtil() { } /** * Gets instance. * * @return the instance */ public static YamlToJsonForCloudUtil getInstance() { return YamlToJsonInner.instance; } private static class YamlToJsonInner { private static final YamlToJsonForCloudUtil instance = new YamlToJsonForCloudUtil(); } /** * Gets yaml. * * @return the yaml */ public Yaml getYaml() { return yaml; } /** * Gets environment. * * @return the environment */ public Environment getEnvironment() { return SpringContextUtil.getBean(Environment.class); } } 复制代码

工具类:

@Component public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; /** * 实现ApplicationContextAware接口的回调方法,设置上下文环境 */ @Override public void setApplicationContext(@NotNull ApplicationContext applicationContext){ SpringContextUtil.applicationContext = applicationContext; } /** * 获得spring上下文 * @return ApplicationContext spring上下文 */ public static ApplicationContext getApplicationContext(){ return applicationContext; } /** * 获取bean * @param name service注解方式name为小驼峰格式 * @return Object bean的实例对象 */ public static Object getBean(String name) throws BeansException { return getApplicationContext().getBean(name); } public static <T> T getBean(Class<T> className) { return getApplicationContext().getBean(className); } /** * 通过接口类型,返回所有实现了这个接口的实现类 * * @param clazz * @param <T> * @return */ public static <T> Map<String, T> getBeansOfType(Class<T> clazz) { return getApplicationContext().getBeansOfType(clazz); } } 复制代码

测试:

@Test public void YamlToJsonEnvironment() { YamlToJsonForCloudUtil yamlToJsonUtil = YamlToJsonForCloudUtil.getInstance(); JSONObject dom = yamlToJsonUtil.YamlToJson("dom"); System.out.println(JSONObject.toJSONString(dom)); } 复制代码 完结

谢谢各位的观看,小弟是个Java新手也是第一次更文,如果有更好的方法欢迎大家在评论区指出来,如果有什么建议也欢迎大家提出


1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,会注明原创字样,如未注明都非原创,如有侵权请联系删除!;3.作者投稿可能会经我们编辑修改或补充;4.本站不提供任何储存功能只提供收集或者投稿人的网盘链接。

标签: #yaml转json #解析 #yaml #文档转为 #Cloud #对象复制代码结果复制代码