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