com.github.jy2.di.utils.JsonMapper Maven / Gradle / Ivy
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.github.jy2.di.utils;
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonMapper {
static ObjectMapper mapper;
public static String map(Object obj) throws IOException {
if (JsonMapper.mapper == null) {
JsonMapper.mapper = new ObjectMapper();
}
return mapper.writeValueAsString(obj);
}
public static void map(Object obj, File file) throws IOException {
if (JsonMapper.mapper == null) {
JsonMapper.mapper = new ObjectMapper();
}
mapper.writeValue(file, obj);
}
public static T map(String json, Class clazz) throws IOException {
if (JsonMapper.mapper == null) {
JsonMapper.mapper = new ObjectMapper();
}
return JsonMapper.mapper.readValue(json, clazz);
}
public static T map(File jsonFile, Class clazz) throws IOException {
if (JsonMapper.mapper == null) {
JsonMapper.mapper = new ObjectMapper();
}
return JsonMapper.mapper.readValue(jsonFile, clazz);
}
public static String mapWithRuntimeException(Object obj) {
if (JsonMapper.mapper == null) {
JsonMapper.mapper = new ObjectMapper();
}
try {
return mapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new RuntimeException("Problem with serializing " + obj.getClass(), e);
}
}
public static void mapWithRuntimeException(Object obj, File file) {
if (JsonMapper.mapper == null) {
JsonMapper.mapper = new ObjectMapper();
}
try {
mapper.writeValue(file, obj);
} catch (IOException e) {
throw new RuntimeException("Problem with serializing " + obj.getClass() + " to file: " + file, e);
}
}
public static T mapWithRuntimeException(String json, Class clazz) {
if (JsonMapper.mapper == null) {
JsonMapper.mapper = new ObjectMapper();
}
try {
return JsonMapper.mapper.readValue(json, clazz);
} catch (IOException e) {
throw new RuntimeException("Problem with parsing " + clazz.getCanonicalName() + ": " + json, e);
}
}
public static T mapWithRuntimeException(String json, TypeReference typeReference) {
if (JsonMapper.mapper == null) {
JsonMapper.mapper = new ObjectMapper();
}
try {
return JsonMapper.mapper.readValue(json, typeReference);
} catch (IOException e) {
throw new RuntimeException("Problem with parsing " + typeReference.toString() + ": " + json, e);
}
}
public static T mapWithRuntimeException(File jsonFile, Class clazz) {
if (JsonMapper.mapper == null) {
JsonMapper.mapper = new ObjectMapper();
}
try {
return JsonMapper.mapper.readValue(jsonFile, clazz);
} catch (IOException e) {
throw new RuntimeException("Problem with parsing " + clazz.getCanonicalName() + ": " + jsonFile, e);
}
}
public static T mapWithRuntimeException(File jsonFile, TypeReference typeReference) {
if (JsonMapper.mapper == null) {
JsonMapper.mapper = new ObjectMapper();
}
try {
return JsonMapper.mapper.readValue(jsonFile, typeReference);
} catch (IOException e) {
throw new RuntimeException("Problem with parsing " + typeReference.toString() + ": " + jsonFile, e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy