简体   繁体   中英

Write special characters to yaml file

im using snakeyaml 1.27

i want to write special characters to a yaml file:

section.put("Success", "\uD83D\uDE03"); 😃
section.put("Warning", "\uD83D\uDE2E"); 😮
section.put("Error", "\uD83D\uDE26"); 😦

but the output is either

  Success: ?
  Warning: ?
  Error: ?

or

  Success: "\U0001f603"
  Warning: "\U0001f62e"
  Error: "\U0001f626"

if i change DumperOptions().setAllowUnicode(true/false);

If i just read and write a yaml the same thing appears. its able to read the 😃 but not write it back.

code:

DumperOptions o = new DumperOptions();
o.setPrettyFlow(true);
o.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
o.setAllowUnicode(true);

Yaml yaml = new Yaml(o);
InputStream is = new FileInputStream(file);
Map<String, Object> data = yaml.load(is);
is.close();

StringWriter writer = new StringWriter();
yaml.dump(data, writer);

FileWriter fw = new FileWriter(file);
fw.write(writer.toString());
fw.close();

yaml before:

Bot_Token: f1432d2asdummy
Owner_ID: '123456'
Command_Trigger: '-'
Game: Ready  playing music. !Play
Emojis:
  Success: 😃
  Warning: 😮
  Error: 😦

yaml after:

Bot_Token: f1432d2asd3dummy
Owner_ID: '123456'
Command_Trigger: '-'
Game: Ready  playing music. !Play
Emojis:
  Success: ?
  Warning: ?
  Error: ?

Replace FileWriter With newBufferdWriter()

DumperOptions o = new DumperOptions();
o.setPrettyFlow(true);
o.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
o.setAllowUnicode(true);

Yaml yaml = new Yaml(o);
InputStream is = new FileInputStream(file);

Map<String, Object> data = yaml.load(is);
is.close();

StringWriter writer = new StringWriter();
yaml.dump(data, writer);

BufferedWriter bw = Files.newBufferedWriter(file.toPath());
bw.write(writer.toString());
bw.close();

YAML is a text format. This means you can't write any binary into a YAML file safely, you can only write printable characters.

If you truly want to write binary, either Base64 encode the binary, or write a string of hexidecimal bytes, and have your other programs expect the format you chose.

Since it looks like you want to write emoji characters, you are writing text, not binary; but, your text is encoded in one of the UTF formats (probably UTF8). YAML supports most of the UTF formats (possibly all of them); but, to make sure your YAML is seens as UTF, you will need to add a BOM (byte order mark) appropriate to the desired format. This is typically not retained in the "read data" so perhaps the only issue you have is not recreating it / writing it to the output stream.

Look up BOM and open the input and output in hex editors. If this is the case, then you'll be able to find plenty of other articles on how to write the BOM of your choice to an output stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM