4
4
5
5
import com .google .gson .Gson ;
6
6
import com .google .gson .JsonArray ;
7
- import com .google .gson .JsonElement ;
8
7
import com .google .gson .JsonObject ;
9
- import com .google .gson .JsonParser ;
10
8
import com .google .gson .JsonPrimitive ;
11
9
import com .google .gson .reflect .TypeToken ;
12
10
25
23
26
24
public class Document {
27
25
protected final File document ;
28
- protected final Map <String , Object > data ;
26
+ protected Map <String , Object > data ;
29
27
public static final int DOCUMENT_MAX_SIZE = 5 *1024 *1024 ;
30
28
private final Executor executor = Executors .newSingleThreadExecutor ();
29
+ private final Gson gson = new Gson ();
31
30
32
31
protected Document (File root ) {
33
32
this .document = root ;
@@ -62,13 +61,14 @@ public void put(@NonNull String field, @NonNull Object value) {
62
61
* @param fields The data to store in the document
63
62
* @throws ClorastoreException If fields contain a value that is not a valid datatype or if an IO error occurred.
64
63
*/
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 );
67
66
68
67
if (document .length () > DOCUMENT_MAX_SIZE && document .delete ())
69
68
throw new ClorastoreException ("Document size exceed 5 MB, it must be less then 5 MB." , Reasons .DOC_SIZE_EXCEED );
70
69
71
- var json = new Gson ().toJsonTree (fields ).getAsJsonObject ();
70
+ var json = gson .toJsonTree (fields ).getAsJsonObject ();
71
+ data = fields ;
72
72
executor .execute (() -> {
73
73
try {
74
74
FileUtils .writeStringToFile (document , json .toString (), Charset .defaultCharset (), false );
@@ -92,7 +92,7 @@ public void setData(@NonNull Map<String, ?> fields) {
92
92
return new HashMap <>();
93
93
94
94
var type = new TypeToken <Map <String , Object >>(){}.getType ();
95
- return new Gson () .fromJson (content ,type );
95
+ return gson .fromJson (content ,type );
96
96
} catch (IOException e ) {
97
97
throw new UncheckedIOException (e );
98
98
}
@@ -149,7 +149,7 @@ public void removeItem(String listName,Object value) {
149
149
* @throws ClassCastException If the object is not compatible with the class provided
150
150
*/
151
151
public <T > T getAsObject (@ NonNull Class <T > clazz ) throws IOException , ClassCastException {
152
- var gson = new Gson ( );
152
+ System . out . println ( data );
153
153
return gson .fromJson (gson .toJsonTree (data ),clazz );
154
154
}
155
155
@@ -160,8 +160,10 @@ public <T> T getAsObject(@NonNull Class<T> clazz) throws IOException, ClassCastE
160
160
* @param object The POJO
161
161
*/
162
162
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 );
165
167
}
166
168
167
169
@@ -186,7 +188,7 @@ public void delete() {
186
188
private void validateDatatype (Object value ) {
187
189
var isValid = value instanceof String || value instanceof Number || value instanceof Boolean || value instanceof List ;
188
190
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 );
190
192
}
191
193
192
194
private void addValueByObject (JsonObject jsonObject , String propertyName , Object value ) {
@@ -215,6 +217,6 @@ private void addValueByObject(JsonObject jsonObject, String propertyName, Object
215
217
216
218
@ Override
217
219
public String toString () {
218
- return new Gson () .toJson (data );
220
+ return gson .toJson (data );
219
221
}
220
222
}
0 commit comments