private JsonValue addInternal()

in johnzon-core/src/main/java/org/apache/johnzon/core/JsonPointerImpl.java [364:418]


    private JsonValue addInternal(JsonValue jsonValue, JsonValue newValue, List<String> currentPath,
                                  boolean check) {
        if (jsonValue.getValueType() == JsonValue.ValueType.OBJECT) {
            JsonObject jsonObject = jsonValue.asJsonObject();
            if (!check) {
                return provider.createObjectBuilder(jsonObject).build();
            }

            JsonObjectBuilder objectBuilder = provider.createObjectBuilder();

            if (jsonObject.isEmpty() && isPositionToAdd(currentPath)) {
                objectBuilder.add(referenceTokens.get(referenceTokens.size() - 1), newValue);
            } else {
                for (Map.Entry<String, JsonValue> entry : jsonObject.entrySet()) {

                    currentPath.add(entry.getKey());
                    objectBuilder.add(entry.getKey(), addInternal(entry.getValue(), newValue, currentPath, canMatch(currentPath)));
                    currentPath.remove(entry.getKey());

                    if (isPositionToAdd(currentPath)) {
                        objectBuilder.add(referenceTokens.get(referenceTokens.size() - 1), newValue);
                    }
                }
            }
            return objectBuilder.build();
        } else if (jsonValue.getValueType() == JsonValue.ValueType.ARRAY) {
            JsonArray jsonArray = jsonValue.asJsonArray();
            if (!check) {
                return provider.createArrayBuilder(jsonArray).build();
            }
            JsonArrayBuilder arrayBuilder = provider.createArrayBuilder();

            int arrayIndex = -1;
            if (isPositionToAdd(currentPath)) {
                arrayIndex = getArrayIndex(referenceTokens.get(referenceTokens.size() - 1), jsonArray, canMatch(currentPath));
            }

            int jsonArraySize = jsonArray.size();
            for (int i = 0; i <= jsonArraySize; i++) {
                if (i == arrayIndex) {
                    arrayBuilder.add(newValue);
                }
                if (i == jsonArraySize) {
                    break;
                }

                String path = String.valueOf(i);
                currentPath.add(path);
                arrayBuilder.add(addInternal(jsonArray.get(i), newValue, currentPath, canMatch(currentPath)));
                currentPath.remove(path);
            }
            return arrayBuilder.build();
        }
        return jsonValue;
    }