1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "json_importer.h"
17
18 #include <core/io/intf_file_manager.h>
19
20 #include <meta/api/util.h>
21
22 #include "backend/json_input.h"
23 #include "backend/json_output.h"
24 #include "metav1_compat.h"
25
26 META_BEGIN_NAMESPACE()
27 namespace Serialization {
28
JsonImporter()29 JsonImporter::JsonImporter() : transformations_ { CreateShared<MetaMigrateV1>() } {}
30
Import(const ISerNode::ConstPtr & tree)31 IObject::Ptr JsonImporter::Import(const ISerNode::ConstPtr& tree)
32 {
33 return imp_.Import(tree);
34 }
35
ImportAsTree(CORE_NS::IFile & input)36 ISerNode::Ptr JsonImporter::ImportAsTree(CORE_NS::IFile& input)
37 {
38 ISerNode::Ptr tree;
39 BASE_NS::string data;
40 data.resize(input.GetLength());
41 if (input.Read(data.data(), data.size()) == data.size()) {
42 JsonInput json;
43 tree = json.Process(data);
44 if (tree) {
45 tree = Transform(tree, json.GetVersion());
46 }
47 }
48 return tree;
49 }
50
Import(CORE_NS::IFile & input)51 IObject::Ptr JsonImporter::Import(CORE_NS::IFile& input)
52 {
53 if (auto tree = ImportAsTree(input)) {
54 return Import(tree);
55 }
56 return nullptr;
57 }
58
Transform(ISerNode::Ptr tree,const Version & ver)59 ISerNode::Ptr JsonImporter::Transform(ISerNode::Ptr tree, const Version& ver)
60 {
61 bool transformed = false;
62 for (auto&& t : transformations_) {
63 if (ver < t->ApplyForLessThanVersion()) {
64 transformed = true;
65 tree = t->Process(tree);
66 if (!tree) {
67 CORE_LOG_E("Transforming serialisation tree failed (step: %s)", t->GetName().c_str());
68 return nullptr;
69 }
70 }
71 }
72 #ifdef _DEBUG
73 // --- debug
74 if (transformed) {
75 JsonOutput backend;
76 auto json = backend.Process(tree);
77 if (json.empty()) {
78 CORE_LOG_E("Fail");
79 return nullptr;
80 }
81 auto f = CORE_NS::GetPluginRegister().GetFileManager().CreateFile("file://./rewrite.json");
82 f->Write(json.c_str(), json.size());
83 }
84 // ---
85 #endif
86 return tree;
87 }
88
GetTransformations() const89 BASE_NS::vector<ISerTransformation::Ptr> JsonImporter::GetTransformations() const
90 {
91 return transformations_;
92 }
SetTransformations(BASE_NS::vector<ISerTransformation::Ptr> t)93 void JsonImporter::SetTransformations(BASE_NS::vector<ISerTransformation::Ptr> t)
94 {
95 transformations_ = BASE_NS::move(t);
96 }
GetInstanceIdMapping() const97 BASE_NS::unordered_map<InstanceId, InstanceId> JsonImporter::GetInstanceIdMapping() const
98 {
99 return imp_.GetInstanceIdMapping();
100 }
SetResourceManager(CORE_NS::IResourceManager::Ptr p)101 void JsonImporter::SetResourceManager(CORE_NS::IResourceManager::Ptr p)
102 {
103 imp_.SetResourceManager(BASE_NS::move(p));
104 }
SetUserContext(IObject::Ptr p)105 void JsonImporter::SetUserContext(IObject::Ptr p)
106 {
107 imp_.SetUserContext(BASE_NS::move(p));
108 }
GetMetadata() const109 SerMetadata JsonImporter::GetMetadata() const
110 {
111 return imp_.GetMetadata();
112 }
113
114 } // namespace Serialization
115 META_END_NAMESPACE()
116