1# <small>nlohmann::adl_serializer::</small>to_json 2 3```cpp 4template<typename BasicJsonType, typename TargetType = ValueType> 5static auto to_json(BasicJsonType& j, TargetType && val) noexcept( 6 noexcept(::nlohmann::to_json(j, std::forward<TargetType>(val)))) 7-> decltype(::nlohmann::to_json(j, std::forward<TargetType>(val)), void()) 8``` 9 10This function is usually called by the constructors of the [basic_json](../basic_json) class. 11 12## Parameters 13 14`j` (out) 15: JSON value to write to 16 17`val` (in) 18: value to read from 19 20## Examples 21 22??? example 23 24 The example below shows how a `to_json` function can be implemented for a user-defined type. This function is called 25 by the `adl_serializer` when the constructor `basic_json(ns::person)` is called. 26 27 ```cpp 28 --8<-- "examples/to_json.cpp" 29 ``` 30 31 Output: 32 33 ```json 34 --8<-- "examples/to_json.output" 35 ``` 36 37## See also 38 39- [from_json](from_json.md) 40 41## Version history 42 43- Added in version 2.1.0. 44