yaml-base
Base classes for YAML implementation.
The specification of YAML is contained in a document entitled YAML Ain't Markup Language (YAML™) Version 1.2, and that document (in §1.3. Relation to JSON) states "YAML can … be viewed as a natural superset of JSON". Accordingly, each of the classes in this library derives from the equivalent class in the jsonutil
library (and that library is a transitive dependency of this one). A tree of YAMLNode
objects can therefore be navigated as if it were a tree of JSONValue
s.
Reference
YAMLNode
The root interface for all YAML nodes. It extends JSONValue
, and it adds a single method:
String getTag()
: return the tag associated with the node
Each of the implementing classes has a constructor that takes a tag in the form of a String
, along with constructors that supply a default tag name (from the specification §10.3. Core Schema). The tag name is immutable and no setter is provided.
YAMLScalar
This is a marker interface, implemented by the implementation classes for the various forms of YAML scalar. It extends YAMLNode
and adds no additional methods.
YAMLString
YAMLString
extends JSONString
and implements YAMLScalar
. The JSONString.getValue()
function returns the value.
Example:
YAMLString yamlString = new YAMLString("Hello!");
System.out.println(yamlString.getValue()); // prints Hello!
YAMLInt
YAMLInt
extends JSONInteger
and implements YAMLScalar
. The JSONInteger.getValue()
function returns the value.
Example:
YAMLInt yamlInt = new YAMLInt(3 * 3 * 3);
System.out.println(yamlInt.getValue()); // prints 27
YAMLLong
YAMLLong
extends JSONLong
and implements YAMLScalar
. The YAMLLong.getValue()
function returns the value.
Example:
YAMLLong yamlLong = new YAMLLong(1234567812345678L);
System.out.println(yamlLong.getValue()); // prints 1234567812345678
YAMLDecimal
YAMLDecimal
extends JSONDecimal
and implements YAMLScalar
. The YAMLDecimal.getValue()
function returns the value.
Example:
YAMLDecimal yamlDecimal = new YAMLDecimal(new BigDecimal("123.45"));
System.out.println(yamlDecimal.getValue()); // prints 123.45
YAMLBoolean
YAMLBoolean
extends JSONBoolean
and implements YAMLScalar
. The YAMLBoolean.getValue()
function returns the value.
Example:
YAMLBoolean yamlBoolean = YAMLBoolean.TRUE;
System.out.println(yamlBoolean.getValue()); // prints true
YAMLSequence
YAMLSequence
extends JSONSequence<YAMLNode>
and implements YAMLNode
. JSONSequence<YAMLNode>
implements List<YAMLNode>
, so all of the functionality of the List
interface is available to access the members of the sequence.
Example:
YAMLSequence yamlSequence = new YAMLSequence(Arrays.asList(new YAMLString("Hello"), new YAMLString("World")));
for (YAMLNode node : yamlSequence)
System.out.println(node); // prints Hello (first time), World (second time)
YAMLMapping
YAMLMapping
extends JSONMapping<YAMLNode>
and implements YAMLNode
. JSONMapping<YAMLNode>
implements Map<String, YAMLNode>
, so all of the functionality of the Map
interface is available to access the members of the mapping.
Example:
Map<String, YAMLNode> map = new HashMap<>();
map.put("greeting", new YAMLString("Hello"));
map.put("who", new YAMLString("World"));
YAMLMapping yamlMapping = new YAMLMapping(map);
System.out.println(yamlMapping.get("greeting")); // prints Hello
System.out.println(yamlMapping.get("who")); // prints World
YAMLDocument
YAMLDocument
is a container to hold the results of a YAML parse operation. It contains the root node of the parsed document, along with the version number from the %YAML
directive (if provided).
Constructors:
YAMLDocument(YAMLNode rootNode, int majorVersion, int minorVersion)
YAMLDocument(YAMLNode rootNode)
(defaults majorVersion to 1 and minorVersion to 2)
Functions:
YAMLNode getRootNode()
int getMajorVersion()
int getMinorVersion()
Dependency Specification
The latest version of the library is 1.2, and it may be obtained from the Maven Central repository.
Maven
<dependency>
<groupId>net.pwall.yaml</groupId>
<artifactId>yaml-base</artifactId>
<version>1.2</version>
</dependency>
Gradle
testImplementation 'net.pwall.yaml:yaml-base:1.2'
Gradle (kts)
testImplementation("net.pwall.yaml:yaml-base:1.2")
Peter Wall
2021-04-20