External Nodes
ExternalNode 适配器可以将第三方 JSON 树接入 SJF4J 的 OBNT (Object-Based Node Tree) 模型。结构化 API 直接操作原生对象,无需转换成 Map、List 或 POJO。
适配器契约
ExternalNode<N, C> 分别声明被适配节点类型(N)和直接子节点的公共类型(C)。
| 表示形式 | 适配器 |
|---|---|
| 同构 AST | ExternalNode<JsonElement, JsonElement> |
| 异构对象 | ExternalNode<Document, Object> |
| 纯标量节点 | ExternalNode<MyString, Object> |
适配器必须声明 nodeType()、childType() 和 jsonType(),其他操作取决于支持的形态。同一发现作用域内,适配器根类型不得重叠,每种节点类型最多匹配一个适配器。原生 OBNT 类型不能作为适配器根类型。
- 结构读取保留原生表示。存在的 JSON null 可以是 Java
null或原生 null 节点;通过 presence 信息区分节点缺失。 - 待操作节点参数非空。Mapping 和 Binding 由框架处理 null 转换;
createValueNode()只接受非空标量。若适配器支持原生 JSON null,其toString()、toNumber()和toBoolean()对该节点必须返回 Javanull。同类型直接返回及复制保留原生节点。 - 写入 Java
null时采用容器自身的 JSON null 表示。 - 默认
convertContainer()将子节点转换为childType();不可变容器或有特殊成员约束的适配器可以覆盖该方法。
支持的原生模型
| 第三方库 | 原生节点类型 |
|---|---|
| Jackson 2.x | com.fasterxml.jackson.databind.JsonNode |
| Jackson 3.x | tools.jackson.databind.JsonNode |
| Gson | com.google.gson.JsonElement |
| JSON-P | jakarta.json.JsonValue |
对应的 SJF4J 后端模块已提供适配器。依赖配置参阅 Backends & Formats。
Gson 示例
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
JsonObject user = new JsonObject();
user.addProperty("name", "Alice");
System.out.println(JsonType.of(user)); // OBJECT
String name = Nodes.toString(Nodes.getInObject(user, "name")); // "Alice"
Nodes.putInObject(user, "active", new JsonPrimitive(true));user 仍然是 Gson 的 JsonObject。SJF4J 将类型识别、属性读取和修改委托给 Gson 适配器,不会构建中间 JSON 树。
是否可修改取决于原生模型。 Gson 的 JsonObject 和 JsonArray 可变;JSON-P 的 JsonObject 和 JsonArray 不可变,因此不支持原地写入。具体可用操作取决于适配器实现。
添加自定义 ExternalNode
下面用 Gson 作为第三方对象模型示例。SJF4J 已自带完整的 Gson 适配器,通常只需要引入 sjf4j-backend-gson。
不要将此示例与 sjf4j-backend-gson 同时注册:两个 JsonElement 适配器会发生冲突。
如果要独立运行示例,请仅添加 sjf4j-core 和 Gson 原生库,不要引入 sjf4j-backend-gson 或聚合模块 sjf4j。
1. 实现 ExternalNode
针对第三方库的根节点类型实现 ExternalNode<N, C>。实现三个类型方法及所支持形态需要的操作。
// ExampleGsonNode.java
package com.example.sjf4j;
import com.google.gson.JsonElement;
import com.google.gson.JsonNull;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import org.sjf4j.JsonType;
import org.sjf4j.exception.NodeException;
import org.sjf4j.external.ExternalNode;
public final class ExampleGsonNode implements ExternalNode<JsonElement, JsonElement> {
@Override
public Class<JsonElement> nodeType() {
return JsonElement.class;
}
@Override
public Class<JsonElement> childType() {
return JsonElement.class;
}
@Override
public JsonType jsonType(JsonElement node) {
if (node.isJsonNull()) return JsonType.NULL;
if (node.isJsonObject()) return JsonType.OBJECT;
if (node.isJsonArray()) return JsonType.ARRAY;
JsonPrimitive value = node.getAsJsonPrimitive();
if (value.isString()) return JsonType.STRING;
if (value.isNumber()) return JsonType.NUMBER;
if (value.isBoolean()) return JsonType.BOOLEAN;
return JsonType.UNKNOWN;
}
@Override
public JsonElement getInObject(JsonElement node, String key) {
if (!node.isJsonObject()) throw new NodeException("expected object node");
return node.getAsJsonObject().get(key);
}
@Override
public boolean containsInObject(JsonElement node, String key) {
if (!node.isJsonObject()) throw new NodeException("expected object node");
return node.getAsJsonObject().has(key);
}
@Override
public JsonElement putInObject(JsonElement node, String key, Object value) {
if (!node.isJsonObject()) throw new NodeException("expected object node");
if (value != null && !(value instanceof JsonElement)) {
throw new NodeException("expected a Gson JsonElement");
}
JsonObject object = node.getAsJsonObject();
JsonElement previous = object.get(key);
object.add(key, value == null ? JsonNull.INSTANCE : (JsonElement) value);
return previous;
}
@Override
public String toString(JsonElement node) {
if (node.isJsonNull()) return null;
if (node.isJsonPrimitive() && node.getAsJsonPrimitive().isString()) {
return node.getAsString();
}
throw new NodeException("expected a Gson string node");
}
@Override
public Number toNumber(JsonElement node) {
if (node.isJsonNull()) return null;
if (node.isJsonPrimitive() && node.getAsJsonPrimitive().isNumber()) {
return node.getAsNumber();
}
throw new NodeException("expected a Gson number node");
}
@Override
public Boolean toBoolean(JsonElement node) {
if (node.isJsonNull()) return null;
if (node.isJsonPrimitive() && node.getAsJsonPrimitive().isBoolean()) {
return node.getAsBoolean();
}
throw new NodeException("expected a Gson boolean node");
}
}这个最小适配器支持节点类型识别、直接读取或修改对象属性,以及标量提取。可以根据需要增加数组访问、遍历、复制和访问元数据等操作。未实现的操作会直接失败。
2. 提供适配器
// ExampleGsonProvider.java
package com.example.sjf4j;
import org.sjf4j.external.ExternalNode;
import org.sjf4j.external.ExternalProvider;
public final class ExampleGsonProvider implements ExternalProvider {
@Override
public ExternalNode<?, ?> externalNode() {
return new ExampleGsonNode();
}
}如果原生库是必需依赖,直接返回适配器即可。如果原生库是可选依赖,请在创建适配器前检测其可用性;只有在库缺失时才返回 null。Provider 类自身应当能够在未安装可选库的情况下加载。
3. 通过 SPI 注册
创建 src/main/resources/META-INF/services/org.sjf4j.external.ExternalProvider,写入 Provider 的完整类名:
com.example.sjf4j.ExampleGsonProviderExternalRegistry 在初始化时自动发现 Provider,SJF4J 将受支持的节点操作分派给匹配的适配器。
4. 使用适配器
在应用类路径中加入 Provider 后,前面的 Gson 示例 即可通过相同的 JsonType 和 Nodes API 工作,无需显式注册。
完整实现参阅 GsonNode 与 GsonExternalProvider。另请参阅 ExternalNode 和 OBNT 模型。
Compiled 使用方式
在 Compiled 接口中直接声明适配器,或通过父接口继承:
@CompiledNavigator
interface ExampleNavigator {
ExternalNode<JsonElement, JsonElement> GSON = new ExampleGsonNode();
@GetByPath("$.name")
JsonElement name(JsonElement root);
}该声明仅对生成代码生效。Runtime 操作及 fallback 仍需按上文通过 SPI 注册。参阅 Compiled APIs。
API 参考(最新发布版本): ExternalNode · ExternalProvider · ExternalRegistry