001 /*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * Sonar is free software; you can redistribute it and/or
007 * modify it under the terms of the GNU Lesser General Public
008 * License as published by the Free Software Foundation; either
009 * version 3 of the License, or (at your option) any later version.
010 *
011 * Sonar is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 * Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public
017 * License along with Sonar; if not, write to the Free Software
018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
019 */
020 package org.sonar.wsclient.unmarshallers;
021
022 import org.json.simple.JSONArray;
023
024 import java.text.ParseException;
025 import java.text.SimpleDateFormat;
026 import java.util.Date;
027 import java.util.Map;
028
029 // Godin: we will use raw types here, because typically JSONObject passed as an argument
030 @SuppressWarnings("rawtypes")
031 public final class JsonUtils {
032
033 private JsonUtils() {
034 // only static methods
035 }
036
037 public static String getString(Map obj, String field) {
038 Object value = obj.get(field);
039 if (value instanceof String || value instanceof Number) {
040 return value.toString();
041 }
042 return null;
043 }
044
045 public static Integer getInteger(Map obj, String field) {
046 Object value = obj.get(field);
047 if (value != null) {
048 return ((Long) value).intValue();
049 }
050 return null;
051 }
052
053 public static Boolean getBoolean(Map obj, String field) {
054 Object value = obj.get(field);
055 if (value != null) {
056 return (Boolean) value;
057 }
058 return null;
059 }
060
061 public static Long getLong(Map obj, String field) {
062 Object value = obj.get(field);
063 if (value != null) {
064 return (Long) value;
065 }
066 return null;
067 }
068
069 public static Double getDouble(Map obj, String field) {
070 Object value = obj.get(field);
071 if (value != null) {
072 return (Double) value;
073 }
074 return null;
075 }
076
077 /**
078 * @since 2.5
079 */
080 public static JSONArray getArray(Map obj, String field) {
081 return (JSONArray) obj.get(field);
082 }
083
084 public static Date getDateTime(Map obj, String field) {
085 return parseDate(obj, field, "yyyy-MM-dd'T'HH:mm:ssZ");
086 }
087
088 public static Date getDate(Map obj, String field) {
089 return parseDate(obj, field, "yyyy-MM-dd");
090 }
091
092 private static Date parseDate(Map obj, String field, String format) {
093 String value = getString(obj, field);
094 if (value != null) {
095 try {
096 SimpleDateFormat dateFormat = new SimpleDateFormat(format);
097 return dateFormat.parse(value);
098
099 } catch (ParseException e) {
100 throw new RuntimeException(e);
101 }
102 }
103 return null;
104 }
105
106 }