Module yaml | Tarantool
Документация на русском языке
поддерживается сообществом
Module yaml

Module yaml

The yaml module takes strings in YAML format and decodes them, or takes a series of non-YAML values and encodes them.

yaml.encode(lua_value)

Convert a Lua object to a YAML string.

Параметры:
  • lua_value – either a scalar value or a Lua table value.
Return:

the original value reformatted as a YAML string.

Rtype:

string

yaml.decode(string)

Convert a YAML string to a Lua object.

Параметры:
  • string – a string formatted as YAML.
Return:

the original contents formatted as a Lua table.

Rtype:

table

yaml.NULL

A value comparable to Lua «nil» which may be useful as a placeholder in a tuple.

Пример

tarantool> yaml = require('yaml')
---
...
tarantool> y = yaml.encode({'a', 1, 'b', 2})
---
...
tarantool> z = yaml.decode(y)
---
...
tarantool> z[1], z[2], z[3], z[4]
---
- a
- 1
- b
- 2
...
tarantool> if yaml.NULL == nil then print('hi') end
hi
---
...

The YAML collection style can be specified with __serialize:

  • __serialize="sequence" for a Block Sequence array,
  • __serialize="seq" for a Flow Sequence array,
  • __serialize="mapping" for a Block Mapping map,
  • __serialize="map" for a Flow Mapping map.

Serializing „A“ and „B“ with different __serialize values causes different results:

tarantool> yaml = require('yaml')
---
...
tarantool> yaml.encode(setmetatable({'A', 'B'}, { __serialize="sequence"}))
---
- |
  ---
  - A
  - B
  ...
...
tarantool> yaml.encode(setmetatable({'A', 'B'}, { __serialize="seq"}))
---
- |
  ---
  ['A', 'B']
  ...
...
tarantool> yaml.encode({setmetatable({f1 = 'A', f2 = 'B'}, { __serialize="map"})})
---
- |
  ---
  - {'f2': 'B', 'f1': 'A'}
  ...
...
tarantool> yaml.encode({setmetatable({f1 = 'A', f2 = 'B'}, { __serialize="mapping"})})
---
- |
  ---
  - f2: B
    f1: A
  ...
...

Also, some YAML configuration settings for encoding can be changed, in the same way that they can be changed for JSON.

Нашли ответ на свой вопрос?
Обратная связь