Java tutorial
/* file of LaBeeFramework https://www.bee-wkspace.com/ Copyright (C) 2016- Naoki Yoshioka (ARTS Laboratory) http://www.arts-lab.net/dev/ This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 3 of the License, or any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. */ package com.bee_wkspace.labee_fw.common; import com.fasterxml.jackson.databind.ObjectMapper; /** * JSON<br> * jackson??? * * @author ARTS Laboratory * * $Id: JsonUtil.java 559 2016-08-14 12:24:00Z pjmember $ */ public class JsonUtil<T> { /** * JSON?????<br> * ??Map?List??????????? * * @param targetObject * @return JSON * @throws Exception */ public String encodeToString(T targetObject) throws Exception { ObjectMapper mapper = new ObjectMapper(); String json = ""; if (targetObject != null) { json = mapper.writeValueAsString(targetObject); } return json; } /** * JSON???<br> * ?clazz?????????<br> * ??Map?List??????????? * * @param json JSON * @param clazz ? * @return ? * @throws Exception */ public T endoceToObject(Class<?> clazz, String json) throws Exception { ObjectMapper mapper = new ObjectMapper(); @SuppressWarnings("unchecked") T targetObject = (T) mapper.readValue(json, clazz); return targetObject; } }