Skip to content

Latest commit

 

History

History
151 lines (116 loc) · 3.45 KB

encode_json.md

File metadata and controls

151 lines (116 loc) · 3.45 KB

jsoncons::encode_json

Serializes a C++ object to a JSON formatted string or stream. encode_json will work for all types that have json_type_traits defined.

Header

#include <jsoncons/json.hpp>

template <class T, class CharT>
void encode_json(const T& val, basic_json_content_handler<CharT>& handler); // (1)

template <class T, class CharT>
void encode_json(const T& val, std::basic_ostream<CharT>& os); // (2)

template <class T, class CharT>
void encode_json(const T& val, 
                 const basic_json_serializing_options<CharT>& options,
                 std::basic_ostream<CharT>& os); // (3)

template <class T, class CharT>
void encode_json(const T& val, std::basic_ostream<CharT>& os, indenting line_indent); // (4)

template <class T, class CharT>
void encode_json(const T& val, 
                 const basic_json_serializing_options<CharT>& options,
                 std::basic_ostream<CharT>& os, indenting line_indent); // (5)

(1) Applies conversion_traits to serialize val to JSON output stream.

Parameters

val C++ object
handler JSON output handler
options Serialization options
os Output stream
indenting indenting::indent to pretty print, indenting::no_indent for compact output

Return value

None

See also

Examples

Map with string-tuple pairs

#include <iostream>
#include <map>
#include <tuple>
#include <jsoncons/json.hpp>

using namespace jsoncons;

int main()
{
    typedef std::map<std::string,std::tuple<std::string,std::string,double>> employee_collection;

    employee_collection employees = 
    { 
        {"John Smith",{"Hourly","Software Engineer",10000}},
        {"Jane Doe",{"Commission","Sales",20000}}
    };

    std::cout << "(1)\n" << std::endl; 
    encode_json(employees,std::cout);
    std::cout << "\n\n";

    std::cout << "(2) Again, with pretty print\n" << std::endl; 
    encode_json(employees, std::cout, jsoncons::indenting::indent);
}

Output:

(1)

{"Jane Doe":["Commission","Sales",20000.0],"John Smith":["Hourly","Software Engineer",10000.0]}

(2) Again, with pretty print

{
    "Jane Doe": ["Commission","Sales",20000.0],
    "John Smith": ["Hourly","Software Engineer",10000.0]
}

Contain JSON output in an object

#include <iostream>
#include <map>
#include <tuple>
#include <jsoncons/json.hpp>

using namespace jsoncons;

int main()
{
    std::map<std::string,std::tuple<std::string,std::string,double>> employees = 
    { 
        {"John Smith",{"Hourly","Software Engineer",10000}},
        {"Jane Doe",{"Commission","Sales",20000}}
    };

    json_serializer serializer(std::cout, jsoncons::indenting::indent); 

    serializer.begin_object();       
    serializer.write_name("Employees");       
    encode_json(employees, serializer);
    serializer.end_object();       
    serializer.flush();       
}

Output:

{
    "Employees": {
        "Jane Doe": ["Commission","Sales",20000.0],
        "John Smith": ["Hourly","Software Engineer",10000.0]
    }
}

See also