【问题标题】:RedisGraph: how to persist properties in data containing BOTH single AND double quotes?RedisGraph:如何在包含单引号和双引号的数据中保留属性?
【发布时间】:2022-11-12 10:57:26
【问题描述】:

我正在测试 RedisGraph 作为将来自客户端的数据存储为 JSON 的一种方式。

JSON 通过 bean 进行验证等,我使用 Jackson 序列化 bean,因此 RedisGraph 字符串的格式正确。有关该格式化步骤的完整性,请参阅最后的示例代码。

数据属性可能包含有效 JSON 格式的单引号,例如:O'Toole

{ "name" : "Peter O'Toole", "desc" : "An actors actor" }

我可以根据最后的代码块使用格式化程序将 JSON 转换为 RedisGraph 命令将允许处理单引号的格式(我不需要转义数据内容 - 即它可以使用客户端发送的内容) .例如这有效:

GRAPH.QUERY movies "CREATE (:Actor {name:\"Peter O'Toole\", desc:\"An actors actor\", actor_id:1})"

到目前为止,一切都很好。

现在,问题是:我在保留原始 JSON 的语法时遇到了问题,它还包含转义的双引号。例如:

{ "name" : "Peter O'Toole", "desc" : "An \"actors\" actor" }

我不想转义或包装 desc 属性值,因为它已经被转义为有效的 JSON。但是,我如何构造 RedisGraph 命令,以便它使用给定的值保留属性?即包含转义的双引号。

换句话说,由于 desc 属性中的 \",这会引发解析错误。

GRAPH.QUERY movies "CREATE (:Actor {name:\"Peter O'Toole\", desc:\"An \"actors\" actor\", actor_id:1})"

鉴于想要保留包含有效 JSON 转义双引号 \" 和未转义单引号的数据是很常见的,因此必须有一种方法可以做到这一点。例如姓名和地址数据。

有任何想法吗?

谢谢,默里。

PS:这也不起作用:它阻塞了 O'Toole 中的嵌入式 '

GRAPH.QUERY movies "CREATE (:Actor {name:\'Peter O'Toole\', desc:\'an \"actors\" actor\', actor_id:3})"
// \u007F is the "delete" character.
// This is the highest char value Jackson allows and is
// unlikely to be in the JSON (hopefully!)
JsonFactory builder = new JsonFactoryBuilder().quoteChar('\u007F').build();

ObjectMapper objectMapper = new ObjectMapper(builder);

// Set pretty printing of json
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

// Do not surround property names with quotes. ie { firstName : "Peter" }
objectMapper.configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(), false);

// Make a Person
Person person = new Person("Peter", "O'Toole");

// Set the desc property using embedded quotes
person.setDesc("An \"actors\" actor");

// Convert Person to JSON
String json = objectMapper.writeValueAsString(person);

// Now convert your json to escape the double quotes around the string properties:
String j2 = json.replaceAll("\u007F", "\\\\\"");

System.out.println(j2);

这产生:

{
  firstName : \"Peter\",
  lastName : \"O'Toole\",
  desc : \"An \"actors\" actor\"
}

这是 Redis GRAPH.QUERY movies "CREATE..." 可以使用的格式(除了上面讨论的 \"actors\" 的问题)。

【问题讨论】:

  • 这不是为你工作吗? GRAPH.QUERY movies 'CREATE (:Actor {name:"Peter O\'Toole", desc:"An \"actors\" actor", actor_id:1})'
  • 谢谢。正如你所建议的,只要我使用O\'Toole,语法就可以了。我的帖子的重点是带有双引号的传入数据属性被转义了\"actors\",但带有单引号的传入属性不是,即O'Toole

标签: java redis redisgraph


【解决方案1】:

好的。问题是试图通过直接将命令输入 RedisInsight 来测试语法的人工制品。事实证明,所有需要做的就是从有效的 json 中删除双引号。

因此,需要明确的是,基于来自客户端应用程序的正常有效 json, 格式化程序测试是:

ObjectMapper objectMapper = new ObjectMapper();

// (Optional) Set pretty printing of json
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);

// Do not surround property names with quotes. ie { firstname : "Peter" }
objectMapper.configure(JsonWriteFeature.QUOTE_FIELD_NAMES.mappedFeature(), false);

// Make a Person
// For this example this is done directly, 
// although in the Java this is done using
// objectMapper.readValue(incomingJson, Person.class)

Person person = new Person("Peter", "O'Toole");

// Set the desc property using escaped double quotes
person.setDesc("An "actor's" actor");

// Convert Person to JSON without quoted property names
String json = objectMapper.writeValueAsString(person);

System.out.println(json);

产量:

{
  firstname : "Peter",
  lastname : "O'Toole",
  desc : "An "actor's" actor"
}

命令字符串由 Vertx Redis 使用:

Vertx vertx = Vertx.vertx();
private final Redis redisClient;

// ...

redisClient = Redis.createClient(vertx);

String cmdStr = "CREATE (:Actor {firstname:"Peter", lastname: "O'Toole", desc:"An "actor's" actor", actor_id:1})";

Future<String> futureResponse = redisClient.send(Request.cmd(Command.GRAPH_QUERY).arg("movies").arg(cmdStr))
    .compose(response -> {
      Log.info("createRequest response=" + response.toString());

      return Future.succeededFuture("OK");
    })
    .onFailure(failure -> {
      Log.error("createRequest failure=" + failure.toString());
    });

:-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-27
    • 1970-01-01
    • 2018-09-02
    • 2021-09-28
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多