JSON Voorhees
Killer JSON for C++
serialization_optional.hpp
Go to the documentation of this file.
1 /** \file jsonv/serialization_optional.hpp
2 * Template specialization to support optional<T> serialization.
3 * These are usually not needed unless you are writing your own
4 * \c extractor or \c serializer.
5 *
6 * Copyright (c) 2015 by Travis Gockel. All rights reserved.
7 *
8 * This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
9 * as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
10 * version.
11 *
12 * \author Vladimir Venediktov (vvenedict@gmail.com)
13 **/
14 
15 #ifndef __JSONV_SERIALIZATION_OPTIONAL_HPP_INCLUDED__
16 #define __JSONV_SERIALIZATION_OPTIONAL_HPP_INCLUDED__
17 
19 #include <jsonv/optional.hpp>
20 
21 
22 namespace jsonv
23 {
24 
25 //specialization for optional<T>
26 template <typename T>
28  public adapter_for<optional<T>>
29 {
30  using element_type = optional<T>;
31 
32 protected:
33  virtual optional<T> create(const extraction_context& context, const value& from) const override
34  {
35  optional<T> out;
36  if (from.is_null()) {
37  return out;
38  }
39  out = context.extract<T>(from);
40  return out;
41  }
42 
43  virtual value to_json(const serialization_context& context, const optional<T>& from) const override
44  {
45  value out;
46  if (from) {
47  out = context.to_json(*from);
48  }
49  return out;
50  }
51 };
52 
53 }
54 
55 #endif
56 
An adapter for container types.
Pulls in an implementation of optional.
bool is_null() const
Tests if this kind is kind::null.
Helper types and functions for serialization.
JSONV_OPTIONAL_TYPE< T > optional
Represents a value that may or may not be present.
Definition: optional.hpp:87
value to_json(const T &from) const
Convenience function for converting a C++ object into a JSON value.
T extract(const value &from) const
Attempt to extract a T from from using the formats associated with this context.
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
Definition: value.hpp:131