Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow duplicate keys in yaml. #102

Open
Dhivyaa21 opened this issue Nov 22, 2018 · 4 comments
Open

Allow duplicate keys in yaml. #102

Dhivyaa21 opened this issue Nov 22, 2018 · 4 comments

Comments

@Dhivyaa21
Copy link

Dhivyaa21 commented Nov 22, 2018

Can yamlreader append the values duplicate keys to the map instead of overwriting them?

@NathanSweet
Copy link
Member

Maps by definition have unique keys.

@NathanSweet
Copy link
Member

NathanSweet commented Nov 22, 2018

You want multiple values to use an ArrayList as the map value? You could use a Map implementation that does this. Or you could fork YamlBeans to do this.

@Dhivyaa21
Copy link
Author

Dhivyaa21 commented Nov 22, 2018

Yeah.. By default a hashmap ignores duplicate keys and overwrites the value with the latest one. But my requirement is to allow dupicate keys and append its values like a MultiValuedMap. Can YamlBeans do this?

@Mr14huashao
Copy link
Collaborator

@Dhivyaa21 @NathanSweet

  • Default
String yaml = "key: 001\nkey: 002";
YamlReader reader = new YamlReader(yaml);
Object object = reader.read();

object is a HashMap instance, does not support duplicate keys.

  • Specified type
String yaml = "key: 001\nkey: 002";
YamlReader reader = new YamlReader(yaml);
ArrayListValuedHashMap<String, String> arrayListValuedHashMap = 
reader.read(ArrayListValuedHashMap.class);

Deserialization exception, yamlbeans does not support.

  • Can use YamlDocumentReader
String yaml = "key: 001\nkey: 002";
YamlDocumentReader yamlDocumentReader = new YamlDocumentReader(yaml);
YamlDocument yamlDocument = yamlDocumentReader.read();

YamlEntry entry1 = yamlDocument.getEntry(0);
System.out.println("key: " + entry1.getKey() + ", value: " + entry1.getValue());
YamlEntry entry2 = yamlDocument.getEntry(1);
System.out.println("key: " + entry2.getKey() + ", value: " + entry2.getValue());

Console:

key: key, value: 001
key: key, value: 002

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants