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    import javax.annotation.Nullable;
026    
027    import java.text.ParseException;
028    import java.text.SimpleDateFormat;
029    import java.util.Date;
030    import java.util.Map;
031    
032    // Godin: we will use raw types here, because typically JSONObject passed as an argument
033    @SuppressWarnings("rawtypes")
034    public final class JsonUtils {
035    
036      private JsonUtils() {
037        // only static methods
038      }
039    
040      @CheckForNull
041      public static String getString(Map obj, String field) {
042        Object value = obj.get(field);
043        if (value instanceof String || value instanceof Number) {
044          return value.toString();
045        }
046        return null;
047      }
048    
049      @CheckForNull
050      public static Integer getInteger(Map obj, String field) {
051        Object value = obj.get(field);
052        if (value != null) {
053          return ((Long) value).intValue();
054        }
055        return null;
056      }
057    
058      @CheckForNull
059      public static Boolean getBoolean(Map obj, String field) {
060        Object value = obj.get(field);
061        if (value != null) {
062          return (Boolean) value;
063        }
064        return null;
065      }
066    
067      @CheckForNull
068      public static Long getLong(Map obj, String field) {
069        Object value = obj.get(field);
070        if (value != null) {
071          return (Long) value;
072        }
073        return null;
074      }
075    
076      @CheckForNull
077      public static Double getDouble(Map obj, String field) {
078        Object value = obj.get(field);
079        if (value != null) {
080          if (value instanceof Long) {
081            return ((Long) value).doubleValue();
082          }
083          return (Double) value;
084        }
085        return null;
086      }
087    
088      /**
089       * @since 2.5
090       */
091      @CheckForNull
092      public static JSONArray getArray(Map obj, String field) {
093        return (JSONArray) obj.get(field);
094      }
095    
096      @CheckForNull
097      public static Date getDateTime(Map obj, String field) {
098        return parseDate(getString(obj, field), "yyyy-MM-dd'T'HH:mm:ssZ");
099      }
100    
101      @CheckForNull
102      public static Date getDate(Map obj, String field) {
103        return parseDate(getString(obj, field));
104      }
105    
106      @CheckForNull
107      public static Date parseDate(@Nullable String value) {
108        return parseDate(value, "yyyy-MM-dd");
109      }
110    
111      @CheckForNull
112      private static Date parseDate(@Nullable String value, String format) {
113        if (value != null) {
114          try {
115            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
116            return dateFormat.parse(value);
117    
118          } catch (ParseException e) {
119            throw new IllegalArgumentException("Fail to parse date '" + value + "': " + format, e);
120          }
121        }
122        return null;
123      }
124    
125    }