zoukankan      html  css  js  c++  java
  • jackson解析处理JSON

    package com.ruoyi.common.json;
    
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import com.fasterxml.jackson.core.JsonGenerationException;
    import com.fasterxml.jackson.core.JsonParseException;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.ObjectWriter;
    
    /**
     * JSON解析处理
     * 
     * @author ruoyi
     */
    public class JSON
    {
        public static final String DEFAULT_FAIL = ""Parse failed"";
        private static final ObjectMapper objectMapper = new ObjectMapper();
        private static final ObjectWriter objectWriter = objectMapper.writerWithDefaultPrettyPrinter();
    
        public static void marshal(File file, Object value) throws Exception
        {
            try
            {
                objectWriter.writeValue(file, value);
            }
            catch (JsonGenerationException e)
            {
                throw new Exception(e);
            }
            catch (JsonMappingException e)
            {
                throw new Exception(e);
            }
            catch (IOException e)
            {
                throw new Exception(e);
            }
        }
    
        public static void marshal(OutputStream os, Object value) throws Exception
        {
            try
            {
                objectWriter.writeValue(os, value);
            }
            catch (JsonGenerationException e)
            {
                throw new Exception(e);
            }
            catch (JsonMappingException e)
            {
                throw new Exception(e);
            }
            catch (IOException e)
            {
                throw new Exception(e);
            }
        }
    
        public static String marshal(Object value) throws Exception
        {
            try
            {
                return objectWriter.writeValueAsString(value);
            }
            catch (JsonGenerationException e)
            {
                throw new Exception(e);
            }
            catch (JsonMappingException e)
            {
                throw new Exception(e);
            }
            catch (IOException e)
            {
                throw new Exception(e);
            }
        }
    
        public static byte[] marshalBytes(Object value) throws Exception
        {
            try
            {
                return objectWriter.writeValueAsBytes(value);
            }
            catch (JsonGenerationException e)
            {
                throw new Exception(e);
            }
            catch (JsonMappingException e)
            {
                throw new Exception(e);
            }
            catch (IOException e)
            {
                throw new Exception(e);
            }
        }
    
        public static <T> T unmarshal(File file, Class<T> valueType) throws Exception
        {
            try
            {
                return objectMapper.readValue(file, valueType);
            }
            catch (JsonParseException e)
            {
                throw new Exception(e);
            }
            catch (JsonMappingException e)
            {
                throw new Exception(e);
            }
            catch (IOException e)
            {
                throw new Exception(e);
            }
        }
    
        public static <T> T unmarshal(InputStream is, Class<T> valueType) throws Exception
        {
            try
            {
                return objectMapper.readValue(is, valueType);
            }
            catch (JsonParseException e)
            {
                throw new Exception(e);
            }
            catch (JsonMappingException e)
            {
                throw new Exception(e);
            }
            catch (IOException e)
            {
                throw new Exception(e);
            }
        }
    
        public static <T> T unmarshal(String str, Class<T> valueType) throws Exception
        {
            try
            {
                return objectMapper.readValue(str, valueType);
            }
            catch (JsonParseException e)
            {
                throw new Exception(e);
            }
            catch (JsonMappingException e)
            {
                throw new Exception(e);
            }
            catch (IOException e)
            {
                throw new Exception(e);
            }
        }
    
        public static <T> T unmarshal(byte[] bytes, Class<T> valueType) throws Exception
        {
            try
            {
                if (bytes == null)
                {
                    bytes = new byte[0];
                }
                return objectMapper.readValue(bytes, 0, bytes.length, valueType);
            }
            catch (JsonParseException e)
            {
                throw new Exception(e);
            }
            catch (JsonMappingException e)
            {
                throw new Exception(e);
            }
            catch (IOException e)
            {
                throw new Exception(e);
            }
        }
    }
  • 相关阅读:
    VSTS知识整理 荣
    扯淡 荣
    我安装了SQL Server2005后,为什么在IIS的默认站点下面并没有发现Reports? 荣
    ERROR 32000 错误 荣
    vs2012程序打包部署下载InstallShield2015LimitedEdition的下载及安装打包整套教程
    微信小程序之保持登录状态即session不改变
    微信小程序如何调用API实现数据请求wx.request()
    改版kingsmotor.cn用到的参考网站
    第一个css+div网页(太弱智了)
    超级搞笑的笑话
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/11750333.html
Copyright © 2011-2022 走看看