JSON Voorhees
Killer JSON for C++
Loading...
Searching...
No Matches
container_adapter.hpp
1/// \file jsonv/serialization/serializer_for.hpp
2///
3/// Copyright (c) 2015-2020 by Travis Gockel. All rights reserved.
4///
5/// This program is free software: you can redistribute it and/or modify it under the terms of the Apache License
6/// as published by the Apache Software Foundation, either version 2 of the License, or (at your option) any later
7/// version.
8///
9/// \author Travis Gockel (travis@gockelhut.com)
10#pragma once
11
12#include <jsonv/config.hpp>
14
15#include "adapter_for.hpp"
16
17namespace jsonv
18{
19
20/// \addtogroup Serialization
21/// \{
22
23/// An adapter for container types. This is for convenience of creating an \c adapter for things like \c std::vector,
24/// \c std::set and such.
25///
26/// \tparam TContainer is the container to create and encode. It must have a member type \c value_type, support
27/// iteration and an \c insert operation.
28template <typename TContainer>
30 public adapter_for<TContainer>
31{
32 using element_type = typename TContainer::value_type;
33
34protected:
35 virtual TContainer create(const extraction_context& context, const value& from) const override
36 {
37 using std::end;
38
40 from.as_array(); // get nice error if input is not an array
41 for (value::size_type idx = 0U; idx < from.size(); ++idx)
42 out.insert(end(out), context.extract_sub<element_type>(from, idx));
43 return out;
44 }
45
46 virtual value to_json(const serialization_context& context, const TContainer& from) const override
47 {
48 value out = array();
49 for (const element_type& x : from)
50 out.push_back(context.to_json(x));
51 return out;
52 }
53};
54
55/// \}
56
57}
An adapter for the type T.
An adapter for container types.
An adapter for enumeration types.
Represents a single JSON value, which can be any one of a potential kind, each behaving slightly diff...
Definition value.hpp:107
Copyright (c) 2014-2020 by Travis Gockel.
JSONV_PUBLIC value array()
Create an empty array value.
Conversion between C++ types and JSON values.