Skip to content

Commit f436c24

Browse files
authored
Update Document.java
1 parent 411efd0 commit f436c24

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

ClorastoreDB/src/main/java/com/clorabase/clorastore/Document.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
import com.google.gson.Gson;
66
import com.google.gson.JsonArray;
7-
import com.google.gson.JsonElement;
87
import com.google.gson.JsonObject;
9-
import com.google.gson.JsonParser;
108
import com.google.gson.JsonPrimitive;
119
import com.google.gson.reflect.TypeToken;
1210

@@ -25,9 +23,10 @@
2523

2624
public class Document {
2725
protected final File document;
28-
protected final Map<String, Object> data;
26+
protected Map<String, Object> data;
2927
public static final int DOCUMENT_MAX_SIZE = 5*1024*1024;
3028
private final Executor executor = Executors.newSingleThreadExecutor();
29+
private final Gson gson = new Gson();
3130

3231
protected Document(File root) {
3332
this.document = root;
@@ -62,13 +61,14 @@ public void put(@NonNull String field, @NonNull Object value) {
6261
* @param fields The data to store in the document
6362
* @throws ClorastoreException If fields contain a value that is not a valid datatype or if an IO error occurred.
6463
*/
65-
public void setData(@NonNull Map<String, ?> fields) {
66-
fields.forEach((s, o) -> validateDatatype(o));
64+
public void setData(@NonNull Map<String, Object> fields) {
65+
fields.values().forEach(this::validateDatatype);
6766

6867
if (document.length() > DOCUMENT_MAX_SIZE && document.delete())
6968
throw new ClorastoreException("Document size exceed 5 MB, it must be less then 5 MB.", Reasons.DOC_SIZE_EXCEED);
7069

71-
var json = new Gson().toJsonTree(fields).getAsJsonObject();
70+
var json = gson.toJsonTree(fields).getAsJsonObject();
71+
data = fields;
7272
executor.execute(() -> {
7373
try {
7474
FileUtils.writeStringToFile(document, json.toString(), Charset.defaultCharset(), false);
@@ -92,7 +92,7 @@ public void setData(@NonNull Map<String, ?> fields) {
9292
return new HashMap<>();
9393

9494
var type = new TypeToken<Map<String, Object>>(){}.getType();
95-
return new Gson().fromJson(content,type);
95+
return gson.fromJson(content,type);
9696
} catch (IOException e) {
9797
throw new UncheckedIOException(e);
9898
}
@@ -149,7 +149,7 @@ public void removeItem(String listName,Object value) {
149149
* @throws ClassCastException If the object is not compatible with the class provided
150150
*/
151151
public <T> T getAsObject(@NonNull Class<T> clazz) throws IOException, ClassCastException {
152-
var gson = new Gson();
152+
System.out.println(data);
153153
return gson.fromJson(gson.toJsonTree(data),clazz);
154154
}
155155

@@ -160,8 +160,10 @@ public <T> T getAsObject(@NonNull Class<T> clazz) throws IOException, ClassCastE
160160
* @param object The POJO
161161
*/
162162
public void setObject(@NonNull Object object) {
163-
var json = new Gson().toJsonTree(object);
164-
setData(json.getAsJsonObject().asMap());
163+
var json = gson.toJsonTree(object);
164+
var type = new TypeToken<Map<String, Object>>(){}.getType();
165+
Map<String,Object> map = gson.fromJson(json,type);
166+
setData(map);
165167
}
166168

167169

@@ -186,7 +188,7 @@ public void delete() {
186188
private void validateDatatype(Object value) {
187189
var isValid = value instanceof String || value instanceof Number || value instanceof Boolean || value instanceof List;
188190
if (!isValid)
189-
throw new ClorastoreException("Datatype not supported. A document can only contain either a 'String' or a subclass of 'Number'", Reasons.ERROR_UNKNOWN);
191+
throw new ClorastoreException("Datatype not supported. A document can only contain either a 'String','Boolean' or a subclass of 'Number'", Reasons.ERROR_UNKNOWN);
190192
}
191193

192194
private void addValueByObject(JsonObject jsonObject, String propertyName, Object value) {
@@ -215,6 +217,6 @@ private void addValueByObject(JsonObject jsonObject, String propertyName, Object
215217

216218
@Override
217219
public String toString() {
218-
return new Gson().toJson(data);
220+
return gson.toJson(data);
219221
}
220222
}

0 commit comments

Comments
 (0)