I'm using SnakeYAML 2.0 to parse YAML files in Java. But I face a problem here. This is my User
class:
package ir.sharif;
import java.util.ArrayList;
import java.util.List;
public class User {
int id;
String username;
String password;
List<User> friends = new ArrayList<>();
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public User() {
}
// getters and setters
}
I use this code to serialize a User
object:
package ir.sharif;
import org.yaml.snakeyaml.Yaml;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
User user = new User();
user.setId(10);
user.setUsername("Jack");
user.setPassword("jackSparrow2023");
List<User> friends = new ArrayList<>();
friends.add(new User(1, "Trent", "FCB"));
friends.add(new User(2, "Jordan", "LFC"));
user.setFriends(friends);
Yaml yaml = new Yaml();
yaml.dump(user, new FileWriter("src/main/resources/sample.yml"));
}
}
And this is how my sample.yml
file looks like after running main
method:
!!ir.sharif.User
friends:
- friends: []
id: 1
password: FCB
username: Trent
- friends: []
id: 2
password: LFC
username: Jordan
id: 10
password: jackSparrow2023
username: Jack
Now I want to deserialize this data and load it as a new User
object:
package ir.sharif;
import org.yaml.snakeyaml.Yaml;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class Main {
public static void main(String[] args) throws IOException {
Yaml yaml = new Yaml();
try (Reader reader = new FileReader("src/main/resources/sample.yml")) {
User user = yaml.load(reader);
System.out.println(user);
}
}
}
But after running, I get this exception:
Exception in thread "main" Global tag is not allowed: tag:yaml.org,2002:ir.sharif.User
in 'reader', line 1, column 1:
!!ir.sharif.User
^
How can I solve it?
As I searched, I'm doing it right but I think the problem is caused because in version 2.0 there are some changes. I tried loading my object like this (I deleted !!ir.sharif.User
line from YAML file):
package ir.sharif;
import org.yaml.snakeyaml.Yaml;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class Main {
public static void main(String[] args) throws IOException {
Yaml yaml = new Yaml();
try (Reader reader = new FileReader("src/main/resources/sample.yml")) {
User user = yaml.loadAs(reader, User.class);
System.out.println(user);
}
}
}
And it works fine. But if I want to deserialize the object serialized by SnakeYAML itself, I can't use this method. What should I do? I appreciate any helps.