44 */
55package org .epics .vtype .json ;
66
7+ import org .epics .util .array .*;
8+ import org .epics .util .number .UnsignedConversions ;
9+
10+ import javax .json .*;
711import java .time .Instant ;
812import java .util .ArrayList ;
913import java .util .List ;
10- import javax .json .Json ;
11- import javax .json .JsonArray ;
12- import javax .json .JsonArrayBuilder ;
13- import javax .json .JsonNumber ;
14- import javax .json .JsonString ;
15- import javax .json .JsonValue ;
16- import org .epics .util .array .ArrayByte ;
17- import org .epics .util .array .ArrayDouble ;
18- import org .epics .util .array .ArrayFloat ;
19- import org .epics .util .array .ArrayInteger ;
20- import org .epics .util .array .ArrayLong ;
21- import org .epics .util .array .ArrayShort ;
22- import org .epics .util .array .ArrayUByte ;
23- import org .epics .util .array .ArrayUInteger ;
24- import org .epics .util .array .ArrayULong ;
25- import org .epics .util .array .ArrayUShort ;
26- import org .epics .util .array .ListByte ;
27- import org .epics .util .array .ListDouble ;
28- import org .epics .util .array .ListFloat ;
29- import org .epics .util .array .ListInteger ;
30- import org .epics .util .array .ListLong ;
31- import org .epics .util .array .ListNumber ;
32- import org .epics .util .array .ListShort ;
33- import org .epics .util .array .ListUByte ;
34- import org .epics .util .array .ListUInteger ;
35- import org .epics .util .array .ListULong ;
36- import org .epics .util .array .ListUShort ;
37- import org .epics .util .number .UnsignedConversions ;
3814
3915/**
4016 * Utility classes to convert JSON arrays to and from Lists and ListNumbers.
4117 *
4218 * @author carcassi
4319 */
4420public class JsonArrays {
45-
21+
4622 /**
4723 * Checks whether the array contains only numbers.
48- *
24+ *
4925 * @param array a JSON array
5026 * @return true if all elements are JSON numbers
5127 */
@@ -57,10 +33,10 @@ public static boolean isNumericArray(JsonArray array) {
5733 }
5834 return true ;
5935 }
60-
36+
6137 /**
6238 * Checks whether the array contains only strings.
63- *
39+ *
6440 * @param array a JSON array
6541 * @return true if all elements are JSON strings
6642 */
@@ -72,10 +48,10 @@ public static boolean isStringArray(JsonArray array) {
7248 }
7349 return true ;
7450 }
75-
51+
7652 /**
7753 * Converts the given numeric JSON array to a ListDouble.
78- *
54+ *
7955 * @param array an array of numbers
8056 * @return a new ListDouble
8157 */
@@ -86,10 +62,10 @@ public static ListDouble toListDouble(JsonArray array) {
8662 }
8763 return ArrayDouble .of (values );
8864 }
89-
65+
9066 /**
9167 * Converts the given numeric JSON array to a ListFloat.
92- *
68+ *
9369 * @param array an array of numbers
9470 * @return a new ListFloat
9571 */
@@ -107,7 +83,7 @@ public static ListFloat toListFloat(JsonArray array) {
10783
10884 /**
10985 * Converts the given numeric JSON array to a {@code ListULong}.
110- *
86+ *
11187 * @param array an array of numbers
11288 * @return a new {@code ListULong}
11389 */
@@ -121,7 +97,7 @@ public static ListULong toListULong(JsonArray array) {
12197
12298 /**
12399 * Converts the given numeric JSON array to a ListLong.
124- *
100+ *
125101 * @param array an array of numbers
126102 * @return a new ListLong
127103 */
@@ -135,7 +111,7 @@ public static ListLong toListLong(JsonArray array) {
135111
136112 /**
137113 * Converts the given numeric JSON array to a {@code ListUInteger}.
138- *
114+ *
139115 * @param array an array of numbers
140116 * @return a new {@code ListUInteger}
141117 */
@@ -146,10 +122,10 @@ public static ListUInteger toListUInteger(JsonArray array) {
146122 }
147123 return ArrayUInteger .of (values );
148124 }
149-
125+
150126 /**
151127 * Converts the given numeric JSON array to a ListInteger.
152- *
128+ *
153129 * @param array an array of numbers
154130 * @return a new ListInteger
155131 */
@@ -163,7 +139,7 @@ public static ListInteger toListInt(JsonArray array) {
163139
164140 /**
165141 * Converts the given numeric JSON array to a {@code ListUShort}.
166- *
142+ *
167143 * @param array an array of numbers
168144 * @return a new {@code ListUShort}
169145 */
@@ -177,7 +153,7 @@ public static ListUShort toListUShort(JsonArray array) {
177153
178154 /**
179155 * Converts the given numeric JSON array to a ListShort.
180- *
156+ *
181157 * @param array an array of numbers
182158 * @return a new ListShort
183159 */
@@ -191,7 +167,7 @@ public static ListShort toListShort(JsonArray array) {
191167
192168 /**
193169 * Converts the given numeric JSON array to a {@code ListUByte}.
194- *
170+ *
195171 * @param array an array of numbers
196172 * @return a new {@code ListUByte}
197173 */
@@ -205,7 +181,7 @@ public static ListUByte toListUByte(JsonArray array) {
205181
206182 /**
207183 * Converts the given numeric JSON array to a ListByte.
208- *
184+ *
209185 * @param array an array of numbers
210186 * @return a new ListByte
211187 */
@@ -219,7 +195,7 @@ public static ListByte toListByte(JsonArray array) {
219195
220196 /**
221197 * Converts the given string JSON array to a List of Strings.
222- *
198+ *
223199 * @param array an array of strings
224200 * @return a new List of Strings
225201 */
@@ -234,7 +210,7 @@ public static List<String> toListString(JsonArray array) {
234210
235211 /**
236212 * Converts the given JSON array to a List of Instants.
237- *
213+ *
238214 * @param array an array
239215 * @return a new List of Timestamps
240216 */
@@ -252,7 +228,7 @@ public static List<Instant> toListTimestamp(JsonArray array) {
252228
253229 /**
254230 * Converts the given List of String to a string JSON array.
255- *
231+ *
256232 * @param list a List of Strings
257233 * @return an array of strings
258234 */
@@ -270,7 +246,7 @@ public static JsonArrayBuilder fromListString(List<String> list) {
270246
271247 /**
272248 * Converts the given List of Timestamp to a JSON array.
273- *
249+ *
274250 * @param list a List of Timestamps
275251 * @return an array
276252 */
@@ -288,7 +264,7 @@ public static JsonArrayBuilder fromListTimestamp(List<Instant> list) {
288264
289265 /**
290266 * Converts the given ListNumber to a number JSON array.
291- *
267+ *
292268 * @param list a list of numbers
293269 * @return an array of numbers
294270 */
@@ -308,21 +284,50 @@ public static JsonArrayBuilder fromListNumber(ListNumber list) {
308284 }
309285 } else {
310286 for (int i = 0 ; i < list .size (); i ++) {
311- double value = list .getDouble (i );
312- if (Double .isFinite (value )){
313- b .add (value );
314- }
315- else if (Double .isNaN (value )) {
316- b .add (VTypeJsonMapper .NAN );
317- } else if (Double .valueOf (value ).equals (Double .POSITIVE_INFINITY )){
318- b .add (VTypeJsonMapper .POS_INF );
319- }
320- else if (Double .valueOf (value ).equals (Double .NEGATIVE_INFINITY )){
321- b .add (VTypeJsonMapper .NEG_INF );
287+ if (list instanceof ArrayDouble ) {
288+ double value = list .getDouble (i );
289+ if (Double .isFinite (value )) {
290+ b .add (value );
291+ } else if (Double .isNaN (value )) {
292+ b .add (VTypeJsonMapper .NAN );
293+ } else if (Double .valueOf (value ).equals (Double .POSITIVE_INFINITY )) {
294+ b .add (VTypeJsonMapper .POS_INF );
295+ } else if (Double .valueOf (value ).equals (Double .NEGATIVE_INFINITY )) {
296+ b .add (VTypeJsonMapper .NEG_INF );
297+ }
298+ } else if (list instanceof ArrayFloat ) {
299+ float value = list .getFloat (i );
300+ if (Float .isFinite (value )) {
301+ b .add (value );
302+ } else if (Float .isNaN (value )) {
303+ b .add (VTypeJsonMapper .NAN );
304+ } else if (Float .valueOf (value ).equals (Float .POSITIVE_INFINITY )) {
305+ b .add (VTypeJsonMapper .POS_INF );
306+ } else if (Float .valueOf (value ).equals (Float .NEGATIVE_INFINITY )) {
307+ b .add (VTypeJsonMapper .NEG_INF );
308+ }
322309 }
323310 }
324311 }
325312 return b ;
326313 }
327-
314+
315+ /**
316+ * @param object In practice an Array* object or a {@link List} of {@link String}. The
317+ * array data may be empty.
318+ * @return A {@link JsonArrayBuilder} that capable of converting the input parameter.
319+ *
320+ */
321+ public static JsonArrayBuilder fromList (Object object ) {
322+ if (object instanceof ListNumber ) {
323+ return fromListNumber ((ListNumber ) object );
324+ } else if (object instanceof List ) {
325+ List list = (List ) object ;
326+ if (list .isEmpty ()) {
327+ return JsonVTypeBuilder .factory .createArrayBuilder ();
328+ }
329+ return fromListString (list );
330+ }
331+ return JsonVTypeBuilder .factory .createArrayBuilder ();
332+ }
328333}
0 commit comments