Convert between CBOR, JSON, MessagePack, TOML, and YAML.
When installed,
Remarshal provides the command-line command remarshal
as well as the short commands
{cbor,json,msgpack,toml,yaml}2{cbor,json,msgpack,toml,yaml}.
You can use these commands
to convert between formats,
reformat,
and detect errors.
Known limitations and quirks
There are limitations
on what data can be converted
between what formats.
CBOR, MessagePack, and YAML with binary fields cannot be converted
to JSON or TOML.
Binary fields can be converted between CBOR, MessagePack, and YAML.
The following date-time value conversions are possible:
TOML Local Time
cannot be converted to a date-time in another format.
All date-time types can be converted to JSON
with the -k/--stringify option,
which turns them into strings.
Contrary to the
YAML timestamp draft spec,
Remarshal converts YAML dates to TOML Local Dates instead of TOML Offset Dates
in the UTC time zone.
It converts TOML Local Dates to YAML dates.
Installation
You will need Python 3.8 or later.
Earlier versions of Python 3 will not work.
The recommended way to run Remarshal is to install the latest release
from PyPI
with
pipx.
pipx install remarshal
Regular installation is not mandatory.
The command
pipx run remarshal [arg ...]
will download Remarshal and run it from a temporary location.
It will cache the downloaded version for up to 14 days.
Remarshal will not be automatically upgraded during this period.
You can also install Remarshal using pip.
python3 -m pip install --user remarshal
It is possible to install the current development version of Remarshal.
Prefer releases unless you have a reason to run the development version.
usage: remarshal [-h] [-v] [-i <input>] [--if {cbor,json,msgpack,toml,yaml}]
[--json-indent <n>] [-k] [--max-values <n>] [-o <output>]
[--of {cbor,json,msgpack,toml,yaml}] [-s] [--unwrap <key>]
[--verbose] [--wrap <key>] [--yaml-indent <n>]
[--yaml-style {,',",|,>}] [--yaml-width <n>]
[input] [output]
Convert between CBOR, JSON, MessagePack, TOML, and YAML.
positional arguments:
input input file
output output file
options:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-i <input>, --input <input>
input file
--if {cbor,json,msgpack,toml,yaml}, --input-format
{cbor,json,msgpack,toml,yaml}, -f {cbor,json,msgpack,toml,yaml},
--from {cbor,json,msgpack,toml,yaml}
input format
--json-indent <n> JSON indentation
-k, --stringify turn into strings: boolean and null keys and date-time
keys and values for JSON; boolean, date-time, and null
keys and null values for TOML
--max-values <n> maximum number of values in input data (default
1000000, negative for unlimited)
-o <output>, --output <output>
output file
--of {cbor,json,msgpack,toml,yaml}, --output-format
{cbor,json,msgpack,toml,yaml}, -t {cbor,json,msgpack,toml,yaml},
--to {cbor,json,msgpack,toml,yaml}
output format
-s, --sort-keys sort JSON and TOML keys instead of preserving key order
--unwrap <key> only output the data stored under the given key
--verbose print debug information when an error occurs
--wrap <key> wrap the data in a map type with the given key
--yaml-indent <n> YAML indentation
--yaml-style {,',",|,>}
YAML formatting style
--yaml-width <n> YAML line width for long strings
Instead of remarshal with format arguments,
you can use a short command
{cbor,json,msgpack,toml,yaml}2{cbor,json,msgpack,toml,yaml}.
The remarshal command and the short commands
exit with status 0 on success,
1 on operational failure,
and 2 on failure to parse the command line.
If no input argument input/-i input is given or its value is -,
Remarshal reads input data from standard input.
Similarly,
with no output/-o output or an output argument that is -,
Remarshal writes the result to standard output.
Wrappers
The options --wrap and --unwrap are available
to solve the problem of converting CBOR, JSON, MessagePack, and YAML data to TOML
when the top-level element of the data is not of a dictionary type
(i.e., not a map in CBOR and MessagePack,
an object in JSON,
or an associative array in YAML).
You cannot represent such data as TOML directly;
the data must be wrapped in a dictionary first.
Passing the option --wrap some-key to remarshal or one of its short commands
wraps the input data in a “wrapper” dictionary with one key, some-key,
with the input data as its value.
The option --unwrap some-key does the opposite:
it converts to the target format and outputs
only the value stored under the key some-key
in the top-level dictionary element of the input data;
the rest of the input is discarded.
If the top-level element is not a dictionary or does not have the key some-key,
--unwrap some-key causes an error.
The following shell transcript demonstrates the problem
and how --wrap and --unwrap solve it:
$ echo '[{"a":"b"},{"c":[1,2,3]}]' | remarshal --if json --of toml
Error: cannot convert non-dictionary data to TOML; use "--wrap" to wrap it in a dictionary
$ echo '[{"a":"b"},{"c":[1,2,3]}]' \
| remarshal --if json --of toml --wrap main
[[main]]
a = "b"
[[main]]
c = [1, 2, 3]
$ echo '[{"a":"b"},{"c":[1,2,3]}]' \
| remarshal --if json --wrap main - test.toml
$ remarshal test.toml --of json
{"main":[{"a":"b"},{"c":[1,2,3]}]}
$ remarshal test.toml --of json --unwrap main
[{"a":"b"},{"c":[1,2,3]}]
example.toml from https://github.com/toml-lang/toml.
example.json,
example.msgpack,
example.cbor,
example.yml,
tests/bin.msgpack,
and tests/bin.yml
are derived from it.
Remarshal
Convert between CBOR, JSON, MessagePack, TOML, and YAML. When installed, Remarshal provides the command-line command
remarshal
as well as the short commands{cbor,json,msgpack,toml,yaml}2{cbor,json,msgpack,toml,yaml}
. You can use these commands to convert between formats, reformat, and detect errors.Known limitations and quirks
There are limitations on what data can be converted between what formats.
-k
/--stringify
option, which turns them into strings.Installation
You will need Python 3.8 or later. Earlier versions of Python 3 will not work.
The recommended way to run Remarshal is to install the latest release from PyPI with pipx.
Regular installation is not mandatory. The command
will download Remarshal and run it from a temporary location. It will cache the downloaded version for up to 14 days. Remarshal will not be automatically upgraded during this period.
You can also install Remarshal using pip.
It is possible to install the current development version of Remarshal. Prefer releases unless you have a reason to run the development version.
Usage
Instead of
remarshal
with format arguments, you can use a short command{cbor,json,msgpack,toml,yaml}2{cbor,json,msgpack,toml,yaml}
. Theremarshal
command and the short commands exit with status 0 on success, 1 on operational failure, and 2 on failure to parse the command line.If no input argument
input
/-i input
is given or its value is-
, Remarshal reads input data from standard input. Similarly, with nooutput
/-o output
or an output argument that is-
, Remarshal writes the result to standard output.Wrappers
The options
--wrap
and--unwrap
are available to solve the problem of converting CBOR, JSON, MessagePack, and YAML data to TOML when the top-level element of the data is not of a dictionary type (i.e., not a map in CBOR and MessagePack, an object in JSON, or an associative array in YAML). You cannot represent such data as TOML directly; the data must be wrapped in a dictionary first. Passing the option--wrap some-key
toremarshal
or one of its short commands wraps the input data in a “wrapper” dictionary with one key,some-key
, with the input data as its value. The option--unwrap some-key
does the opposite: it converts to the target format and outputs only the value stored under the keysome-key
in the top-level dictionary element of the input data; the rest of the input is discarded. If the top-level element is not a dictionary or does not have the keysome-key
,--unwrap some-key
causes an error.The following shell transcript demonstrates the problem and how
--wrap
and--unwrap
solve it:Examples
(This example uses
taplo fmt
to reformat the TOML and break up long lines containing the arrays. Remarshal does not limit TOML line length.)License
MIT. See the file
LICENSE
.example.toml
from https://github.com/toml-lang/toml.example.json
,example.msgpack
,example.cbor
,example.yml
,tests/bin.msgpack
, andtests/bin.yml
are derived from it.