Using the Dgraph Java setSetNquads() method, how do you insert UTF-8 with escape characters?

Hello,
I’m verifying that I’m escaping what needs to be escaped, but not over-escaping characters when sending them into DGraph via the Java Client’s setSetNquads() method.

Reading from here: java - StringEscapeUtils not handling utf-8 - Stack Overflow, is what needs to be escaped. Is there a better reference in the docs? I couldn’t fine one… The list for easy reference:

Backspace is replaced with \b
Form feed is replaced with \f
Newline is replaced with \n
Carriage return is replaced with \r
Tab is replaced with \t
Double quote is replaced with \"
Backslash is replaced with \\

Pseudo code to illustrate the UTF-8 encoding complexity:
Attempt 1:

//Bob in Simplified Chinese, per Google Translate, with an extra double-quote tossed in.
String nickname = "鲍\"勃";
Mutation mu1 = DgraphProto.Mutation.newBuilder()
   .setSetNquads(ByteString.copyFromUtf8("<0x4e43> <nickname> \"" + nickname + "\" .")).build();

Mutation 1’s RDF statement is: <0x4e43> <nickname> "鲍"勃" .<— Obviously malformed with an extra quote, yields a RunTimeException: UNKNOWN: while lexing <0x4e43> <nickname> "鲍"勃" . at line 1 column 25: Invalid input: 勃 at lexText

Attempt 2:

//Bob in Simplified Chinese, per Google Translate, with an extra double-quote tossed in.
String nickname = "\u0B01\"\u4F2F" = StringEscapeUtils.escapeJava("鲍\"勃");
Mutation mu2 = DgraphProto.Mutation.newBuilder()
   .setSetNquads(ByteString.copyFromUtf8("<0x4e43> <nickname> \"" + nickname + "\" .")).build();

Mutation 2’s RDF statement is: <0x4e43> <nickname> "\u0B01\"\u4F2F . <— This succeeds, however now we’re storing the UTF-8 ascii representation in Dgraph, when Dgraph can handle storing pure UTF-8.

Attempt 3, using this approach: java - StringEscapeUtils not handling utf-8 - Stack Overflow

//Bob in Simplified Chinese, per Google Translate, with an extra double-quote tossed in.
String nickname = StringEscapeUtils.escapeJava("鲍\"勃");
nickname = new UnicodeUnescaper().translate(nickname);
Mutation mu3 = DgraphProto.Mutation.newBuilder()
   .setSetNquads(ByteString.copyFromUtf8("<0x4e43> <nickname> \"" + nickname + "\" .")).build();

Mutation 3’s RDF statement is: <0x4e43> <nickname> "鲍\"勃" . <— Ideal situation

This 3rd attempt seems what you’d want to store in Dgraph, but feels really clunky having to call StringEscapeUtils and UnicodeUnescaper.

  1. Is there a better way to do this?
  2. Is pure UTF-8 the ‘better’ thing to store in DGraph vs the Ascii representation of the characters?

Thanks,
Ryan