fix(serialize-xml): custom path generation

This commit is contained in:
Johannes Frohnmeyer 2024-04-20 12:32:24 +02:00
parent de55636d20
commit b8d9b52ab5
Signed by: Johannes
GPG Key ID: E76429612C2929F4
1 changed files with 29 additions and 2 deletions

View File

@ -369,14 +369,41 @@ public class XmlReader extends SerializeReader<IOException, XmlReader> implement
return result;
}
private String getPath(boolean usePreviousPath) {
if (nextTagNamePath != null) return nextTagNamePath;
StringBuilder result = new StringBuilder().append('$');
for (int i = 0; i < stackSize; i++) {
int scope = stack[i];
switch (scope) {
case WrapperScope.ARRAY -> {
int pathIndex = pathIndices[i];
// If index is last path element it points to next array element; have to decrement
if (usePreviousPath && pathIndex > 0 && i == stackSize - 1) {
pathIndex--;
}
result.append('[').append(pathIndex).append(']');
}
case WrapperScope.OBJECT -> {
result.append('.');
if (pathNames[i] != null) {
result.append(pathNames[i]);
}
}
case WrapperScope.OBJECT_VALUE_WRAPPER, WrapperScope.OBJECT_VALUE_WRAPPER_USED, WrapperScope.DOCUMENT -> {}
default -> throw new AssertionError("Unknown scope value: " + scope);
}
}
return result.toString();
}
@Override
public String getPath() {
return nextTagName == null ? reader.getPath() : nextTagNamePath;
return getPath(false);
}
@Override
public String getPreviousPath() {
return getPath(); // TODO this should be different when handling arrays
return getPath(true);
}
@Override