Permalink
Browse files

Remove commas from between numbers so json parser parses correctly

  • Loading branch information...
1 parent 507ca27 commit 5e25441c8fbcaf35278e0dd3333102c9c1784132 @jcastaldoFoodEssentials jcastaldoFoodEssentials committed Jan 4, 2017
@@ -16,7 +16,8 @@
private static final JsonParser JSON_PARSER = new JsonParser();
public static INDArray deserializeRawJson(String serializedRawArray) {
- JsonArray jsonArray = JSON_PARSER.parse(serializedRawArray).getAsJsonArray();
+ String cleanedRawArray = serializedRawArray.replaceAll("(?<=[\\d])(,)(?=[\\d])", "");
+ JsonArray jsonArray = JSON_PARSER.parse(cleanedRawArray).getAsJsonArray();
List<Integer> dimensions = new ArrayList<>();
dimensions.add(jsonArray.size());
@@ -33,7 +34,7 @@ public static INDArray deserializeRawJson(String serializedRawArray) {
dimensions.add(0, 1);
}
- return buildArray(dimensions, serializedRawArray);
+ return buildArray(dimensions, cleanedRawArray);
}
/*
@@ -76,6 +76,19 @@ public void deserializeRawJson_ArrayIsRankFive_ExpectCorrectDeserialization() {
assertEquals(expectedArray, array);
}
+ @Test
+ public void deserializeRawJson_HaveCommaInsideNumbers_ExpectCorrectDeserialization() {
+ String serializedRawArray =
+ "[[1.00, 1,100.00, 3.00],\n" +
+ "[13.00, 5.00, 15,591.00],\n" +
+ "[7,000.00, 17.00, 9.00]]";
+ INDArray expectedArray = Nd4j.create(new double[] { 1, 1100, 3, 13, 5, 15591, 7000, 17, 9 }, new int[] { 3, 3 });
+
+ INDArray indArray = GsonDeserializationUtils.deserializeRawJson(serializedRawArray);
+
+ assertEquals(expectedArray, indArray);
+ }
+
private INDArray buildExpectedArray(int numberOfTripletRows, int... shape) {
INDArray expectedArray = Nd4j.create(3 * numberOfTripletRows, 3);
for(int i = 0; i < numberOfTripletRows; i++) {

0 comments on commit 5e25441

Please sign in to comment.