Skip to content

Commit fd140f3

Browse files
committed
Revert "Issue_66 Support for NaN, Inf and -Inf in JSON serialization"
This reverts commit cb77b49.
1 parent cb77b49 commit fd140f3

File tree

6 files changed

+11
-125
lines changed

6 files changed

+11
-125
lines changed

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,3 @@ nbactions.xml
1515

1616
# For all you Mac OS users out there
1717
**/.DS_Store
18-
19-
# For all you IntelliJ users out there
20-
**/*.iml
21-
**/.idea

epics-vtype/vtype-json/src/main/java/org/epics/vtype/json/JsonArrays.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import javax.json.JsonArray;
1212
import javax.json.JsonArrayBuilder;
1313
import javax.json.JsonNumber;
14-
import javax.json.JsonObject;
1514
import javax.json.JsonString;
1615
import javax.json.JsonValue;
1716
import org.epics.util.array.ArrayByte;
@@ -83,11 +82,9 @@ public static boolean isStringArray(JsonArray array) {
8382
public static ListDouble toListDouble(JsonArray array) {
8483
double[] values = new double[array.size()];
8584
for (int i = 0; i < values.length; i++) {
86-
Double doubleValue = VTypeToJsonV1.getDoubleFromJsonString(array.get(i).toString());
87-
if(doubleValue != null){
88-
values[i] = doubleValue;
89-
}
90-
else {
85+
if (array.isNull(i)) {
86+
values[i] = Double.NaN;
87+
} else {
9188
values[i] = array.getJsonNumber(i).doubleValue();
9289
}
9390
}
@@ -316,18 +313,9 @@ public static JsonArrayBuilder fromListNumber(ListNumber list) {
316313
} else {
317314
for (int i = 0; i < list.size(); i++) {
318315
double value = list.getDouble(i);
319-
if(Double.isFinite(value)){
320-
b.add(value);
321-
}
322-
else if (Double.isNaN(value)) {
323-
b.add(VTypeJsonMapper.NAN);
324-
} else if(Double.valueOf(value).equals(Double.POSITIVE_INFINITY)){
325-
b.add(VTypeJsonMapper.POS_INF);
326-
}
327-
else if(Double.valueOf(value).equals(Double.NEGATIVE_INFINITY)){
328-
b.add(VTypeJsonMapper.NEG_INF);
329-
}
330-
else {
316+
if (Double.isNaN(value) || Double.isInfinite(value)) {
317+
b.addNull();
318+
} else {
331319
b.add(value);
332320
}
333321
}

epics-vtype/vtype-json/src/main/java/org/epics/vtype/json/JsonVTypeBuilder.java

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,26 +70,13 @@ public JsonVTypeBuilder add(String string, long l) {
7070
return this;
7171
}
7272

73-
/**
74-
* For the special cases of {@link Double#NaN}, {@link Double#POSITIVE_INFINITY} and
75-
* {@link Double#NEGATIVE_INFINITY} this method adds a string representation of the value.
76-
* @param string Name of property to set.
77-
* @param d Value to set.
78-
* @return The {@link JsonVTypeBuilder} instance.
79-
*/
8073
@Override
8174
public JsonVTypeBuilder add(String string, double d) {
82-
if(Double.isFinite(d)){
75+
if (Double.isNaN(d) || Double.isInfinite(d)) {
76+
builder.addNull(string);
77+
} else {
8378
builder.add(string, d);
8479
}
85-
else if (Double.isNaN(d)) {
86-
builder.add(string, VTypeJsonMapper.NAN);
87-
} else if(Double.valueOf(d).equals(Double.POSITIVE_INFINITY)){
88-
builder.add(string, VTypeJsonMapper.POS_INF);
89-
}
90-
else if(Double.valueOf(d).equals(Double.NEGATIVE_INFINITY)){
91-
builder.add(string, VTypeJsonMapper.NEG_INF);
92-
}
9380
return this;
9481
}
9582

epics-vtype/vtype-json/src/main/java/org/epics/vtype/json/VTypeJsonMapper.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,6 @@
4141
* @author carcassi
4242
*/
4343
class VTypeJsonMapper implements JsonObject {
44-
45-
public static final String NAN = Double.toString(Double.NaN);
46-
/**
47-
* Quoted version of {@link Double#NaN}. Needed for comparison when de-serializing,
48-
* as serialization of {@link Double#NaN} creates a quotes string.
49-
*/
50-
public static final String NAN_QUOTED = "\"" + NAN + "\"";
51-
public static final String POS_INF = Double.toString(Double.POSITIVE_INFINITY);
52-
/**
53-
* Quoted version of {@link Double#POSITIVE_INFINITY}. Needed for comparison when de-serializing,
54-
* as serialization of {@link Double#POSITIVE_INFINITY} creates a quotes string.
55-
*/
56-
public static final String POS_INF_QUOTED = "\"" + POS_INF + "\"";
57-
public static final String NEG_INF = Double.toString(Double.NEGATIVE_INFINITY);
58-
/**
59-
* Quoted version of {@link Double#NEGATIVE_INFINITY}. Needed for comparison when de-serializing,
60-
* as serialization of {@link Double#NEGATIVE_INFINITY} creates a quotes string.
61-
*/
62-
public static final String NEG_INF_QUOTED = "\"" + NEG_INF + "\"";
6344

6445
private final JsonObject json;
6546

epics-vtype/vtype-json/src/main/java/org/epics/vtype/json/VTypeToJsonV1.java

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import org.epics.util.number.ULong;
1313
import org.epics.util.number.UShort;
1414
import org.epics.vtype.EnumDisplay;
15-
import org.epics.vtype.VDouble;
1615
import org.epics.vtype.VEnum;
1716
import org.epics.vtype.VNumber;
1817
import org.epics.vtype.VNumberArray;
@@ -79,15 +78,11 @@ static JsonObject toJson(VType vType) {
7978
throw new UnsupportedOperationException("Not implemented yet");
8079
}
8180

82-
static VType toVNumber(JsonObject json) {
81+
static VNumber toVNumber(JsonObject json) {
8382
VTypeJsonMapper mapper = new VTypeJsonMapper(json);
8483
Number value;
8584
switch(mapper.getTypeName()) {
8685
case "VDouble":
87-
Double doubleValue = getDoubleFromJsonString(json.get("value").toString());
88-
if(doubleValue != null){
89-
return VDouble.of(doubleValue, mapper.getAlarm(), mapper.getTime(), mapper.getDisplay());
90-
}
9186
value = mapper.getJsonNumber("value").doubleValue();
9287
break;
9388
case "VFloat":
@@ -212,24 +207,4 @@ static JsonObject toJson(VEnum vEnum) {
212207
.addEnum(vEnum)
213208
.build();
214209
}
215-
216-
/**
217-
* Converts an input (JSON) string to a {@link Double} for the special cases
218-
* {@link Double#NaN}, {@link Double#POSITIVE_INFINITY} and {@link Double#NEGATIVE_INFINITY}.
219-
* @param valueAsString
220-
* @return <code>null</code> if input is <code>null</code> or a valid double number, otherwise
221-
* {@link Double#NaN}, {@link Double#POSITIVE_INFINITY} and {@link Double#NEGATIVE_INFINITY}.
222-
*/
223-
public static Double getDoubleFromJsonString(String valueAsString){
224-
if(VTypeJsonMapper.NAN_QUOTED.equals(valueAsString)){
225-
return Double.NaN;
226-
}
227-
else if(VTypeJsonMapper.POS_INF_QUOTED.equals(valueAsString)){
228-
return Double.POSITIVE_INFINITY;
229-
}
230-
else if(VTypeJsonMapper.NEG_INF_QUOTED.equals(valueAsString)){
231-
return Double.NEGATIVE_INFINITY;
232-
}
233-
return null;
234-
}
235210
}

epics-vtype/vtype-json/src/test/java/org/epics/vtype/json/VTypeToJsonTest.java

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import java.time.Instant;
1212
import java.util.Arrays;
1313
import java.util.Collections;
14-
import java.util.List;
1514
import javax.json.Json;
1615
import javax.json.JsonObject;
1716
import javax.json.JsonReader;
@@ -289,45 +288,5 @@ public void vByteArray1() {
289288
testDeserialization("VByteArray1", vByteArray1);
290289
testDeserialization("VByteArray1a", vByteArray1);
291290
}
292-
293-
/**
294-
* Tests serialization and de-serialization of a Double.NaN. See {@link JsonVTypeBuilder#add} and
295-
* {@link VTypeToJsonV1#toVNumber}
296-
*/
297-
@Test
298-
public void testDoubleNaN(){
299-
VDouble vDouble = VDouble.of(Double.NaN, Alarm.none(), Time.of(Instant.EPOCH), Display.none());
300-
JsonObject jsonObject = VTypeToJson.toJson(vDouble);
301-
assertEquals("NaN", jsonObject.getString("value"));
302-
vDouble = (VDouble)VTypeToJson.toVType(jsonObject);
303-
assertTrue(Double.isNaN(vDouble.getValue()));
304-
}
305-
306-
@Test
307-
public void testDoublePositiveInfinity(){
308-
VDouble vDouble = VDouble.of(Double.POSITIVE_INFINITY, Alarm.none(), Time.of(Instant.EPOCH), Display.none());
309-
JsonObject jsonObject = VTypeToJson.toJson(vDouble);
310-
assertEquals(Double.toString(Double.POSITIVE_INFINITY), jsonObject.getString("value"));
311-
vDouble = (VDouble)VTypeToJson.toVType(jsonObject);
312-
assertTrue(vDouble.getValue().equals(Double.POSITIVE_INFINITY));
313-
}
314-
315-
@Test
316-
public void testDoubleNegativeInfinity(){
317-
VDouble vDouble = VDouble.of(Double.NEGATIVE_INFINITY, Alarm.none(), Time.of(Instant.EPOCH), Display.none());
318-
JsonObject jsonObject = VTypeToJson.toJson(vDouble);
319-
assertEquals(VTypeJsonMapper.NEG_INF, jsonObject.getString("value"));
320-
vDouble = (VDouble)VTypeToJson.toVType(jsonObject);
321-
assertTrue(vDouble.getValue().equals(Double.NEGATIVE_INFINITY));
322-
}
323-
324-
@Test
325-
public void testDoubleNaNInArray(){
326-
VDoubleArray vDoubleArray1 = VDoubleArray.of(ArrayDouble.of(0, Double.NaN, 2), Alarm.none(), Time.of(Instant.ofEpochSecond(0, 0)), Display.none());
327-
JsonObject jsonObject = VTypeToJson.toJson(vDoubleArray1);
328-
List valueObject = (List)jsonObject.get("value");
329-
assertEquals(VTypeJsonMapper.NAN_QUOTED, valueObject.get(1).toString());
330-
vDoubleArray1 = (VDoubleArray)VTypeToJson.toVType(jsonObject);
331-
assertTrue(Double.isNaN(vDoubleArray1.getData().getDouble(1)));
332-
}
291+
333292
}

0 commit comments

Comments
 (0)