/src/oatpp/src/oatpp/json/ObjectMapper.cpp
Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * |
3 | | * Project _____ __ ____ _ _ |
4 | | * ( _ ) /__\ (_ _)_| |_ _| |_ |
5 | | * )(_)( /(__)\ )( (_ _)(_ _) |
6 | | * (_____)(__)(__)(__) |_| |_| |
7 | | * |
8 | | * |
9 | | * Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com> |
10 | | * |
11 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
12 | | * you may not use this file except in compliance with the License. |
13 | | * You may obtain a copy of the License at |
14 | | * |
15 | | * http://www.apache.org/licenses/LICENSE-2.0 |
16 | | * |
17 | | * Unless required by applicable law or agreed to in writing, software |
18 | | * distributed under the License is distributed on an "AS IS" BASIS, |
19 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20 | | * See the License for the specific language governing permissions and |
21 | | * limitations under the License. |
22 | | * |
23 | | ***************************************************************************/ |
24 | | |
25 | | #include "ObjectMapper.hpp" |
26 | | |
27 | | namespace oatpp { namespace json { |
28 | | |
29 | | ObjectMapper::ObjectMapper(const SerializerConfig& serializerConfig, const DeserializerConfig& deserializerConfig) |
30 | 3.96k | : data::mapping::ObjectMapper(getMapperInfo()) |
31 | 3.96k | , m_serializerConfig(serializerConfig) |
32 | 3.96k | , m_deserializerConfig(deserializerConfig) |
33 | 3.96k | {} |
34 | | |
35 | 2.15k | void ObjectMapper::writeTree(data::stream::ConsistentOutputStream* stream, const data::mapping::Tree& tree, data::mapping::ErrorStack& errorStack) const { |
36 | 2.15k | Serializer::State state; |
37 | 2.15k | state.config = &m_serializerConfig.json; |
38 | 2.15k | state.tree = &tree; |
39 | 2.15k | Serializer::serializeToStream(stream, state); |
40 | 2.15k | if(!state.errorStack.empty()) { |
41 | 0 | errorStack = std::move(state.errorStack); |
42 | 0 | return; |
43 | 0 | } |
44 | 2.15k | } |
45 | | |
46 | 2.15k | void ObjectMapper::write(data::stream::ConsistentOutputStream* stream, const oatpp::Void& variant, data::mapping::ErrorStack& errorStack) const { |
47 | | |
48 | | /* if variant is Tree - we can serialize it right away */ |
49 | 2.15k | if(variant.getValueType() == oatpp::Tree::Class::getType()) { |
50 | 0 | auto tree = static_cast<const data::mapping::Tree*>(variant.get()); |
51 | 0 | writeTree(stream, *tree, errorStack); |
52 | 0 | return; |
53 | 0 | } |
54 | | |
55 | 2.15k | data::mapping::Tree tree; |
56 | 2.15k | data::mapping::ObjectToTreeMapper::State state; |
57 | | |
58 | 2.15k | state.config = &m_serializerConfig.mapper; |
59 | 2.15k | state.tree = &tree; |
60 | | |
61 | 2.15k | m_objectToTreeMapper.map(state, variant); |
62 | 2.15k | if(!state.errorStack.empty()) { |
63 | 0 | errorStack = std::move(state.errorStack); |
64 | 0 | return; |
65 | 0 | } |
66 | | |
67 | 2.15k | writeTree(stream, tree, errorStack); |
68 | | |
69 | 2.15k | } |
70 | | |
71 | 3.96k | oatpp::Void ObjectMapper::read(utils::parser::Caret& caret, const data::type::Type* type, data::mapping::ErrorStack& errorStack) const { |
72 | | |
73 | 3.96k | data::mapping::Tree tree; |
74 | | |
75 | 3.96k | { |
76 | 3.96k | Deserializer::State state; |
77 | 3.96k | state.caret = ⁁ |
78 | 3.96k | state.tree = &tree; |
79 | 3.96k | state.config = &m_deserializerConfig.json; |
80 | 3.96k | Deserializer::deserialize(state); |
81 | 3.96k | if(!state.errorStack.empty()) { |
82 | 662 | errorStack = std::move(state.errorStack); |
83 | 662 | return nullptr; |
84 | 662 | } |
85 | 3.96k | } |
86 | | |
87 | | /* if expected type is Tree (root element is Tree) - then we can just move deserialized tree */ |
88 | 3.30k | if(type == data::type::Tree::Class::getType()) { |
89 | 0 | return oatpp::Tree(std::move(tree)); |
90 | 0 | } |
91 | | |
92 | 3.30k | { |
93 | 3.30k | data::mapping::TreeToObjectMapper::State state; |
94 | 3.30k | state.tree = &tree; |
95 | 3.30k | state.config = &m_deserializerConfig.mapper; |
96 | 3.30k | const auto & result = m_treeToObjectMapper.map(state, type); |
97 | 3.30k | if(!state.errorStack.empty()) { |
98 | 594 | errorStack = std::move(state.errorStack); |
99 | 594 | return nullptr; |
100 | 594 | } |
101 | 2.71k | return result; |
102 | 3.30k | } |
103 | | |
104 | 3.30k | } |
105 | | |
106 | 0 | const data::mapping::ObjectToTreeMapper& ObjectMapper::objectToTreeMapper() const { |
107 | 0 | return m_objectToTreeMapper; |
108 | 0 | } |
109 | | |
110 | 0 | const data::mapping::TreeToObjectMapper& ObjectMapper::treeToObjectMapper() const { |
111 | 0 | return m_treeToObjectMapper; |
112 | 0 | } |
113 | | |
114 | 0 | data::mapping::ObjectToTreeMapper& ObjectMapper::objectToTreeMapper() { |
115 | 0 | return m_objectToTreeMapper; |
116 | 0 | } |
117 | | |
118 | 0 | data::mapping::TreeToObjectMapper& ObjectMapper::treeToObjectMapper() { |
119 | 0 | return m_treeToObjectMapper; |
120 | 0 | } |
121 | | |
122 | 0 | const ObjectMapper::SerializerConfig& ObjectMapper::serializerConfig() const { |
123 | 0 | return m_serializerConfig; |
124 | 0 | } |
125 | | |
126 | 0 | const ObjectMapper::DeserializerConfig& ObjectMapper::deserializerConfig() const { |
127 | 0 | return m_deserializerConfig; |
128 | 0 | } |
129 | | |
130 | 0 | ObjectMapper::SerializerConfig& ObjectMapper::serializerConfig() { |
131 | 0 | return m_serializerConfig; |
132 | 0 | } |
133 | | |
134 | 15.8k | ObjectMapper::DeserializerConfig& ObjectMapper::deserializerConfig() { |
135 | 15.8k | return m_deserializerConfig; |
136 | 15.8k | } |
137 | | |
138 | | }} |