import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; public class YamlExample { private static final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); public static void main(String[] args) throws Exception { User user = new User("John Doe", 30); String yaml = objectMapper.writeValueAsString(user); System.out.println(yaml); User deserializedUser = objectMapper.readValue(yaml, User.class); System.out.println(deserializedUser.getName()); System.out.println(deserializedUser.getAge()); } static class User { private String name; private int age; public User() {} public User(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } } import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; public class SpecialCharactersExample { private static final ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory()); public static void main(String[] args) throws Exception { String yaml = "name: 'John : Doe' age: 30 description: 'This is a YAML: example'"; SpecialCharactersUser user = objectMapper.readValue(yaml, SpecialCharactersUser.class); System.out.println(user.getName()); System.out.println(user.getAge()); System.out.println(user.getDescription()); } @JsonIgnoreProperties(ignoreUnknown = true) static class SpecialCharactersUser { private String name; private int age; private String description; // getters and setters public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } } } import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; import java.io.File; public class CustomYamlExample { public static void main(String[] args) throws Exception { YAMLFactory yamlFactory = new YAMLFactory(); ObjectMapper objectMapper = new ObjectMapper(yamlFactory); File configFile = new File("config.yaml"); Config config = objectMapper.readValue(configFile, Config.class); System.out.println("Server IP: " + config.getServer().getIp()); System.out.println("Server Port: " + config.getServer().getPort()); } static class Config { private Server server; // getters and setters public Server getServer() { return server; } public void setServer(Server server) { this.server = server; } } static class Server { private String ip; private int port; // getters and setters public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } } }


上一篇:
下一篇:
切换中文