Yaml转换list,json,map工具类

package com.ikcrm.lib.common.utils;

import java.util.List;
import java.util.Map;

import org.yaml.snakeyaml.Yaml;

import com.alibaba.fastjson.JSONObject;

/**
 * 
* @ClassName: YamlUtils
* @Description: TODO(Yaml转换工具类)
* @author ruxiang
* @date 2017年6月28日 下午1:00:56
*
 */
public class YamlUtils {
   
   
   /**
    * Yaml转json
    * @param yamlString
    * @return
    */
   
   @SuppressWarnings("unchecked")
   public static JSONObject convertToJson(String yamlString) {
       Yaml yaml= new Yaml();
       Map, Object> map = (Map, Object>) yaml.load(yamlString);
       JSONObject jsonObject=new JSONObject(map);
       return jsonObject;
   }
   
   /**
    * Yaml转Map
    * @param yamlString
    * @return
    */
   
   @SuppressWarnings("unchecked")
   public static Map,Object> convertToMap(String yamlString) {
      if(yamlString.startsWith("---\n")){
         yamlString = yamlString.replaceAll("---\n","");
      }if(yamlString.contains("!ruby/hash")){
         yamlString = yamlString.replaceAll("(?i)!ruby/.*\n","\n");
      }
       Yaml yaml= new Yaml();
      Map,Object> map= (Map, Object>) yaml.load(yamlString);
       return map;
   }
   
   /**
    * Yaml转Object
    * @param yamlString
    * @return
    */
   public static Object convertToObject(String yamlString) {
       Yaml yaml= new Yaml();
       Object obj = yaml.load(yamlString);
       return obj;
   }
   

   /**
    * Yaml转List
    * @param yamlString
    * @return
    */

   @SuppressWarnings("unchecked")
   public static List toList(String yamlString) {
      Yaml yaml= new Yaml();
      List list= (List) yaml.load(yamlString);
      return list;
   }

}

你可能感兴趣的:(java)