Samples JDK
JSONParserUtil.java
1 /*
2  * Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013.
3  *
4  * This is an unpublished work, is confidential and proprietary to
5  * eMeter Corporation as a trade secret and is not to be used or
6  * disclosed except as may be provided in an applicable eMeter Corporation
7  * license agreement.
8  *
9  * U.S. Government Rights. The software is commercial computer software, and
10  * the accompanying documentation is commercial computer software documentation.
11  * The terms and conditions of eMeter Corporation's license agreement are fully
12  * applicable to the Government's use and disclosure of the software and
13  * documentation, and shall supersede any conflicting terms or conditions.
14  * No license of any kind is granted in the case of acquisitions which contain
15  * or are subject to the clauses FAR 52-227.19 COMMERCIAL COMPUTER
16  * SOFTWARE-RESTRICTED RIGHTS (JUNE 1987) or DFARS 252.227-7013 RIGHTS IN
17  * TECHNICAL DATA AND COMPUTER SOFTWARE (OCT 1988) or any other clause which
18  * purports to grant to the Government rights greater than, or additional to
19  * those, set forth in such license agreement, or which purports to impose
20  * additional requirements upon eMeter. If the license agreement fails to meet
21  * the Government's stated needs or is inconsistent in any respect with federal
22  * law, the Government agrees to return the software and documentation, unused,
23  * to eMeter. The Licensor/Manufacturer is eMeter Corporation,
24  * 4000 E. Third Ave., Fourth Floor Foster City, CA 94404 USA
25  */
26 package com.freemindcafe.serialization.sample3;
27 
28 import java.io.IOException;
29 import java.util.List;
30 
31 import org.codehaus.jackson.map.ObjectMapper;
32 import org.codehaus.jackson.map.type.TypeFactory;
33 
34 /**
35  * The Class JSONParserUtil.
36  *
37  * @author Abhinav Solan
38  */
39 public class JSONParserUtil {
40 
41  /**
42  * json to list of object
43  * @param json
44  * @param clazz
45  * @return the List<T>
46  */
47  @SuppressWarnings("unchecked")
48  public static <T extends Object> List<T> jsonToListOfObject(final String json, final Class<T> clazz) {
49  ObjectMapper mapper = new ObjectMapper();
50  try {
51  return (List<T>) mapper.readValue(json, TypeFactory.defaultInstance().constructCollectionType(List.class,
52  clazz));
53  } catch (final IOException e) {
54  throw new RuntimeException(e);
55  }
56  }
57 
58  /**
59  * json to object
60  * @param json
61  * @param clazz
62  * @return T
63  */
64  public static <T extends Object> T jsonToObject(final String json, final Class<T> clazz) {
65  ObjectMapper mapper = new ObjectMapper();
66  try {
67  return (T) mapper.readValue(json, clazz);
68  } catch (final IOException e) {
69  throw new RuntimeException(e);
70  }
71  }
72 
73  /**
74  * object to json
75  * @param t
76  * @param clazz
77  * @return json string
78  */
79  @SuppressWarnings("unchecked")
80  public static <T> String objectToJson(final Object t, final Class<T> clazz) {
81  ObjectMapper mapper = new ObjectMapper();
82  try {
83  return mapper.writeValueAsString((T) t);
84  } catch (final IOException e) {
85  throw new RuntimeException(e);
86  }
87  }
88 }
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)