OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 182 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 182 Rev 185
Line 20... Line 20...
20
import java.math.BigDecimal;
20
import java.math.BigDecimal;
21
import java.math.BigInteger;
21
import java.math.BigInteger;
22
import java.text.Format;
22
import java.text.Format;
23
import java.util.Calendar;
23
import java.util.Calendar;
24
import java.util.Date;
24
import java.util.Date;
-
 
25
import java.util.function.Function;
25
 
26
 
26
import com.google.gson.Gson;
27
import com.google.gson.Gson;
27
import com.google.gson.JsonElement;
28
import com.google.gson.JsonElement;
28
import com.google.gson.reflect.TypeToken;
29
import com.google.gson.reflect.TypeToken;
29
 
30
 
Line 66... Line 67...
66
        if (param != null) {
67
        if (param != null) {
67
            if (param instanceof HTMLable) {
68
            if (param instanceof HTMLable) {
68
                result = ((HTMLable) param).getHTML();
69
                result = ((HTMLable) param).getHTML();
69
            } else if (param instanceof JSONAble) {
70
            } else if (param instanceof JSONAble) {
70
                result = ((JSONAble) param).toJSON();
71
                result = ((JSONAble) param).toJSON();
-
 
72
            } else if (param instanceof JSONNamed) {
-
 
73
                result = ((JSONNamed) param).getJSONName();
71
            } else if (param instanceof Date) {
74
            } else if (param instanceof Date) {
72
                result = format((Date) param);
75
                result = format((Date) param);
73
            } else if (param instanceof Calendar) {
76
            } else if (param instanceof Calendar) {
74
                result = formatCalendar(((Calendar) param));
77
                result = formatCalendar(((Calendar) param));
75
            } else if (param instanceof Class<?>) {
78
            } else if (param instanceof Class<?>) {
Line 91... Line 94...
91
                    hexString = hexString.substring(2, hexString.length());
94
                    hexString = hexString.substring(2, hexString.length());
92
                }
95
                }
93
                result = "#" + hexString;
96
                result = "#" + hexString;
94
            } else if (param instanceof BigDecimal) {
97
            } else if (param instanceof BigDecimal) {
95
                result = ((BigDecimal) param).doubleValue();
98
                result = ((BigDecimal) param).doubleValue();
-
 
99
            } else if (param instanceof Enum) {
-
 
100
                result = ((Enum<?>) param).name().toLowerCase();
96
            } else {
101
            } else {
97
                result = param;
102
                result = param;
98
            }
103
            }
99
        }
104
        }
100
 
105
 
Line 132... Line 137...
132
                } catch (java.text.ParseException e) {
137
                } catch (java.text.ParseException e) {
133
                    throw new IllegalArgumentException("object (" + o.getClass().getName() + ") is not assignable for '" + type + "', the format is not valid", e);
138
                    throw new IllegalArgumentException("object (" + o.getClass().getName() + ") is not assignable for '" + type + "', the format is not valid", e);
134
                }
139
                }
135
            } else if (type.equals(Color.class)) {
140
            } else if (type.equals(Color.class)) {
136
                result = type.cast(Color.decode(o.toString()));
141
                result = type.cast(Color.decode(o.toString()));
-
 
142
            } else if (JSONNamed.class.isAssignableFrom(type)) {
-
 
143
                for (final T enumConstant : type.getEnumConstants()) {
-
 
144
                    if (((JSONNamed) enumConstant).getJSONName().equals(o))
-
 
145
                        return enumConstant;
-
 
146
                }
-
 
147
                throw new IllegalArgumentException("Unknown name '" + o + "' for " + type);
-
 
148
            } else if (Enum.class.isAssignableFrom(type)) {
-
 
149
                @SuppressWarnings("unchecked")
-
 
150
                final Enum<?> enumVal = Enum.valueOf(type.asSubclass(Enum.class), o.toString().toUpperCase());
-
 
151
                result = type.cast(enumVal);
137
            } else {
152
            } else {
138
                result = type.cast(o);
153
                result = type.cast(o);
139
            }
154
            }
140
        } else {
155
        } else {
141
            result = null;
156
            result = null;
Line 147... Line 162...
147
    public static <T> T getParameterFromJSON(final JSONObject json, final String key, final Class<T> type) {
162
    public static <T> T getParameterFromJSON(final JSONObject json, final String key, final Class<T> type) {
148
        return getParameterFromJSON(json, key, type, null);
163
        return getParameterFromJSON(json, key, type, null);
149
    }
164
    }
150
 
165
 
151
    public static <T> T getParameterFromJSON(final JSONObject json, final String key, final Class<T> type, T defaultValue) {
166
    public static <T> T getParameterFromJSON(final JSONObject json, final String key, final Class<T> type, T defaultValue) {
-
 
167
        return getParameterFromJSON(json, key, (o) -> getObjectFromJSON(o, type), defaultValue);
-
 
168
    }
-
 
169
 
-
 
170
    public static <T> T getParameterFromJSON(final JSONObject json, final String key, final Function<Object, T> ctor, T defaultValue) {
152
        if (json == null) {
171
        if (json == null) {
153
            throw new IllegalArgumentException("null JSON");
172
            throw new IllegalArgumentException("null JSON");
154
        }
173
        }
155
        if (key == null) {
174
        if (key == null) {
156
            throw new IllegalArgumentException("null key");
175
            throw new IllegalArgumentException("null key");
157
        }
176
        }
158
        return json.containsKey(key) ? getObjectFromJSON(json.get(key), type) : defaultValue;
177
        return json.containsKey(key) ? ctor.apply(json.get(key)) : defaultValue;
159
    }
178
    }
160
 
179
 
161
    public static JSONObject convertStringToJsonObject(final String jsonString) {
180
    public static JSONObject convertStringToJsonObject(final String jsonString) {
162
        final JSONParser parser = new JSONParser(JSONParser.USE_HI_PRECISION_FLOAT);
181
        final JSONParser parser = new JSONParser(JSONParser.USE_HI_PRECISION_FLOAT);
163
        final JSONObject json;
182
        final JSONObject json;