This object provides simple YAML parsing functions. Swagger Parser uses this object internally for its own YAML parsing, but it is also exposed so you can use it in your code if needed.
-
text (required) -
string
The YAML string to be parsed. -
Return Value:
Returns the parsed value, which can be any valid JSON type (object, array, string, number, etc.)
This method is similar to JSON.parse()
, but it supports YAML in addition to JSON (since any JSON document is also a valid YAML document).
var YAML = SwaggerParser.YAML;
var text = "title: person \n" +
"required: \n" +
" - name \n" +
" - age \n" +
"properties: \n" +
" name: \n" +
" type: string \n" +
" age: \n" +
" type: number"
var obj = YAML.parse(text);
// {
// title: "person",
// required: ["name", "age"],
// properties: {
// name: {
// type: "string"
// },
// age: {
// type: "number"
// }
// }
// }
-
value (required)
The value to be converted to a YAML string. Can be any valid JSON type (object, array, string, number, etc.) -
Return Value:
string
Returns the a YAML string containing the serialized value
This method is similar to JSON.stringify()
, except that it converts a value to a YAML string instead of a JSON string.
var YAML = SwaggerParser.YAML;
var obj = {
title: "person",
required: ["name", "age"],
properties: {
name: {
type: "string"
},
age: {
type: "number"
}
}
};
var string = YAML.stringify(obj);
// title: person
// required:
// - name
// - age
// properties:
// name:
// type: string
// age:
// type: number