|
18 | 18 |
|
19 | 19 | class JSON { |
20 | 20 |
|
21 | | - static double checkDouble(double d) throws JSONException { |
22 | | - if (Double.isInfinite(d) || Double.isNaN(d)) { |
23 | | - throw new JSONException("Forbidden numeric value: " + d); |
24 | | - } |
25 | | - return d; |
26 | | - } |
| 21 | + static double checkDouble(double d) throws JSONException { |
| 22 | + if (Double.isInfinite(d) || Double.isNaN(d)) { |
| 23 | + throw new JSONException("Forbidden numeric value: " + d); |
| 24 | + } |
| 25 | + return d; |
| 26 | + } |
27 | 27 |
|
28 | | - static Boolean toBoolean(Object value) { |
29 | | - if (value instanceof Boolean) { |
30 | | - return (Boolean) value; |
31 | | - } |
32 | | - if (value instanceof String) { |
33 | | - String stringValue = (String) value; |
34 | | - if ("true".equalsIgnoreCase(stringValue)) { |
35 | | - return true; |
36 | | - } |
37 | | - if ("false".equalsIgnoreCase(stringValue)) { |
38 | | - return false; |
39 | | - } |
40 | | - } |
41 | | - return null; |
42 | | - } |
| 28 | + static Boolean toBoolean(Object value) { |
| 29 | + if (value instanceof Boolean) { |
| 30 | + return (Boolean) value; |
| 31 | + } |
| 32 | + if (value instanceof String) { |
| 33 | + String stringValue = (String) value; |
| 34 | + if ("true".equalsIgnoreCase(stringValue)) { |
| 35 | + return true; |
| 36 | + } |
| 37 | + if ("false".equalsIgnoreCase(stringValue)) { |
| 38 | + return false; |
| 39 | + } |
| 40 | + } |
| 41 | + return null; |
| 42 | + } |
43 | 43 |
|
44 | | - static Double toDouble(Object value) { |
45 | | - if (value instanceof Double) { |
46 | | - return (Double) value; |
47 | | - } |
48 | | - if (value instanceof Number) { |
49 | | - return ((Number) value).doubleValue(); |
50 | | - } |
51 | | - if (value instanceof String) { |
52 | | - try { |
53 | | - return Double.valueOf((String) value); |
54 | | - } |
55 | | - catch (NumberFormatException ignored) { |
56 | | - } |
57 | | - } |
58 | | - return null; |
59 | | - } |
| 44 | + static Double toDouble(Object value) { |
| 45 | + if (value instanceof Double) { |
| 46 | + return (Double) value; |
| 47 | + } |
| 48 | + if (value instanceof Number) { |
| 49 | + return ((Number) value).doubleValue(); |
| 50 | + } |
| 51 | + if (value instanceof String) { |
| 52 | + try { |
| 53 | + return Double.valueOf((String) value); |
| 54 | + } catch (NumberFormatException ignored) { |
| 55 | + } |
| 56 | + } |
| 57 | + return null; |
| 58 | + } |
60 | 59 |
|
61 | | - static Integer toInteger(Object value) { |
62 | | - if (value instanceof Integer) { |
63 | | - return (Integer) value; |
64 | | - } |
65 | | - if (value instanceof Number) { |
66 | | - return ((Number) value).intValue(); |
67 | | - } |
68 | | - if (value instanceof String) { |
69 | | - try { |
70 | | - return (int) Double.parseDouble((String) value); |
71 | | - } |
72 | | - catch (NumberFormatException ignored) { |
73 | | - } |
74 | | - } |
75 | | - return null; |
76 | | - } |
| 60 | + static Integer toInteger(Object value) { |
| 61 | + if (value instanceof Integer) { |
| 62 | + return (Integer) value; |
| 63 | + } |
| 64 | + if (value instanceof Number) { |
| 65 | + return ((Number) value).intValue(); |
| 66 | + } |
| 67 | + if (value instanceof String) { |
| 68 | + try { |
| 69 | + return (int) Double.parseDouble((String) value); |
| 70 | + } catch (NumberFormatException ignored) { |
| 71 | + } |
| 72 | + } |
| 73 | + return null; |
| 74 | + } |
77 | 75 |
|
78 | | - static Long toLong(Object value) { |
79 | | - if (value instanceof Long) { |
80 | | - return (Long) value; |
81 | | - } |
82 | | - if (value instanceof Number) { |
83 | | - return ((Number) value).longValue(); |
84 | | - } |
85 | | - if (value instanceof String) { |
86 | | - try { |
87 | | - return (long) Double.parseDouble((String) value); |
88 | | - } |
89 | | - catch (NumberFormatException ignored) { |
90 | | - } |
91 | | - } |
92 | | - return null; |
93 | | - } |
| 76 | + static Long toLong(Object value) { |
| 77 | + if (value instanceof Long) { |
| 78 | + return (Long) value; |
| 79 | + } |
| 80 | + if (value instanceof Number) { |
| 81 | + return ((Number) value).longValue(); |
| 82 | + } |
| 83 | + if (value instanceof String) { |
| 84 | + try { |
| 85 | + return (long) Double.parseDouble((String) value); |
| 86 | + } catch (NumberFormatException ignored) { |
| 87 | + } |
| 88 | + } |
| 89 | + return null; |
| 90 | + } |
94 | 91 |
|
95 | | - static String toString(Object value) { |
96 | | - if (value instanceof String) { |
97 | | - return (String) value; |
98 | | - } |
99 | | - if (value != null) { |
100 | | - return String.valueOf(value); |
101 | | - } |
102 | | - return null; |
103 | | - } |
| 92 | + static String toString(Object value) { |
| 93 | + if (value instanceof String) { |
| 94 | + return (String) value; |
| 95 | + } |
| 96 | + if (value != null) { |
| 97 | + return String.valueOf(value); |
| 98 | + } |
| 99 | + return null; |
| 100 | + } |
104 | 101 |
|
105 | | - public static JSONException typeMismatch(Object indexOrName, Object actual, String requiredType) |
106 | | - throws JSONException { |
107 | | - if (actual == null) { |
108 | | - throw new JSONException("Value at " + indexOrName + " is null."); |
109 | | - } |
110 | | - throw new JSONException("Value " + actual + " at " + indexOrName + " of type " + actual.getClass().getName() |
111 | | - + " cannot be converted to " + requiredType); |
112 | | - } |
| 102 | + public static JSONException typeMismatch(Object indexOrName, Object actual, String requiredType) |
| 103 | + throws JSONException { |
| 104 | + if (actual == null) { |
| 105 | + throw new JSONException("Value at " + indexOrName + " is null."); |
| 106 | + } |
| 107 | + throw new JSONException("Value " + actual + " at " + indexOrName + " of type " + actual.getClass().getName() |
| 108 | + + " cannot be converted to " + requiredType); |
| 109 | + } |
113 | 110 |
|
114 | | - public static JSONException typeMismatch(Object actual, String requiredType) throws JSONException { |
115 | | - if (actual == null) { |
116 | | - throw new JSONException("Value is null."); |
117 | | - } |
118 | | - throw new JSONException("Value " + actual + " of type " + actual.getClass().getName() |
119 | | - + " cannot be converted to " + requiredType); |
120 | | - } |
| 111 | + public static JSONException typeMismatch(Object actual, String requiredType) throws JSONException { |
| 112 | + if (actual == null) { |
| 113 | + throw new JSONException("Value is null."); |
| 114 | + } |
| 115 | + throw new JSONException("Value " + actual + " of type " + actual.getClass().getName() |
| 116 | + + " cannot be converted to " + requiredType); |
| 117 | + } |
121 | 118 |
|
| 119 | + private JSON() { |
| 120 | + } |
122 | 121 | } |
0 commit comments