Java tutorial
//Copyright (c) 2013 ice3x2@gmail.com // //Permission is hereby granted, free of charge, to any person //obtaining a copy of this software and associated documentation //files (the "Software"), to deal in the Software without //restriction, including without limitation the rights to use, //copy, modify, merge, publish, distribute, sublicense, and/or sell //copies of the Software, and to permit persons to whom the //Software is furnished to do so, subject to the following //conditions: // //The above copyright notice and this permission notice shall be //included in all copies or substantial portions of the Software. // //THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, //EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES //OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND //NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT //HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, //WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING //FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR //OTHER DEALINGS IN THE SOFTWARE. package kr.re.dev.YammaJson; import java.util.ArrayList; import java.util.List; import kr.re.dev.YammaJson.ObjectMapper.WrapData; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; /** * <pre> * Json. v0.9.4 * * http://www.dev.re.kr/9 * </pre> * @author dev.re.kr */ @SuppressWarnings({ "rawtypes", "unchecked" }) public class YammaJSON { /** * ? Json .<br/> * - Key ?? ? JSONObject . <br/> * - Collection ? JSONArray ?. * - Object Key ?? ? ? JSONObject ?. * @param obj ? Key ?? ?? ?. * @return ? null ? JSONObject */ public static JSONObject toJSON(Object obj) { return new YammaJsonSerializer().serialize(obj); } public static Object fromJSON(Object object, String json) { JSONObject jsonObject = new JSONObject(json); return fromJSON(object, jsonObject); } public static Object fromJSON(Object object, JSONObject json) { ObjectMapper om = new ObjectMapper(); om.mappinFieldByAnnotation(object, json); return object; } public static Object fromJSONArray(List<?> list, Class<?> type, String json, String path) throws JSONException { JSONObject jsonObject = new JSONObject(json); return fromJSONArray(list, type, jsonObject, path); } public static Object fromJSONArray(List<?> list, Class<?> type, JSONObject json, String path) { ObjectMapper om = new ObjectMapper(); WrapData wrapData = new WrapData(); wrapData.data = list; om.inflateValues(wrapData, new String[] { path }, new Type(type), json, true); return wrapData.data; } public static Object fromJSON(Object object, String json, String path) throws JSONException { JSONObject jsonObject = new JSONObject(json); return fromJSON(object, jsonObject, path); } public static Object fromJSON(Object object, JSONObject json, String path) { ObjectMapper om = new ObjectMapper(); WrapData wrapData = new WrapData(); wrapData.data = object; om.inflateValues(wrapData, new String[] { path }, new Type(object.getClass()), json, true); return wrapData.data; } public static Object fromJSON(Class<?> type, String json) throws JSONException { JSONObject jsonObject = new JSONObject(json); return fromJSON(type, jsonObject); } public static Object fromJSON(Class<?> type, JSONObject json) { ObjectMapper om = new ObjectMapper(); try { Object obj = om.newInstanceByDefaultConstractor(type); om.mappinFieldByAnnotation(obj, json); return obj; } catch (Exception e) { return null; } } public static Object fromJSON(Class<?> type, String json, String path) throws JSONException { JSONObject jsonObject = new JSONObject(json); return fromJSON(type, jsonObject, path); } public static Object fromJSON(Class<?> type, JSONObject json, String path) { ObjectMapper om = new ObjectMapper(); WrapData wrapData = new WrapData(); if (!om.isDefaultType(type)) try { wrapData.data = om.newInstanceByDefaultConstractor(type); } catch (Exception e) { e.printStackTrace(); return null; } om.inflateValues(wrapData, new String[] { path }, new Type(type), json, true); return wrapData.data; } public static <E> List<E> fromJSONArray(Class<E> type, String jsonString, String path) { jsonString = jsonString.replace("\n", ""); boolean isArray = jsonString.matches("^[ |\\t]{0,}[\\[].*[\\]][ |\\t]{0,}$"); if (isArray) { JSONArray jsonArray = new JSONArray(jsonString); return fromJSONArray(type, jsonArray); } else { JSONObject jsonObject = new JSONObject(jsonString); return fromJSONArray(type, jsonObject, path); } } public static <E> List<E> fromJSONArray(Class<E> type, String jsonArrayString) { JSONArray jsonArray = new JSONArray(jsonArrayString); return fromJSONArray(type, jsonArray); } public static <E> List<E> fromJSONArray(Class<E> type, JSONArray jsonArray) { ObjectMapper om = new ObjectMapper(); WrapData wrapData = new WrapData(); List<E> list = new ArrayList<E>(); wrapData.data = list; om.inflateJsonArray(wrapData, new Type(type), jsonArray); return (List<E>) wrapData.data; } public static <E> List<E> fromJSONArray(Class<E> type, JSONObject json, String path) { ObjectMapper om = new ObjectMapper(); WrapData wrapData = new WrapData(); List<E> list = new ArrayList<E>(); wrapData.data = list; om.inflateValues(wrapData, new String[] { path }, new Type(type), json, true); return (List<E>) wrapData.data; } }