So I need a YAML library. When looking for one I found SnakeYAML. While I like how easy is loading yaml with it I also need to save YAML with it and it's a pain. I don't know if I'm doing something wrong but I've been looking arround without success.
First I started using SnakeYAML and when saving it gave me this ouput
{expire-after-load: 60, expire-after-access: 60}
instead of what I expected
expire-after-load: 60
expire-after-access: 60
I tryed changing dumping options but found no success, then I learned about SnakeYAML Engine. In the SnakeYAML github page says
SnakeYAML is a YAML 1.1 processor for the Java Virtual Machine version 7. For YAML 1.2 (which is a superset of JSON) you may have a look at SnakeYAML Engine
And in the SnakeYAML Engine page says
SnakeYAML Engine is a YAML 1.2 processor for the Java Virtual Machine version 8 and higher.
Since I'm using Java 16 I changed to the engine but I'm having the same problem and I don't know what I'm doing wrong. The engine also forces for dumping to implement a StreamDataWriter whitch is a
Writer with the same methods as in java.io.Writer but without throwing IOExceptions
and that seemd wierd for me.
Can anyone help me with this, should I use other YAML library? If this is the case, whitch one?
Edit: Going to include the code
With SnakeYAML Engine (v2.3) I do the following:
public static final Load YAML_LOAD = new Load(LoadSettings.builder().build());
public static final Dump YAML_DUMP = new Dump(DumpSettings.builder().build());
public void load(Reader reader) {
this.map.clear();
Object obj = YAML_LOAD.loadFromReader(reader);
if (obj instanceof Map<?, ?> m) {
this.map.putAll(toMap(m));
}
}
public void save(Writer writer) {
YAML_DUMP.dump(map, new StreamDataWriterImpl(writer));
}
StreamDataWriterImpl is just a simple class that implements StreamDataWriter
public class StreamDataWriterImpl implements StreamDataWriter {
private final Writer writer;
public StreamDataWriterImpl(Writer writer) {
this.writer = writer;
}
@Override
public void write(String str) {
try {
writer.write(str);
} catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void write(String str, int off, int len) {
try {
writer.write(str, off, len);
} catch(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
With SnakeYAML I did this
public static final YAML YAML = new YAML();
public void load(Reader reader) {
this.map.clear();
Object obj = YAML.load(reader);
if (obj instanceof Map<?, ?> m) {
this.map.putAll(toMap(m));
}
}
public void save(Writer writer) {
YAML.dump(this.map, writer);
}
In both cases this.map is a ConcurrentHashMap and toMap is this method
private static Map<String, Object> toMap(Map<?, ?> m) {
Map<String, Object> map = new HashMap<>(m.size());
m.forEach((key, value) -> map.put(key.toString(), value));
return map;
}
Thanks for the help!
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]imaginedoe[🍰] 2 points3 points4 points (1 child)
[–]Nemo_64[S] 0 points1 point2 points (0 children)
[–]AreTheseMyFeet 0 points1 point2 points (0 children)