11 Eylül 2023 Pazartesi

SnakeYaml Engine

Maven
Şu satırı dahil ederiz
<dependency>
  <groupId>org.snakeyaml</groupId>
  <artifactId>snakeyaml-engine</artifactId>
  <version>2.7</version>
</dependency>
Load  Sınıfı
loadFromString metodu
Örnek
Şöyle yaparız
import org.snakeyaml.engine.v2.api.Load;
import org.snakeyaml.engine.v2.api.LoadSettings;
import org.snakeyaml.engine.v2.api.LoadSettingsBuilder;
import org.snakeyaml.engine.v2.nodes.Node;
import org.snakeyaml.engine.v2.parser.ParserImpl;

public static void main(String[] args) {
  // Create a YAML string to parse
  String yamlString = "name: John\nage: 30";

  // Create a LoadSettings instance with your desired settings (optional)
  LoadSettings settings = new LoadSettingsBuilder().build();

  // Create a Load instance
  Load load = new Load(settings);

  / Parse the YAML string
  Node node = load.loadFromString(yamlString);

  // You can work with the parsed YAML data here
  System.out.println(node);
 
}
Dump Sınıfı
dumpToString metodu
Örnek
Şöyle yaparız
import org.snakeyaml.engine.v2.api.Dump;
import org.snakeyaml.engine.v2.api.DumpSettings;
import org.snakeyaml.engine.v2.api.DumpSettingsBuilder;
import org.snakeyaml.engine.v2.nodes.Node;


public static void main(String[] args) {
  // Create your YAML data as a SnakeYAML Node
  Node yamlData = ...; // Create your Node here

  // Create a DumpSettings instance with your desired settings (optional)
  DumpSettings settings = new DumpSettingsBuilder().build();

  // Create a Dump instance
  Dump dump = new Dump(settings);

  // Generate YAML from the Node
  String generatedYAML = dump.dumpToString(yamlData);

  // You can work with the generated YAML string here
  System.out.println(generatedYAML);
}

Hiç yorum yok:

Yorum Gönder