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_container; 31 32 import aermicioi.aedi; 33 import aermicioi.aedi_property_reader.json.json_factory; 34 import aermicioi.aedi_property_reader.generic_convertor_container; 35 import aermicioi.aedi.storage.locator; 36 import std.json; 37 import std.range; 38 import std.typecons; 39 40 /** 41 Json document data source/locator used by converting containers. 42 **/ 43 class JsonLocator : Locator!(JSONValue, string) { 44 45 private { 46 JSONValue json_; 47 } 48 49 public { 50 /** 51 Default constructor for JsonLocator 52 53 Initializes container with an empty json object 54 **/ 55 this() { 56 JSONValue[string] obj; 57 this.json = JSONValue(obj); 58 } 59 60 @property { 61 /** 62 Set json 63 64 Params: 65 json = json document used as source 66 67 Returns: 68 typeof(this) 69 **/ 70 JsonLocator json(JSONValue json) @safe nothrow 71 in { 72 assert(json.type == JSON_TYPE.OBJECT, "Json value for locator should be of object type"); 73 } 74 body { 75 this.json_ = json; 76 77 return this; 78 } 79 80 /** 81 Get json 82 83 Returns: 84 JSONValue 85 **/ 86 JSONValue json() @safe nothrow { 87 return this.json_; 88 } 89 } 90 91 /** 92 Get a json element that is accessable from root element. 93 94 Params: 95 path = the element id. 96 97 Throws: 98 NotFoundException in case if the element wasn't found. 99 100 Returns: 101 JSONValue child json element if it is available. 102 **/ 103 JSONValue get(string path) { 104 foreach (key, json; this.json.object) { 105 if (key == path) { 106 return json; 107 } 108 } 109 110 throw new NotFoundException("Could not find child json node identified by " ~ path ~ " in root json"); 111 } 112 113 /** 114 Check if a json element is present in json by key. 115 116 Params: 117 path = identity of element. 118 119 Returns: 120 bool true if an element by key is present in Locator. 121 **/ 122 bool has(in string path) inout { 123 foreach (key, json; this.json_.object) { 124 if (key == path) { 125 return true; 126 } 127 } 128 129 return false; 130 } 131 } 132 } 133 134 /** 135 Generic convertor container version for json documents. 136 **/ 137 alias JsonConvertorContainer = GenericConvertorContainer!(JSONValue, JsonConvertorFactory);