VelocyPack VPackParser returns 来自 toJson 方法的 null _key

VelocyPack VPackParser returns null _key from toJson method

我在名为 test 的集合中有以下文档:

[
  {
    "_key": "2469",
    "_id": "test/2469",
    "_rev": "_Ujegqfu---",
    "fieldName": "some value"
  }
]

我正在使用以下两种方法检索它:

public Result helloWorldJson() {

    ArangoDB db = new ArangoDB.Builder().user("<user>").password("<pass>").build();
    VPackParser parser = new VPackParser() ;

    VPackSlice slice = db.db("<db>").collection("test").getDocument("2469", VPackSlice.class);      
    String json = db.db("cms").collection("test").getDocument("2469", String.class);

    return Results.text().render("{velocy: " + parser.toJson(slice, true) + ", json: " + json);
}

产生此输出:

{velocy: {"_key":"2469","_id":null,"_rev":"_Ujegqfu---","fieldName":"some value"}, json: {"_key":"2469","_id":"test\/2469","_rev":"_Ujegqfu---","fieldName":"some value"}

VPackParser 是故意留下 _id null 还是我遗漏了什么?

存储文档中的 _id 字段来自特殊的 velocypack 类型,Json 不支持该类型并包含 collection-id,文档存储在其中。 要在人类可读的 "collection-name/document-key" 中正确反序列化该字段,反序列化过程需要知道给定 collection-id 的 collection-name。这只有在进程可以调用数据库或 java-driver 的内部 collection-cache 时才有可能。只有当您调用 getDocument(String,Type) 或其他 API 方法时,反序列化过程才能访问它。 VPackParser 是一个独立的 Velocypack<->Json 解析器,它无法解析字段 _id.

String json = db.db("cms").collection("test").getDocument("2469", String.class);

在您的调用中,当您将 type 设置为 String 时,将使用 VPackParser 实例上的方法 toJson(),该方法可以访问数据库和collection-cache 等在将 velocypack 解析为 json.

时可以正确反序列化字段 _id

如果您想从 api 调用中单独反序列化 Velocypack(正确解析 _id),您可以使用从中获得的 class ArangoUtil ArangoDBArangoDatabaseArangoCollection.

上的方法 util()
VPackSlice slice = db.db("<db>").collection("test").getDocument("2469", VPackSlice.class);
String json = db.util().deserialize(slice, String.class);