001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2014 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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 License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.wsclient.unmarshallers;
021    
022    import org.json.simple.JSONArray;
023    
024    import javax.annotation.CheckForNull;
025    
026    import java.text.ParseException;
027    import java.text.SimpleDateFormat;
028    import java.util.Date;
029    import java.util.Map;
030    
031    // Godin: we will use raw types here, because typically JSONObject passed as an argument
032    @SuppressWarnings("rawtypes")
033    public final class JsonUtils {
034    
035      private JsonUtils() {
036        // only static methods
037      }
038    
039      @CheckForNull
040      public static String getString(Map obj, String field) {
041        Object value = obj.get(field);
042        if (value instanceof String || value instanceof Number) {
043          return value.toString();
044        }
045        return null;
046      }
047    
048      @CheckForNull
049      public static Integer getInteger(Map obj, String field) {
050        Object value = obj.get(field);
051        if (value != null) {
052          return ((Long) value).intValue();
053        }
054        return null;
055      }
056    
057      @CheckForNull
058      public static Boolean getBoolean(Map obj, String field) {
059        Object value = obj.get(field);
060        if (value != null) {
061          return (Boolean) value;
062        }
063        return null;
064      }
065    
066      @CheckForNull
067      public static Long getLong(Map obj, String field) {
068        Object value = obj.get(field);
069        if (value != null) {
070          return (Long) value;
071        }
072        return null;
073      }
074    
075      @CheckForNull
076      public static Double getDouble(Map obj, String field) {
077        Object value = obj.get(field);
078        if (value != null) {
079          if (value instanceof Long) {
080            return ((Long) value).doubleValue();
081          }
082          return (Double) value;
083        }
084        return null;
085      }
086    
087      /**
088       * @since 2.5
089       */
090      @CheckForNull
091      public static JSONArray getArray(Map obj, String field) {
092        return (JSONArray) obj.get(field);
093      }
094    
095      @CheckForNull
096      public static Date getDateTime(Map obj, String field) {
097        return parseDate(obj, field, "yyyy-MM-dd'T'HH:mm:ssZ");
098      }
099    
100      @CheckForNull
101      public static Date getDate(Map obj, String field) {
102        return parseDate(obj, field, "yyyy-MM-dd");
103      }
104    
105      @CheckForNull
106      private static Date parseDate(Map obj, String field, String format) {
107        String value = getString(obj, field);
108        if (value != null) {
109          try {
110            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
111            return dateFormat.parse(value);
112    
113          } catch (ParseException e) {
114            throw new IllegalArgumentException("Fail to parse date property '" + field + "': " + format, e);
115          }
116        }
117        return null;
118      }
119    
120    }