1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
11 # Store and load json data.
13 # This simple system can be used to store and retrieve json data.
19 # JsonStore use the file system to store and load json files.
21 # For initialization you need to give the directory used in the
22 # file system to save objects.
25 # var store = new JsonStore("store_dir")
30 # With JsonStore you manage *documents*.
31 # Documents are simple json files that can be stored and loaded from json store.
33 # JsonStore can store documents of type JsonObject and JsonArray.
35 # var red = new JsonObject
37 # red["code"] = "FF0000"
40 # Data are stored under a *key*.
41 # This is the path to the document from `JsonStore::store_dir`
42 # without the `.json` extension.
46 # * key `document` will store data under `store_dir / "document.json"`
47 # * key `collection/data` will store data under `store_dir / "collection/data.json"`
50 # var key = "colors/red"
55 # store.store_object(key, red)
60 # assert store.has_key(key)
61 # var obj = store.load_object(key)
62 # assert obj["name"] == obj["name"]
67 # A collection is a set of documents stored under the same path.
70 # var green = new JsonObject
71 # green["name"] = "green"
72 # green["code"] = "00FF00"
73 # store.store_object("colors/green", green)
75 # assert store.has_collection("colors")
77 # var col = store.list_collection("colors")
78 # assert col.length == 2
79 # assert col.has("green")
80 # assert col.has("red")
85 # You can delete all the data contained in the `JsonStore::store_dir` with `clear`.
95 # A JsonStore can save and load json data from file system.
98 # Directory where data are stored.
100 # Directory is created lazilly at the first write.
101 var store_dir
: String
103 # Delete all stored data.
105 # Warning: all `JsonStore` instances sharing the same `store_dir` will
108 if not store_dir
.file_exists
then return
112 # Is there data are stored under `key`.
113 fun has_key
(key
: String): Bool do
114 return ("{store_dir}/{key}.json".simplify_path
).file_exists
117 # Save `json` object under `key`.
118 fun store_object
(key
: String, json
: JsonObject) do
119 store_json
(key
, json
)
122 # Save `json` array under `key`.
123 fun store_array
(key
: String, json
: JsonArray) do
124 store_json
(key
, json
)
127 # Save `json` data under `key`.
129 # Only `JsonObject` and `JsonArray` are allowed in a json file.
130 # Use `store_object` or `store_array` instead.
131 private fun store_json
(key
: String, json
: Jsonable) do
132 var path
= "{store_dir}/{key}.json".simplify_path
134 var file
= new FileWriter.open
(path
)
135 file
.write
(json
.to_json
)
139 # Load a JsonObject associated to `key` from store.
140 fun load_object
(key
: String): JsonObject do
141 return load_json
(key
).as(JsonObject)
144 # Load a JsonArray associated to `key` from store.
145 fun load_array
(key
: String): JsonArray do
146 return load_json
(key
).as(JsonArray)
149 # Load a JsonObject associated to `key` from store.
151 # Ensure `has_data(key)`
152 private fun load_json
(key
: String): nullable Jsonable do
154 var path
= "{store_dir}/{key}.json".simplify_path
155 var file
= new FileReader.open
(path
)
156 var text
= file
.read_all
158 return text
.parse_json
161 # Get the list of keys stored under the collection `key`.
162 fun list_collection
(key
: String): JsonArray do
163 var res
= new JsonArray
164 var coll
= ("{store_dir}/{key}".simplify_path
).to_path
165 if not coll
.exists
or not coll
.stat
.is_dir
then return res
166 for file
in coll
.files
do
167 if file
.to_s
.has_suffix
(".json") then
168 res
.add
(file
.to_s
.basename
(".json"))
174 # Does `key` matches a collection?
175 fun has_collection
(key
: String): Bool do
176 var path
= ("{store_dir}/{key}".simplify_path
).to_path
177 return path
.exists
and path
.stat
.is_dir