package org.nd4j.serde.gson;

import com.google.common.primitives.Ints;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.factory.Nd4j;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class GsonDeserializationUtils {
    private static final JsonParser JSON_PARSER = new JsonParser();

    public static INDArray deserializeRawJson(String serializedRawArray) {
        JsonArray jsonArray = JSON_PARSER.parse(serializedRawArray).getAsJsonArray();

        List<Integer> dimensions = new ArrayList<>();
        dimensions.add(jsonArray.size());
        getSizeMultiDimensionalArray(jsonArray, dimensions);

        if (isArrayWithSingleRow(dimensions)) {
            dimensions.add(0, 1);
        }

        return buildArray(dimensions, serializedRawArray);
    }

    private static void getSizeMultiDimensionalArray(JsonArray jsonArray, List<Integer> dimensions) {
        Iterator<JsonElement> iterator = jsonArray.iterator();

        if (iterator.hasNext()) {
            JsonElement jsonElement = iterator.next();
            if (jsonElement.isJsonArray()) {
                JsonArray shapeArray = jsonElement.getAsJsonArray();
                dimensions.add(shapeArray.size());
                getSizeMultiDimensionalArray(shapeArray, dimensions);
            }
        }
    }

    private static boolean isArrayWithSingleRow(List<Integer> dimensions) {
        return dimensions.size() == 1;
    }

    private static INDArray buildArray(List<Integer> dimensions, String rawArray) {
        int[] shape = Ints.toArray(dimensions);
        String[] entries = rawArray.replaceAll("\\[", "").replaceAll("]", "").replaceAll("\\n", "").split(",");
        double[] entryValues = new double[entries.length];

        for (int i = 0; i < entries.length; i++) {
            entryValues[i] = Double.parseDouble(entries[i]);
        }

        return Nd4j.create(entryValues, shape);
    }
}

@@ -170,16 +170,61 @@
 Parser;%0A
+import org.apache.commons.lang3.StringUtils;%0A
 import o
@@ -1769,76 +1769,60 @@
 ies 
+ 
 = 
-rawArray.replaceAll(%22%5C%5C%5B%22, %22%22).replaceAll(%22%5D%22, %22%22).replaceAll(%22
+StringUtils.replacePattern(rawArray, %22%5B%5C%5C%5B%5C%5C%5D
 %5C%5Cn
+%5D
 %22, %22

Use a single regex to replace the array characters