1 /**
2 License:
3 Boost Software License - Version 1.0 - August 17th, 2003
4 5 Permission is hereby granted, free of charge, to any person or organization
6 obtaining a copy of the software and accompanying documentation covered by
7 this license (the "Software") to use, reproduce, display, distribute,
8 execute, and transmit the Software, and to prepare derivative works of the
9 Software, and to permit third-parties to whom the Software is furnished to
10 do so, all subject to the following:
11 12 The copyright notices in the Software and this entire statement, including
13 the above license grant, this restriction and the following disclaimer,
14 must be included in all copies of the Software, in whole or in part, and
15 all derivative works of the Software, unless such copies or derivative
16 works are solely in the form of machine-executable object code generated by
17 a source language processor.
18 19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
22 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
23 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
24 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 DEALINGS IN THE SOFTWARE.
26 27 Authors:
28 aermicioi
29 **/30 moduleaermicioi.aedi_property_reader.json.json;
31 32 importaermicioi.aedi.storage.storage;
33 importaermicioi.aedi.storage.locator;
34 importaermicioi.aedi_property_reader.json.accessor;
35 importaermicioi.aedi_property_reader.core.accessor;
36 importaermicioi.aedi_property_reader.json.convertor;
37 importaermicioi.aedi_property_reader.json.inspector;
38 importaermicioi.aedi_property_reader.json.type_guesser;
39 importaermicioi.aedi_property_reader.core.convertor;
40 importaermicioi.aedi_property_reader.core.type_guesser;
41 importaermicioi.aedi_property_reader.core.document;
42 importstd.json;
43 importstd.experimental.allocator;
44 importstd.experimental.logger;
45 46 aliasJsonDocumentContainer = AdvisedDocumentContainer!(JSONValue, JSONValue, JsonConvertor);
47 48 /**
49 Create a convertor container with data source being json document.
50 51 Params:
52 value = source of data for container to use to construct components.
53 accessor = property accessing logic
54 guesser = type guesser based on held json value
55 allocator = allocator used to allocate converted values
56 Returns:
57 JsonConvertorContainer
58 **/59 autojson(JSONValuevalue, PropertyAccessor!JSONValueaccessor, TypeGuesser!JSONValueguesser, RCIAllocatorallocator = theAllocator) {
60 61 JsonDocumentContainercontainer = newJsonDocumentContainer(value);
62 container.guesser = guesser;
63 container.accessor = accessor;
64 container.allocator = allocator;
65 66 returncontainer;
67 }
68 69 /**
70 ditto
71 **/72 autojson(JSONValuevalue, TypeGuesser!JSONValueguesser, RCIAllocatorallocator = theAllocator) {
73 74 returnvalue.json(accessor, guesser, allocator);
75 }
76 77 /**
78 ditto
79 **/80 autojson(JSONValuevalue, RCIAllocatorallocator = theAllocator) {
81 82 returnvalue.json(accessor, newJsonTypeGuesser, allocator);
83 }
84 85 /**
86 ditto
87 **/88 autojson(RCIAllocatorallocator = theAllocator) {
89 90 returnJSONValue().json(allocator);
91 }
92 93 /**
94 Create a convertor container with data source being json document.
95 96 Params:
97 pathOrData = path to source of data or source data itself in form of string for container to use to construct components.
98 returnEmpty = wheter to return or not a locator with empty data source
99 Returns:
100 JsonConvertorContainer
101 **/102 autojson(stringpathOrData, boolreturnEmpty = true) {
103 importstd.file;
104 105 debug(trace) trace("Loading json document from ", pathOrData);
106 debug(trace) trace("Checking if ", pathOrData, " is a json file");
107 if (pathOrData.exists) {
108 109 debug(trace) trace("Loading json from ", pathOrData);
110 pathOrData = pathOrData.readText();
111 }
112 113 try {
114 115 debug(trace) trace("Attempting to parse ", pathOrData, " as json");
116 returnjson(parseJSON(pathOrData));
117 } catch (Exceptione) {
118 119 debug(trace) trace("Failed to parse json");
120 if (returnEmpty) {
121 122 debug(trace) trace("Returning empty container");
123 returnjson();
124 }
125 126 thrownewException(
127 "Could not create json convertor container from file or content passed in pathOrData: " ~ pathOrData,
128 e129 );
130 }
131 }
132 133 privateautoaccessor() {
134 returndsl(newJsonPropertyAccessor, newJsonIndexAccessor);
135 }