Skip to content

GH-10058: Add SpEL JSON accessors and converter with Jackson 3 #10203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 16, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ configure(javaProjects) { subproject ->

tasks.withType(Javadoc) {
options.addBooleanOption('Xdoclint:syntax', true) // only check syntax with doclint
options.addBooleanOption('Werror', true) // fail build on Javadoc warnings
// TODO until deprecation cleanup options.addBooleanOption('Werror', true) // fail build on Javadoc warnings
}

tasks.withType(JavaForkOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2025-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.integration.json;

import org.jspecify.annotations.Nullable;
import tools.jackson.databind.node.ArrayNode;

import org.springframework.expression.AccessException;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.IndexAccessor;
import org.springframework.expression.TypedValue;

/**
* A SpEL {@link IndexAccessor} that knows how to read indexes from JSON arrays, using
* Jackson's {@link ArrayNode} API.
*
* <p>Supports indexes supplied as an integer literal &mdash; for example, {@code myJsonArray[1]}.
* Also supports negative indexes &mdash; for example, {@code myJsonArray[-1]} which equates
* to {@code myJsonArray[myJsonArray.length - 1]}. Furthermore, {@code null} is returned for
* any index that is out of bounds (see {@link ArrayNode#get(int)} for details).
*
* @author Jooyoung Pyoung
*
* @since 7.0
* @see JsonNodePropertyAccessor
*/
public class JsonArrayNodeIndexAccessor implements IndexAccessor {

private static final Class<?>[] SUPPORTED_CLASSES = { ArrayNode.class };

@Override
public Class<?>[] getSpecificTargetClasses() {
return SUPPORTED_CLASSES;
}

@Override
public boolean canRead(EvaluationContext context, Object target, Object index) {
return (target instanceof ArrayNode && index instanceof Integer);
}

@Override
public TypedValue read(EvaluationContext context, Object target, Object index) throws AccessException {
ArrayNode arrayNode = (ArrayNode) target;
Integer intIndex = (Integer) index;
if (intIndex < 0) {
// negative index: get from the end of array, for compatibility with JsonNodePropertyAccessor.ArrayNodeAsList.
intIndex = arrayNode.size() + intIndex;
}
return JsonNodePropertyAccessor.typedValue(arrayNode.get(intIndex));
}

@Override
public boolean canWrite(EvaluationContext context, Object target, Object index) {
return false;
}

@Override
public void write(EvaluationContext context, Object target, Object index, @Nullable Object newValue) {
throw new UnsupportedOperationException("Write is not supported");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@
* any index that is out of bounds (see {@link ArrayNode#get(int)} for details).
*
* @author Sam Brannen
*
* @since 6.4
* @see JsonPropertyAccessor
* @deprecated Since 7.0 in favor of {@link JsonArrayNodeIndexAccessor} for Jackson 3.
*/
@Deprecated(forRemoval = true, since = "7.0")
public class JsonIndexAccessor implements IndexAccessor {

private static final Class<?>[] SUPPORTED_CLASSES = { ArrayNode.class };
Expand Down
Loading
Loading