#any->convert(from?: 'json'|yaml|'csv'|'markdown'|'latex', to?: 'json'|yaml|'csv'|html|'markdown'|'latex', header?: bool, as?: type, headers?: list, indented?: bool) → any
Muda o formato do valor. from: lê texto escrito em um formato (json, yaml, csv, markdown ou latex), e to: escreve o valor em um formato (json, yaml, csv, ou html, markdown e latex para documentos); use os dois para converter um no outro. Lendo csv:
not header quando não há linha de cabeçalho. Lendo json, yaml ou csv: as: um tipo verifica os dados e os transforma em registros. Escrevendo csv: headers: escolhe e ordena as colunas. Escrevendo json: indented os espalha em linhas. ->from(type:) e ->to(type:) são atalhos para from: e to: sozinhos.| Parâmetro | Recebe |
|---|---|
from opcional | uma destas palavras: 'json'|yaml|'csv'|'markdown'|'latex' |
to opcional | uma destas palavras: 'json'|yaml|'csv'|html|'markdown'|'latex' |
header opcional | uma flag: header ou not header |
as opcional | type |
headers opcional | list |
indented opcional | uma flag: indented ou not indented |
Os argumentos podem ser nomeados: ->convert(…, to: …, header: …).
Exemplo
<!-- 'apple': the json text is read into a list of maps -->
<echo>{ '[{"fruit": "apple"}]'->convert(from: json)->first().fruit }</echo>
<let name="prices" value="[{ fruit: 'apple', price: 3 }, { fruit: 'plum', price: 5 }]" />
<!-- fruit,price on the first line, then one line a row: the maps' keys name the columns -->
<echo>{ $prices->convert(to: csv) }</echo>
<!-- 'apple: 3': yaml read, then written as json -->
<echo>{ 'apple: 3'->convert(from: yaml, to: json) }</echo>// 'apple': the json text is read into a list of maps
echo '[{"fruit": "apple"}]'->convert(from: json)->first().fruit
$prices = [{ fruit: 'apple', price: 3 }, { fruit: 'plum', price: 5 }]
// fruit,price on the first line, then one line a row: the maps' keys name the columns
echo $prices->convert(to: csv)
// 'apple: 3': yaml read, then written as json
echo 'apple: 3'->convert(from: yaml, to: json)