YAMLファイルの読み込みサンプルプログラム
Last active
July 13, 2018 08:43
-
-
Save tasukujp/a5e0ef75495fc4d73ea6 to your computer and use it in GitHub Desktop.
SnakeYAMLの使い方
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- Hesperiidae | |
- Papilionidae | |
- Apatelodidae | |
- Epiplemidae |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
Time: 2001-11-23 15:01:42 -5 | |
User: ed | |
Warning: | |
This is an error message | |
for the log file | |
--- | |
Time: 2001-11-23 15:02:31 -5 | |
User: ed | |
Warning: | |
A slightly different error | |
message. | |
--- | |
Date: 2001-11-23 15:03:17 -5 | |
User: ed | |
Warning: | |
Unknown variable bar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
none: [~, null] | |
bool: [true, false, on, off] | |
int: 42 | |
float: 3.14159 | |
list: [LITE, RES_ACID, SUS_DEXT] | |
dict: {hp: 13, sp: 5} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.yaml.snakeyaml.DumperOptions; | |
import org.yaml.snakeyaml.Yaml; | |
import org.yaml.snakeyaml.nodes.Tag; | |
import java.util.Arrays; | |
import java.util.HashMap; | |
import java.util.List; | |
import java.util.Map; | |
public class SnakeYamlDump { | |
public static void main(String[] args) { | |
Yaml yaml = new Yaml(); | |
String yamlData; | |
/** Listのダンプ */ | |
List<Object> list = Arrays.asList("hoge", null, 1234, true); | |
yamlData = yaml.dump(list); | |
System.out.println(yamlData); | |
/** Mapのダンプ */ | |
Map<String, Object> map = new HashMap<String, Object>() { | |
{put("Java", 5);} | |
{put("Ruby", 3);} | |
{put("Python", 2);} | |
}; | |
yamlData = yaml.dump(map); | |
System.out.println(yamlData); | |
/** UserObjectのダンプ */ | |
User user = new User(); | |
user.setId(1); | |
user.setName("tasukujp"); | |
user.setHobby(Arrays.asList("running", "bouldering", "baseball")); | |
user.setSkill(new HashMap<String, Object>() { | |
{put("Java", true);} | |
{put("Ruby", null);} | |
}); | |
yamlData = yaml.dumpAs(user, Tag.YAML, DumperOptions.FlowStyle.BLOCK); | |
System.out.println(yamlData); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.yaml.snakeyaml.Yaml; | |
import java.util.List; | |
import java.util.Map; | |
public class SnakeYamlLoad { | |
public static void main(String[] args) { | |
Yaml yaml = new Yaml(); | |
/** Listのロード */ | |
List list = (List) yaml.load(ClassLoader.getSystemResourceAsStream("list.yaml")); | |
System.out.println(list); | |
/** Mapのロード */ | |
Map map = yaml.loadAs(ClassLoader.getSystemResourceAsStream("map.yaml"), Map.class); | |
System.out.println(map); | |
/** Userクラスのロード */ | |
User user = yaml.loadAs(ClassLoader.getSystemResourceAsStream("user.yaml"), User.class); | |
System.out.println(user); | |
/** 複数データのロード */ | |
for (Object data : yaml.loadAll(ClassLoader.getSystemResourceAsStream("loadAll.yaml"))) { | |
System.out.println(data); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.List; | |
import java.util.Map; | |
public class User { | |
private int id; | |
private String name; | |
private List<String> hobby; | |
private Map<String, Object> skill; | |
public int getId() { return id; } | |
public void setId(int id) { this.id = id; } | |
public String getName() { return name; } | |
public void setName(String name) { this.name = name; } | |
public List<String> getHobby() { return hobby; } | |
public void setHobby(List<String> hobby) { this.hobby = hobby; } | |
public Map<String, Object> getSkill() { return skill; } | |
public void setSkill(Map<String, Object> skill) { this.skill = skill; } | |
@Override | |
public String toString() { | |
return "id=" + id + ", name=" + name + ", hobby=" + hobby + ", skill=" + skill; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
id: 1 | |
name: tasuku | |
hobby: | |
- "running" | |
- "bouldering" | |
- "baseball" | |
skill: | |
"Java": null | |
"Ruby": True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment