1 // ----------------------------------------------------------------------------
2 // -                        Open3D: www.open3d.org                            -
3 // ----------------------------------------------------------------------------
4 // The MIT License (MIT)
5 //
6 // Copyright (c) 2018 www.open3d.org
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to deal
10 // in the Software without restriction, including without limitation the rights
11 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 // copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // ----------------------------------------------------------------------------
26 
27 #include "IJsonConvertibleIO.h"
28 
29 #include <unordered_map>
30 #include <Core/Utility/Console.h>
31 #include <Core/Utility/FileSystem.h>
32 
33 namespace three{
34 
35 namespace {
36 
37 static const std::unordered_map<std::string,
38         std::function<bool(const std::string &, IJsonConvertible &)>>
39         file_extension_to_ijsonconvertible_read_function
40         {{"json", ReadIJsonConvertibleFromJSON},
41         };
42 
43 static const std::unordered_map<std::string,
44         std::function<bool(const std::string &, const IJsonConvertible &)>>
45         file_extension_to_ijsonconvertible_write_function
46         {{"json", WriteIJsonConvertibleToJSON},
47         };
48 
49 }    // unnamed namespace
50 
ReadIJsonConvertible(const std::string & filename,IJsonConvertible & object)51 bool ReadIJsonConvertible(const std::string &filename,
52         IJsonConvertible &object)
53 {
54     std::string filename_ext =
55             filesystem::GetFileExtensionInLowerCase(filename);
56     if (filename_ext.empty()) {
57         PrintWarning("Read IJsonConvertible failed: unknown file extension.\n");
58         return false;
59     }
60     auto map_itr =
61             file_extension_to_ijsonconvertible_read_function.find(filename_ext);
62     if (map_itr == file_extension_to_ijsonconvertible_read_function.end()) {
63         PrintWarning("Read IJsonConvertible failed: unknown file extension.\n");
64         return false;
65     }
66     return map_itr->second(filename, object);
67 }
68 
WriteIJsonConvertible(const std::string & filename,const IJsonConvertible & object)69 bool WriteIJsonConvertible(const std::string &filename,
70         const IJsonConvertible &object)
71 {
72     std::string filename_ext =
73             filesystem::GetFileExtensionInLowerCase(filename);
74     if (filename_ext.empty()) {
75         PrintWarning("Write IJsonConvertible failed: unknown file extension.\n");
76         return false;
77     }
78     auto map_itr =
79             file_extension_to_ijsonconvertible_write_function.find(
80             filename_ext);
81     if (map_itr == file_extension_to_ijsonconvertible_write_function.end()) {
82         PrintWarning("Write IJsonConvertible failed: unknown file extension.\n");
83         return false;
84     }
85     return map_itr->second(filename, object);
86 }
87 
88 }    // namespace three
89