e69e4b6ab379e501d26ba10b20484cef9582a6b9
[ccsdk/apps.git] / sdnr / wireless-transport / code-Carbon-SR1 / apps / route / impl / src / main / java / com / highstreet / technologies / odl / app / impl / tools / JsonUtil.java
1 /*
2  * Copyright © 2015 ZTE and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package com.highstreet.technologies.odl.app.impl.tools;
9
10 import com.fasterxml.jackson.databind.ObjectMapper;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13
14 import java.io.File;
15 import java.io.IOException;
16 import java.net.URL;
17 import java.text.SimpleDateFormat;
18
19 import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
20 import static com.fasterxml.jackson.databind.DeserializationFeature.READ_ENUMS_USING_TO_STRING;
21 import static com.fasterxml.jackson.databind.SerializationFeature.WRITE_ENUMS_USING_TO_STRING;
22
23 /**
24  * Created by olinchy on 6/18/14 for MO_JAVA.
25  */
26 public class JsonUtil
27 {
28     private static final Logger LOG = LoggerFactory.getLogger(JsonUtil.class);
29     private static ObjectMapper mapper;
30
31     static
32     {
33         mapper = new ObjectMapper();
34         mapper.configure(WRITE_ENUMS_USING_TO_STRING, true);
35         mapper.configure(FAIL_ON_UNKNOWN_PROPERTIES, false);
36         mapper.configure(READ_ENUMS_USING_TO_STRING, true);
37         mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
38     }
39
40     public static <T> T toObject(URL content, Class<T> clazz)
41     {
42         try
43         {
44             return mapper.readValue(content, clazz);
45         }
46         catch (IOException e)
47         {
48             LOG.warn("read value failed !", e);
49             return null;
50         }
51     }
52
53     public static <T> T toObject(File content, Class<T> clazz)
54     {
55         try
56         {
57             return mapper.readValue(content, clazz);
58         }
59         catch (IOException e)
60         {
61             LOG.warn("read value failed !", e);
62             return null;
63         }
64     }
65 }