Module yaml
ballerina/yaml Ballerina library
Overview
This module provides APIs to convert a YAML configuration file to json, and vice-versa.
Since the parser is following LL(1) grammar, it follows a non-recursive predictive parsing algorithm which operates in a linear time complexity.
For information on the operations, which you can perform with the yaml module, see the below Functions.
Functions
readFile
function readFile(string filePath, *ReadConfig config) returns json|ErrorParses a YAML file into a Ballerina json object.
Parameters
- filePath string - Path to the YAML file
 
- config *ReadConfig - Configuration for reading a YAML file
 
Return Type
- json|Error - YAML map object on success. Else, returns an error
 
readString
function readString(string yamlString, *ReadConfig config) returns json|ErrorParses a Ballerina string of YAML content into a Ballerina map object.
Parameters
- yamlString string - YAML content
 
- config *ReadConfig - Configuration for reading a YAML file
 
Return Type
- json|Error - YAML map object on success. Else, returns an error
 
writeFile
function writeFile(string filePath, json yamlStructure, *WriteConfig config) returns Error?Writes the YAML structure to a file.
Parameters
- filePath string - Path to the file
 
- yamlStructure json - Structure to be written to the file
 
- config *WriteConfig - Configurations for writing a YAML file
 
Return Type
- Error? - An error on failure
 
writeString
function writeString(json yamlStructure, *WriteConfig config) returns string[]|ErrorConverts the YAML structure to an array of strings.
Parameters
- yamlStructure json - Structure to be written to the file
 
- config *WriteConfig - Configurations for writing a YAML file
 
Enums
yaml: FailSafeSchema
Represents the basic YAML types available in the Fail safe schema.
Members
yaml: YAMLSchema
Represents the YAML schema available for the parser.
Members
Records
yaml: ReadConfig
Configurations for reading a YAML document.
Fields
- schema YAMLSchema(default CORE_SCHEMA) - YAML schema used for writing
 
- yamlTypes YamlType[](default []) - Custom YAML types for the schema
 
- isStream boolean(default false) - If set, the parser reads a stream of YAML documents
 
- allowAnchorRedefinition boolean(default true) - Flag is set if anchors can be redefined multiple times
 
- allowMapEntryRedefinition boolean(default false) - Flag is set if same map keys are allowed in a mapping
 
yaml: WriteConfig
Configurations for writing a YAML document.
Fields
- indentationPolicy int(default 2) - Number of whitespace for an indentation
 
- blockLevel int(default 1) - The maximum depth level for a block collection
 
- canonical boolean(default false) - If set, the tags are written along with the nodes
 
- useSingleQuotes boolean(default false) - If set, single quotes are used to surround scalars
 
- forceQuotes boolean(default false) - If set, all the scalars are surrounded by quotes
 
- schema YAMLSchema(default CORE_SCHEMA) - YAML schema used for writing
 
- yamlTypes YamlType[](default []) - Custom YAML types for the schema
 
- isStream boolean(default false) - If set, the parser will write a stream of YAML documents
 
yaml: YamlType
Represents the attributes of the custom YAML type.
Fields
- tag string - YAML tag for the custom type
 
- ballerinaType typedesc<json> - The equivalent Ballerina type for the YAML tag
 
- kind FailSafeSchema - Fail safe schema type
 
- construct 
function(json) returns (json|SchemaError)- Function to generate the Ballerina data structure. 
- represent 
function(json) returns (string|SchemaError)- Function to convert the Ballerina data structure to YAML. 
Errors
yaml: Error
Represents the generic error type for the YAML package.
Simple name reference types
yaml: ComposingError
ComposingError
Represents an error caused during the composing.
yaml: EmittingError
EmittingError
Represents an error caused during the emitting.
yaml: SchemaError
SchemaError
Represents an error caused regarding YAML schema.
yaml: ParsingError
ParsingError
Represents an error caused during the parsing.
yaml: LexicalError
LexicalError
Represents an error caused during the lexical analyzing.
yaml: AliasingError
AliasingError
Represents an error caused when failed to alias an anchor.
yaml: IndentationError
IndentationError
Represents an error caused when the indentation is not correct.
yaml: ConversionError
ConversionError
Represents an error caused by the Ballerina lang when converting a data type.
yaml: ComposeError
ComposeError
Represents an error caused for an invalid compose.
yaml: GrammarError
GrammarError
Represents an error caused for an invalid grammar production.
yaml: ScanningError
ScanningError
Represents an error that is generated when an invalid character for a lexeme is detected.
yaml: ConstructionError
ConstructionError
Represents an error caused when constructing a Ballerina data type.
Import
import ballerina/yaml;Metadata
Released date: about 2 years ago
Version: 0.5.0
License: Apache-2.0
Compatibility
Platform: any
Ballerina version: 2201.8.0-20230908-135700-74a59dff
GraalVM compatible: Yes
Pull count
Total: 0
Current verison: 0
Weekly downloads
Keywords
yaml
Contributors