JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
serialization.hpp
Go to the documentation of this file.
1/// \file jsonv/serialization.hpp
2/// Conversion between C++ types and JSON values.
3///
4/// Copyright (c) 2015-2020 by Travis Gockel. All rights reserved.
5///
6/// This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
7/// as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
8/// version.
9///
10/// \author Travis Gockel (travis@gockelhut.com)
11#pragma once
12
13#include <jsonv/config.hpp>
19#include <jsonv/value.hpp>
20#include <jsonv/version.hpp>
21
22#include <optional>
23#include <typeinfo>
24
25namespace jsonv
26{
27
28/// \addtogroup Serialization
29/// \{
30/// Serialization components are responsible for conversion between a C++ type and a JSON \c value.
31
33 public context
34{
35public:
36 /// Create a new instance using the default \c formats (\c formats::global).
38
39 /// Create a new instance using the given \a fmt, \a ver and \a userdata.
41 std::optional<jsonv::version> ver = std::nullopt,
42 const void* userdata = nullptr
43 );
44
46
47 /// Convenience function for converting a C++ object into a JSON value.
48 ///
49 /// \see formats::to_json
52 value to_json(const T& from) const
53 {
54 return to_json(typeid(T), static_cast<const void*>(&from));
55 }
56
57 /// Dynamically convert a type into a JSON value.
58 ///
59 /// \see formats::to_json
61 value to_json(const std::type_info& type, const void* from) const;
62};
63
64/// Encode a JSON \c value from \a from using the provided \a fmts.
65template <typename T>
67value to_json(const T& from, const formats& fmts)
68{
70 return context.to_json(from);
71}
72
73/// Encode a JSON \c value from \a from using \c jsonv::formats::global().
74template <typename T>
77{
79 return context.to_json(from);
80}
81
82/// \}
83
84}
Copyright (c) 2015-2020 by Travis Gockel.
Provides extra information to routines used for extraction and serialization.
Definition context.hpp:26
An adapter for enumeration types.
Simply put, this class is a collection of extractor and serializer instances.
Definition formats.hpp:160
value to_json(const std::type_info &type, const void *from) const
Dynamically convert a type into a JSON value.
serialization_context()
Create a new instance using the default formats (formats::global).
serialization_context(jsonv::formats fmt, std::optional< jsonv::version > ver=std::nullopt, const void *userdata=nullptr)
Create a new instance using the given fmt, ver and userdata.
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
Definition value.hpp:113
Copyright (c) 2014-2020 by Travis Gockel.
#define JSONV_NODISCARD
Warn if the caller discards the result of this function.
Definition config.hpp:121
#define JSONV_PUBLIC
This function or class is part of the public API for JSON Voorhees.
Definition config.hpp:102
Copyright (c) 2015-2020 by Travis Gockel.
Extraction of C++ types from a JSON AST.
Copyright (c) 2015-2020 by Travis Gockel.
value to_json(const T &from, const formats &fmts)
Encode a JSON value from from using the provided fmts.
Copyright (c) 2015-2020 by Travis Gockel.
Copyright (c) 2012-2020 by Travis Gockel.