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 module aermicioi.aedi_property_reader.json.json;
31 
32 import aermicioi.aedi.storage.storage;
33 import aermicioi.aedi.storage.locator;
34 import aermicioi.aedi_property_reader.json.accessor;
35 import aermicioi.aedi_property_reader.core.accessor;
36 import aermicioi.aedi_property_reader.json.convertor;
37 import aermicioi.aedi_property_reader.json.inspector;
38 import aermicioi.aedi_property_reader.json.type_guesser;
39 import aermicioi.aedi_property_reader.core.convertor;
40 import aermicioi.aedi_property_reader.core.type_guesser;
41 import aermicioi.aedi_property_reader.core.document;
42 import std.json;
43 import std.experimental.allocator;
44 import std.experimental.logger;
45 
46 alias JsonDocumentContainer = 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 auto json(JSONValue value, PropertyAccessor!JSONValue accessor, TypeGuesser!JSONValue guesser, RCIAllocator allocator = theAllocator) {
60 
61     JsonDocumentContainer container = new JsonDocumentContainer(value);
62     container.guesser = guesser;
63     container.accessor = accessor;
64     container.allocator = allocator;
65 
66     return container;
67 }
68 
69 /**
70 ditto
71 **/
72 auto json(JSONValue value, TypeGuesser!JSONValue guesser, RCIAllocator allocator = theAllocator) {
73 
74     return value.json(accessor, guesser, allocator);
75 }
76 
77 /**
78 ditto
79 **/
80 auto json(JSONValue value, RCIAllocator allocator = theAllocator) {
81 
82     return value.json(accessor, new JsonTypeGuesser, allocator);
83 }
84 
85 /**
86 ditto
87 **/
88 auto json(RCIAllocator allocator = theAllocator) {
89 
90     return JSONValue().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 auto json(string pathOrData, bool returnEmpty = true) {
103     import std.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         return json(parseJSON(pathOrData));
117     } catch (Exception e) {
118 
119         debug(trace) trace("Failed to parse json");
120         if (returnEmpty) {
121 
122             debug(trace) trace("Returning empty container");
123             return json();
124         }
125 
126         throw new Exception(
127             "Could not create json convertor container from file or content passed in pathOrData: " ~ pathOrData,
128             e
129         );
130     }
131 }
132 
133 private auto accessor() {
134     return dsl(new JsonPropertyAccessor, new JsonIndexAccessor);
135 }