////
Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
Copyright (c) 2021 Dmitry Arkhipov (grisumbras@yandex.ru)

Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)

Official repository: https://github.com/boostorg/json
////

[#conversion]
= 值转换 尽管 &lt;<ref_value>&gt; 容器便于创建临时结构，但通常仍需在 JSON 与用户自定义类型或标准库类型之间进行转换。</ref_value>

函数模板 &lt;<ref_value_from>&gt; 提供了从类型 `T` 构造 &lt;<ref_value>&gt; 的接口。函数模板 &lt;<ref_value_to>&gt; 则执行相反方向的转换，从类型 `T` 转换为 &lt;<ref_value>&gt;。两者均支持多种不同的 https://en.cppreference.com/w/cpp/language/types[基础类型]（如 `int` 或 `double`）、标准库类型（如 `std::string` 或 `std::vector<t>`），并可扩展以支持用户定义类型。</t></ref_value></ref_value_to></ref_value></ref_value_from>

[source]
----
include::../../../test/snippets.cpp[tag=snippet_conv_1,indent=0]
----

对于类型 `T`，将从以下类别列表中选择合适的转换方式，并采用第一个匹配的类别。

.Conversion categories
[%autowidth,cols=4]
|===
|Category of T|Comment|`value_from` behavior|`value_to` behavior

|Custom conversion.
|
|Custom behavior.
|Custom behavior.

|Boost.JSON container.
|
|The result is equal to the input value.
|The result is equal to the input value.

|`bool`
|
|The result is equal to the input value.
|The result is equal to the input value.

|https://en.cppreference.com/w/cpp/types/is_arithmetic[Arithmetic type]
|
a| 结果是一个与输入值相等的数字，并且其类型为

* 若 `T` 为有符号整数，则为 `std::int64_t`；或
* 若 `T` 为无符号整数，则为 `std::uint64_t`；或
* 否则为 `double`。
|The result is created via <<ref_value_to_number>>.

|Type satisfying <<ref_is_null_like>>
|Intended for types like {std_monostate}.
|The result is a null value.
|The result is default-constructed.

|Type satisfying <<ref_is_string_like>>.
|A sequence of `char`s, e.g. `std::string`.
|The result is a <<ref_string>>.
|The result is constructed from a <<ref_string_view>>.

|Type satisfying <<ref_is_variant_like>>.
|`std::variant` and similar types, e.g. `boost::variant2::variant`.
|The result is equal to the result of conversion of the active variant
a| 结果为一个数值，等于输入值且类型为：

|Type satisfying <<ref_is_optional_like>>
|
|If the input value is empty, the result is a `null`. Otherwise it is
等同于将存储在可选对象内部的对象进行转换。| 如果输入值为 `null` ，则结果将默认进行构造。否则，结果将根据将输入转换为存储在可选对象中的类型所得到的结果进行构造。

|Type satisfying <<ref_is_map_like>>.
|A one-to-one mapping (e.g. `std::map`) with string-like keys.
|The result is an <<ref_object>>.
|The result is default-constructed, and elements are `insert`-ed at the end.

|Type satisfying <<ref_is_sequence_like>>.
|A sequence of elements, e.g. `std::vector`.
|The result is an <<ref_array>>.
|The result is default-constructed, and elements are `insert`-ed at the end.

|Type satisfying <<ref_is_tuple_like>>.
|A heterogenous sequence with fixed size, e.g. `std::tuple` and `std::pair`.
|The result is an <<ref_array>>.
|The result is constructed with the array elements as constructor arguments.

|Type satisfying <<ref_is_described_class>>
|
|The result is an <<ref_object>> with described members' names as keys.
|The result is default-constructed and described members are assigned
相应的值。

|Type satisfying <<ref_is_described_enum>>
|
|If the input value is equal to one of the described enumerators, the result is
一个包含该枚举项名称的 &lt;<ref_string>&gt;；否则，结果等同于将输入值转换为其底层类型后的值。</ref_string>

|Type satisfying <<ref_is_path_like>>.
|`std::filesystem::path` and similar types, e.g. `boost::filesystem::path`.
|The result is equal to the result of `path::generic_string`.
|The result is constructed from two pointers to `const char`.
|===

对于复合类型（如序列、元组、已描述的类等），对其所包含对象的转换会递归应用。例如：

[source]
----
include::../../../test/snippets.cpp[tag=snippet_conv_recursive,indent=0]
----

在这里，该地图会被转换为一个 &lt;<ref_object>&gt;，因为其符合 &lt;<ref_is_map_like>&gt; 这个条件。其每个键都会被转换为一个 &lt;<ref_string>&gt;，因为 `std::string` 符合 &lt;<ref_is_string_like>&gt; 这个条件，而其每个值都会被转换为一个 &lt;<ref_array>&gt;，因为 `std::pair` 符合 &lt;<ref_is_tuple_like>&gt; 这个条件。最后，对每个 `std::pair` 的元素进行转换，将其转换为一个 `std::int64_t` 数字和一个 `bool` 值。</ref_is_tuple_like></ref_array></ref_is_string_like></ref_string></ref_is_map_like></ref_object>

:leveloffset: +1

include::custom.adoc[] include::nothrow.adoc[] include::alloc.adoc[] include::context.adoc[] include::forward.adoc[] include::direct.adoc[] include::guidelines.adoc[]

:leveloffset: -1
