|
| 1 | +[/ |
| 2 | + Copyright (c) 2023 Dmitry Arkhipov ( [email protected]) |
| 3 | + |
| 4 | + Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | + |
| 7 | + Official repository: https://github.com/cppalliance/json |
| 8 | +] |
| 9 | + |
| 10 | +[/-----------------------------------------------------------------------------] |
| 11 | + |
| 12 | + |
| 13 | +[section Conversion customization guidelines] |
| 14 | + |
| 15 | +With so many options, it can be hard to choose the best way to customise |
| 16 | +conversion for your type. In this section, we will discuss those options and |
| 17 | +suggest which to choose when. |
| 18 | + |
| 19 | +The first advice is to use one of the library-provided conversions, rather |
| 20 | +then providing a custom one, unless the resulting format is undesirable. If |
| 21 | +the library deduces the wrong conversion category, you can opt out by |
| 22 | +specialising the relevant trait to inherit from `std::false_type`. |
| 23 | + |
| 24 | +The next thing to consider is whether your conversions are intended for |
| 25 | +internal use, or whether your users are not members of your team. If your users |
| 26 | +are external, then they will ultimately determine the conditions in which these |
| 27 | +conversions will be used. Conversely, for internal libraries and applications, |
| 28 | +you have the full control of usage conditions. |
| 29 | + |
| 30 | +If your users are external, they and not you decide whether throwing exceptions |
| 31 | +is acceptable. So, in this case it is better to use non-throwing `tag_invoke` |
| 32 | +overloads. In addition, for customising conversion of composite types, always |
| 33 | +use `tag_invoke` overload with 2 context parameters. This will allow correct |
| 34 | +context propagation to elements of composites. This will also allow propagation |
| 35 | +of exceptions from conversion of elements. |
| 36 | + |
| 37 | +Finally, it is worth mentioning that due to the ability to provide conversions |
| 38 | +to JSON containers without a binary dependency on the library, you don't have |
| 39 | +to push such dependency on your users. This is particularly relevant for |
| 40 | +libraries for which interoperation with Boost.JSON is only extra functionality. |
| 41 | + |
| 42 | +[endsect] |
0 commit comments