123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- package com.sooka.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 lei_wang
- */
- 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);
- }
- }
- }
|