2 * Copyright © 2015 ZTE and others. All rights reserved.
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
8 package com.highstreet.technologies.odl.app.impl.tools;
10 import com.fasterxml.jackson.databind.ObjectMapper;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
15 import java.io.IOException;
17 import java.text.SimpleDateFormat;
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;
24 * Created by olinchy on 6/18/14 for MO_JAVA.
28 private static final Logger LOG = LoggerFactory.getLogger(JsonUtil.class);
29 private static ObjectMapper mapper;
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"));
40 public static <T> T toObject(URL content, Class<T> clazz)
44 return mapper.readValue(content, clazz);
48 LOG.warn("read value failed !", e);
53 public static <T> T toObject(File content, Class<T> clazz)
57 return mapper.readValue(content, clazz);
61 LOG.warn("read value failed !", e);