26 package com.freemindcafe.serialization.sample3;
28 import java.io.IOException;
29 import java.util.List;
31 import org.codehaus.jackson.map.ObjectMapper;
32 import org.codehaus.jackson.map.type.TypeFactory;
47 @SuppressWarnings(
"unchecked")
48 public static <T extends Object> List<T>
jsonToListOfObject(final String json, final Class<T> clazz) {
49 ObjectMapper mapper =
new ObjectMapper();
51 return (List<T>) mapper.readValue(json, TypeFactory.defaultInstance().constructCollectionType(List.class,
53 }
catch (
final IOException e) {
54 throw new RuntimeException(e);
64 public static <T extends Object> T
jsonToObject(
final String json,
final Class<T> clazz) {
65 ObjectMapper mapper =
new ObjectMapper();
67 return (T) mapper.readValue(json, clazz);
68 }
catch (
final IOException e) {
69 throw new RuntimeException(e);
79 @SuppressWarnings(
"unchecked")
80 public static <T> String
objectToJson(final Object t, final Class<T> clazz) {
81 ObjectMapper mapper =
new ObjectMapper();
83 return mapper.writeValueAsString((T) t);
84 }
catch (
final IOException e) {
85 throw new RuntimeException(e);
static< T > String objectToJson(final Object t, final Class< T > clazz)
static< TextendsObject > T jsonToObject(final String json, final Class< T > clazz)
static< TextendsObject > List< T > jsonToListOfObject(final String json, final Class< T > clazz)