OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 132 | Rev 144 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
94 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.utils.io;
15
 
132 ilm 16
import org.openconcerto.utils.NumberUtils;
142 ilm 17
import org.openconcerto.utils.XMLDateFormat;
132 ilm 18
 
94 ilm 19
import java.awt.Color;
142 ilm 20
import java.math.BigDecimal;
21
import java.math.BigInteger;
22
import java.text.Format;
94 ilm 23
import java.util.Calendar;
142 ilm 24
import java.util.Date;
94 ilm 25
 
26
import net.minidev.json.JSONArray;
27
import net.minidev.json.JSONObject;
132 ilm 28
import net.minidev.json.parser.JSONParser;
29
import net.minidev.json.parser.ParseException;
94 ilm 30
 
31
public class JSONConverter {
32
 
142 ilm 33
    static private final Format DF = new XMLDateFormat();
34
 
35
    static synchronized private final String format(final Date d) {
36
        return DF.format(d);
37
    }
38
 
39
    public synchronized static String formatCalendar(final Calendar calendar) {
40
        return DF.format(calendar);
41
    }
42
 
43
    static synchronized private final Date parse(final String s) throws java.text.ParseException {
44
        return (Date) DF.parseObject(s);
45
    }
46
 
94 ilm 47
    public static Object getJSON(Object param) {
48
        Object result = null;
49
 
50
        if (param != null) {
132 ilm 51
            if (param instanceof HTMLable) {
52
                result = ((HTMLable) param).getHTML();
53
            } else if (param instanceof JSONAble) {
94 ilm 54
                result = ((JSONAble) param).toJSON();
142 ilm 55
            } else if (param instanceof Date) {
56
                result = format((Date) param);
132 ilm 57
            } else if (param instanceof Calendar) {
142 ilm 58
                result = formatCalendar(((Calendar) param));
94 ilm 59
            } else if (param instanceof Class<?>) {
60
                result = ((Class<?>) param).getName();
61
            } else if (param instanceof Iterable) {
62
                final Iterable<?> tmp = (Iterable<?>) param;
63
                final JSONArray jsonArray = new JSONArray();
64
                for (Object o : tmp) {
65
                    jsonArray.add(getJSON(o));
66
                }
67
                result = jsonArray;
68
            } else if (param instanceof Color) {
69
                if (param != null) {
70
                    final Color paramColor = (Color) param;
71
                    final JSONObject jsonColor = new JSONObject();
72
                    jsonColor.put("r", paramColor.getRed());
73
                    jsonColor.put("g", paramColor.getGreen());
74
                    jsonColor.put("b", paramColor.getBlue());
75
                    result = jsonColor;
76
                }
142 ilm 77
            } else if (param instanceof BigDecimal) {
78
                result = ((BigDecimal) param).doubleValue();
94 ilm 79
            } else {
80
                result = param;
81
            }
82
        }
83
 
84
        return result;
85
    }
86
 
142 ilm 87
    public static <T> T getObjectFromJSON(final Object o, final Class<T> type) {
88
        final T result;
94 ilm 89
        if (o != null && !o.equals("null")) {
142 ilm 90
            if (type.isInstance(o)) {
91
                result = type.cast(o);
92
            } else if (type.equals(Integer.class)) {
93
                final int intVal;
94
                if (o instanceof BigDecimal) {
95
                    intVal = ((BigDecimal) o).intValueExact();
96
                } else if (o instanceof BigInteger) {
97
                    // TODO use intValueExact() in Java 8
98
                    final BigInteger bigInt = (BigInteger) o;
99
                    if (bigInt.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0 || bigInt.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0)
100
                        throw new IllegalArgumentException("object (" + o.getClass().getName() + ") is not assignable for '" + type + "'");
101
                    intVal = bigInt.intValue();
94 ilm 102
                } else {
103
                    try {
142 ilm 104
                        intVal = NumberUtils.ensureInt((Long) o);
105
                    } catch (ArithmeticException ex) {
106
                        throw new IllegalArgumentException("object (" + o.getClass().getName() + ") is not assignable for '" + type + "'", ex);
94 ilm 107
                    }
108
                }
142 ilm 109
                result = type.cast(intVal);
132 ilm 110
            } else if (type.equals(Date.class)) {
94 ilm 111
                final String sparam = (String) o;
142 ilm 112
                try {
113
                    final Date c = parse(sparam);
114
                    result = type.cast(c);
115
                } catch (java.text.ParseException e) {
116
                    throw new IllegalArgumentException("object (" + o.getClass().getName() + ") is not assignable for '" + type + "', the format is not valid", e);
94 ilm 117
                }
142 ilm 118
            } else if (type.equals(Color.class)) {
119
                final JSONObject jsonColor = (JSONObject) o;
120
                final int r = JSONConverter.getParameterFromJSON(jsonColor, "r", Integer.class);
121
                final int g = JSONConverter.getParameterFromJSON(jsonColor, "g", Integer.class);
122
                final int b = JSONConverter.getParameterFromJSON(jsonColor, "b", Integer.class);
123
                result = type.cast(new Color(r, g, b));
94 ilm 124
            } else {
142 ilm 125
                result = type.cast(o);
94 ilm 126
            }
132 ilm 127
        } else {
128
            result = null;
94 ilm 129
        }
130
 
142 ilm 131
        return result;
94 ilm 132
    }
133
 
142 ilm 134
    public static <T> T getParameterFromJSON(final JSONObject json, final String key, final Class<T> type) {
94 ilm 135
        return getParameterFromJSON(json, key, type, null);
136
    }
137
 
142 ilm 138
    public static <T> T getParameterFromJSON(final JSONObject json, final String key, final Class<T> type, T defaultValue) {
139
        return json.containsKey(key) ? getObjectFromJSON(json.get(key), type) : defaultValue;
94 ilm 140
    }
141
 
132 ilm 142
    public static JSONObject convertStringToJsonObject(final String jsonString) {
143
        final JSONParser parser = new JSONParser(JSONParser.USE_HI_PRECISION_FLOAT);
144
        final JSONObject json;
145
        try {
146
            json = (JSONObject) parser.parse(jsonString);
147
        } catch (final ParseException ex) {
148
            throw new IllegalArgumentException(ex.getMessage(), ex);
149
        }
150
        return json;
151
    }
152
 
153
    public static JSONArray convertStringToJsonArray(final String jsonString) {
154
        final JSONParser parser = new JSONParser(JSONParser.USE_HI_PRECISION_FLOAT);
155
        final JSONArray json;
156
        try {
157
            json = (JSONArray) parser.parse(jsonString);
158
        } catch (final ParseException ex) {
159
            throw new IllegalArgumentException(ex.getMessage(), ex);
160
        }
161
        return json;
162
    }
94 ilm 163
}