Skip to content

Commit

Permalink
README documentation for the allow duplicates features (#61)
Browse files Browse the repository at this point in the history
* fix for #57

* as per review by @NathanSweet on fix for #57

* removing the whitespace in the eof

* trying to remove the spaces in files so that git does not complain about

* adding gitattributes file to fix the line endings to clrf (windows style)

* reverting my changes to see if I can redo them to fix the line ending now.. also added a .gitattributes file to keep the line endings to windows style

* Saving files before refreshing line endings

* redone my changes

* correcting the .gitattributes file

* removing .gitattributes

* fix line endings to windows

* fixing line endings to windows format

* adding documentation for issue #57

* correcting documentation typo

* correcting documentation typo

* correcting documentation typo

* added more descriptive example

* added more descriptive example
  • Loading branch information
sanjusoftware authored and NathanSweet committed Nov 5, 2016
1 parent ad36665 commit 36b0e5c
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,38 @@ In this map, the "oldest friend" and "best friend" keys reference the same objec
map.put("best friend", contact);
```

## Duplicate key validation

By default, the behaviour of this YAML parser is to ignore duplicate keys if you have. e.g if you have the following

```yaml
name: Nathan Sweet
age: 28
address:
line1: 485 Madison Ave S
line1: 711 3rd Ave S
line2: NYC
```
The above YAML will give you an `address` object with attribute `line1` set to `711 3rd Ave S`. This is because the key `line1` in the above YAML is duplicated and thus the last value of `line1` will be retained. YAML parser will not complain about it. However, if your business logic requires you to validate YAML for such duplicates, then you can still do using `allowDuplicates` option of the `YamlConfig` object. Following is how its done:

```java
try {
YamlConfig yamlConfig = new YamlConfig();
yamlConfig.setAllowDuplicates(false); // default value is true
YamlReader reader = new YamlReader(new FileReader("contact.yml"), yamlConfig);
Object object = reader.read();
System.out.println(object);
Map map = (Map)object;
System.out.println(map.get("address"));
} catch (YamlException ex) {
ex.printStackTrace();
// or handle duplicate key case here according to your business logic
}
```

The above code will not print anything, but throw `YamlReaderException` at line 5 saying, `Duplicate key found 'line1'`.

## Architecture

The YAML tokenizer, parser, and emitter are based on those from the JvYAML project. They have been heavily refactored, bugs fixed, etc. The rest of the JvYAML project was not used because of its complexity. YamlBeans strives for the simplest possible thing that works, with the goal being to make it easy to use the YAML data format with Java.
Expand Down

0 comments on commit 36b0e5c

Please sign in to comment.