/
ORecordSerializerJSON.java
executable file
·747 lines (648 loc) · 30.6 KB
/
ORecordSerializerJSON.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
*
* * Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
* *
* * Licensed under the Apache License, Version 2.0 (the "License");
* * you may not use this file except in compliance with the License.
* * You may obtain a copy of the License at
* *
* * http://www.apache.org/licenses/LICENSE-2.0
* *
* * Unless required by applicable law or agreed to in writing, software
* * distributed under the License is distributed on an "AS IS" BASIS,
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* * See the License for the specific language governing permissions and
* * limitations under the License.
* *
* * For more information: http://www.orientechnologies.com
*
*/
package com.orientechnologies.orient.core.serialization.serializer.record.string;
import com.orientechnologies.common.collection.OMultiValue;
import com.orientechnologies.common.parser.OStringParser;
import com.orientechnologies.orient.core.Orient;
import com.orientechnologies.orient.core.db.ODatabaseRecordThreadLocal;
import com.orientechnologies.orient.core.db.OUserObject2RecordHandler;
import com.orientechnologies.orient.core.db.document.ODatabaseDocument;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.db.record.ORecordLazyList;
import com.orientechnologies.orient.core.db.record.ORecordLazyMultiValue;
import com.orientechnologies.orient.core.db.record.ORecordLazySet;
import com.orientechnologies.orient.core.db.record.OTrackedList;
import com.orientechnologies.orient.core.db.record.OTrackedSet;
import com.orientechnologies.orient.core.db.record.ridbag.ORidBag;
import com.orientechnologies.orient.core.exception.OSerializationException;
import com.orientechnologies.orient.core.fetch.OFetchHelper;
import com.orientechnologies.orient.core.fetch.OFetchPlan;
import com.orientechnologies.orient.core.fetch.json.OJSONFetchContext;
import com.orientechnologies.orient.core.fetch.json.OJSONFetchListener;
import com.orientechnologies.orient.core.id.ORID;
import com.orientechnologies.orient.core.id.ORecordId;
import com.orientechnologies.orient.core.metadata.schema.OClass;
import com.orientechnologies.orient.core.metadata.schema.OProperty;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.orientechnologies.orient.core.record.ORecord;
import com.orientechnologies.orient.core.record.ORecordInternal;
import com.orientechnologies.orient.core.record.ORecordStringable;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.record.impl.ODocumentHelper;
import com.orientechnologies.orient.core.record.impl.ODocumentInternal;
import com.orientechnologies.orient.core.record.impl.ORecordBytes;
import com.orientechnologies.orient.core.serialization.OBase64Utils;
import com.orientechnologies.orient.core.serialization.serializer.OJSONWriter;
import com.orientechnologies.orient.core.serialization.serializer.OStringSerializerHelper;
import com.orientechnologies.orient.core.util.ODateHelper;
import java.io.IOException;
import java.io.StringWriter;
import java.text.ParseException;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
@SuppressWarnings("serial")
public class ORecordSerializerJSON extends ORecordSerializerStringAbstract {
public static final String NAME = "json";
public static final ORecordSerializerJSON INSTANCE = new ORecordSerializerJSON();
public static final String ATTRIBUTE_FIELD_TYPES = "@fieldTypes";
public static final char[] PARAMETER_SEPARATOR = new char[] { ':', ',' };
public static final int INITIAL_SIZE = 5000;
private static final Long MAX_INT = (long) Integer.MAX_VALUE;
private static final Long MIN_INT = (long) Integer.MIN_VALUE;
private static final Double MAX_FLOAT = (double) Float.MAX_VALUE;
private static final Double MIN_FLOAT = (double) Float.MIN_VALUE;
private interface CollectionItemVisitor {
void visitItem(Object item);
}
public class FormatSettings {
public boolean includeVer;
public boolean includeType;
public boolean includeId;
public boolean includeClazz;
public boolean attribSameRow;
public boolean alwaysFetchEmbeddedDocuments;
public int indentLevel;
public String fetchPlan = null;
public boolean keepTypes = true;
public boolean dateAsLong = false;
public boolean prettyPrint = false;
public FormatSettings(final String iFormat) {
if (iFormat == null) {
includeType = true;
includeVer = true;
includeId = true;
includeClazz = true;
attribSameRow = true;
indentLevel = 1;
fetchPlan = "";
keepTypes = true;
alwaysFetchEmbeddedDocuments = true;
} else {
includeType = false;
includeVer = false;
includeId = false;
includeClazz = false;
attribSameRow = false;
alwaysFetchEmbeddedDocuments = false;
indentLevel = 1;
keepTypes = false;
if (iFormat != null && !iFormat.isEmpty()) {
final String[] format = iFormat.split(",");
for (String f : format)
if (f.equals("type"))
includeType = true;
else if (f.equals("rid"))
includeId = true;
else if (f.equals("version"))
includeVer = true;
else if (f.equals("class"))
includeClazz = true;
else if (f.equals("attribSameRow"))
attribSameRow = true;
else if (f.startsWith("indent"))
indentLevel = Integer.parseInt(f.substring(f.indexOf(':') + 1));
else if (f.startsWith("fetchPlan"))
fetchPlan = f.substring(f.indexOf(':') + 1);
else if (f.startsWith("keepTypes"))
keepTypes = true;
else if (f.startsWith("alwaysFetchEmbedded"))
alwaysFetchEmbeddedDocuments = true;
else if (f.startsWith("dateAsLong"))
dateAsLong = true;
else if (f.startsWith("prettyPrint"))
prettyPrint = true;
else if (f.startsWith("graph") || f.startsWith("shallow"))
// SUPPORTED IN OTHER PARTS
;
else
throw new IllegalArgumentException("Unrecognized JSON formatting option: " + f);
}
}
}
}
@Override
public int getCurrentVersion() {
return 0;
}
@Override
public int getMinSupportedVersion() {
return 0;
}
public ORecord fromString(String iSource, ORecord iRecord, final String[] iFields, boolean needReload) {
return fromString(iSource, iRecord, iFields, null, needReload);
}
@Override
public ORecord fromString(String iSource, ORecord iRecord, final String[] iFields) {
return fromString(iSource, iRecord, iFields, null, false);
}
public ORecord fromString(String iSource, ORecord iRecord, final String[] iFields, final String iOptions, boolean needReload) {
iSource = unwrapSource(iSource);
boolean noMap = false;
if (iOptions != null) {
final String[] format = iOptions.split(",");
for (String f : format)
if (f.equalsIgnoreCase("noMap"))
noMap = true;
}
if (iRecord != null)
// RESET ALL THE FIELDS
iRecord.clear();
final List<String> fields = OStringSerializerHelper.smartSplit(iSource, PARAMETER_SEPARATOR, 0, -1, true, true, false, false,
' ', '\n', '\r', '\t');
if (fields.size() % 2 != 0)
throw new OSerializationException("Error on unmarshalling JSON content: wrong format \"" + iSource
+ "\". Use <field> : <value>");
Map<String, Character> fieldTypes = null;
if (fields != null && fields.size() > 0) {
// SEARCH FOR FIELD TYPES IF ANY
for (int i = 0; i < fields.size(); i += 2) {
final String fieldName = OStringSerializerHelper.getStringContent(fields.get(i));
final String fieldValue = fields.get(i + 1);
final String fieldValueAsString = OStringSerializerHelper.getStringContent(fieldValue);
if (fieldName.equals(ATTRIBUTE_FIELD_TYPES) && iRecord instanceof ODocument) {
fieldTypes = loadFieldTypes(fieldTypes, fieldValueAsString);
} else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_TYPE)) {
if (iRecord == null || ORecordInternal.getRecordType(iRecord) != fieldValueAsString.charAt(0)) {
// CREATE THE RIGHT RECORD INSTANCE
iRecord = Orient.instance().getRecordFactoryManager().newInstance((byte) fieldValueAsString.charAt(0));
}
} else if (needReload && fieldName.equals(ODocumentHelper.ATTRIBUTE_RID) && iRecord instanceof ODocument) {
if (fieldValue != null && fieldValue.length() > 0) {
ORecord localRecord = ODatabaseRecordThreadLocal.INSTANCE.get().load(new ORecordId(fieldValueAsString));
if (localRecord != null)
iRecord = localRecord;
}
} else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_CLASS) && iRecord instanceof ODocument) {
((ODocument) iRecord).setClassNameIfExists("null".equals(fieldValueAsString) ? null : fieldValueAsString);
}
}
if (iRecord == null)
iRecord = new ODocument();
try {
int recordVersion = 0;
long timestamp = 0L;
long macAddress = 0L;
for (int i = 0; i < fields.size(); i += 2) {
final String fieldName = OStringSerializerHelper.getStringContent(fields.get(i));
final String fieldValue = fields.get(i + 1);
final String fieldValueAsString = OStringSerializerHelper.getStringContent(fieldValue);
// RECORD ATTRIBUTES
if (fieldName.equals(ODocumentHelper.ATTRIBUTE_RID))
ORecordInternal.setIdentity(iRecord, new ORecordId(fieldValueAsString));
else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_VERSION))
iRecord.getRecordVersion().setCounter(Integer.parseInt(fieldValue));
else if (fieldName.equals(ODocumentHelper.ATTRIBUTE_TYPE)) {
continue;
} else if (fieldName.equals(ATTRIBUTE_FIELD_TYPES) && iRecord instanceof ODocument) {
continue;
} else if (fieldName.equals("value") && !(iRecord instanceof ODocument)) {
// RECORD VALUE(S)
if ("null".equals(fieldValue))
iRecord.fromStream(new byte[] {});
else if (iRecord instanceof ORecordBytes) {
// BYTES
iRecord.fromStream(OBase64Utils.decode(fieldValueAsString));
} else if (iRecord instanceof ORecordStringable) {
((ORecordStringable) iRecord).value(fieldValueAsString);
} else
throw new IllegalArgumentException("unsupported type of record");
} else if (iRecord instanceof ODocument) {
final ODocument doc = ((ODocument) iRecord);
// DETERMINE THE TYPE FROM THE SCHEMA
OType type = determineType(doc, fieldName);
final Object v = getValue(doc, fieldName, fieldValue, fieldValueAsString, type, null, fieldTypes, noMap, iOptions);
if (v != null)
if (v instanceof Collection<?> && !((Collection<?>) v).isEmpty()) {
if (v instanceof ORecordLazyMultiValue)
((ORecordLazyMultiValue) v).setAutoConvertToRecord(false);
// CHECK IF THE COLLECTION IS EMBEDDED
if (type == null) {
// TRY TO UNDERSTAND BY FIRST ITEM
Object first = ((Collection<?>) v).iterator().next();
if (first != null && first instanceof ORecord && !((ORecord) first).getIdentity().isValid())
type = v instanceof Set<?> ? OType.EMBEDDEDSET : OType.EMBEDDEDLIST;
}
if (type != null) {
// TREAT IT AS EMBEDDED
doc.field(fieldName, v, type);
continue;
}
} else if (v instanceof Map<?, ?> && !((Map<?, ?>) v).isEmpty()) {
// CHECK IF THE MAP IS EMBEDDED
Object first = ((Map<?, ?>) v).values().iterator().next();
if (first != null && first instanceof ORecord && !((ORecord) first).getIdentity().isValid()) {
doc.field(fieldName, v, OType.EMBEDDEDMAP);
continue;
}
} else if (v instanceof ODocument && type != null && type.isLink()) {
String className = ((ODocument) v).getClassName();
if (className != null && className.length() > 0)
((ODocument) v).save();
}
if (type == null && fieldTypes != null && fieldTypes.containsKey(fieldName))
type = ORecordSerializerStringAbstract.getType(fieldValue, fieldTypes.get(fieldName));
if (v instanceof OTrackedSet<?>) {
if (OMultiValue.getFirstValue((Set<?>) v) instanceof OIdentifiable)
type = OType.LINKSET;
} else if (v instanceof OTrackedList<?>) {
if (OMultiValue.getFirstValue((List<?>) v) instanceof OIdentifiable)
type = OType.LINKLIST;
}
if (type != null)
doc.field(fieldName, v, type);
else
doc.field(fieldName, v);
}
}
} catch (Exception e) {
if (iRecord.getIdentity().isValid())
throw new OSerializationException("Error on unmarshalling JSON content for record " + iRecord.getIdentity(), e);
else
throw new OSerializationException("Error on unmarshalling JSON content for record: " + iSource, e);
}
}
return iRecord;
}
@Override
public StringBuilder toString(final ORecord iRecord, final StringBuilder iOutput, final String iFormat,
final OUserObject2RecordHandler iObjHandler, final Map<ODocument, Boolean> iMarshalledRecords, boolean iOnlyDelta,
boolean autoDetectCollectionType) {
try {
final StringWriter buffer = new StringWriter(INITIAL_SIZE);
final OJSONWriter json = new OJSONWriter(buffer, iFormat);
final FormatSettings settings = new FormatSettings(iFormat);
json.beginObject();
OJSONFetchContext context = new OJSONFetchContext(json, settings);
context.writeSignature(json, iRecord);
if (iRecord instanceof ODocument) {
final OFetchPlan fp = OFetchHelper.buildFetchPlan(settings.fetchPlan);
OFetchHelper.fetch(iRecord, null, fp, new OJSONFetchListener(), context, iFormat);
} else if (iRecord instanceof ORecordStringable) {
// STRINGABLE
final ORecordStringable record = (ORecordStringable) iRecord;
json.writeAttribute(settings.indentLevel + 1, true, "value", record.value());
} else if (iRecord instanceof ORecordBytes) {
// BYTES
final ORecordBytes record = (ORecordBytes) iRecord;
json.writeAttribute(settings.indentLevel + 1, true, "value", OBase64Utils.encodeBytes(record.toStream()));
} else
throw new OSerializationException("Error on marshalling record of type '" + iRecord.getClass()
+ "' to JSON. The record type cannot be exported to JSON");
json.endObject(0, true);
iOutput.append(buffer);
return iOutput;
} catch (IOException e) {
throw new OSerializationException("Error on marshalling of record to JSON", e);
}
}
@Override
public String toString() {
return NAME;
}
private OType determineType(ODocument doc, String fieldName) {
OType type = null;
final OClass cls = ODocumentInternal.getImmutableSchemaClass(doc);
if (cls != null) {
final OProperty prop = cls.getProperty(fieldName);
if (prop != null)
type = prop.getType();
}
return type;
}
private Map<String, Character> loadFieldTypes(Map<String, Character> fieldTypes, String fieldValueAsString) {
// LOAD THE FIELD TYPE MAP
final String[] fieldTypesParts = fieldValueAsString.split(",");
if (fieldTypesParts.length > 0) {
fieldTypes = new HashMap<String, Character>();
String[] part;
for (String f : fieldTypesParts) {
part = f.split("=");
if (part.length == 2)
fieldTypes.put(part[0], part[1].charAt(0));
}
}
return fieldTypes;
}
private String unwrapSource(String iSource) {
if (iSource == null)
throw new OSerializationException("Error on unmarshalling JSON content: content is null");
iSource = iSource.trim();
if (!iSource.startsWith("{") || !iSource.endsWith("}"))
throw new OSerializationException("Error on unmarshalling JSON content '" + iSource + "': content must be between { }");
iSource = iSource.substring(1, iSource.length() - 1).trim();
return iSource;
}
@SuppressWarnings("unchecked")
private Object getValue(final ODocument iRecord, String iFieldName, String iFieldValue, String iFieldValueAsString, OType iType,
OType iLinkedType, final Map<String, Character> iFieldTypes, final boolean iNoMap, final String iOptions) {
if (iFieldValue.equals("null"))
return null;
if (iFieldName != null && ODocumentInternal.getImmutableSchemaClass(iRecord) != null) {
final OProperty p = ODocumentInternal.getImmutableSchemaClass(iRecord).getProperty(iFieldName);
if (p != null) {
iType = p.getType();
iLinkedType = p.getLinkedType();
}
}
if (iType == null && iFieldTypes != null && iFieldTypes.containsKey(iFieldName))
iType = ORecordSerializerStringAbstract.getType(iFieldValue, iFieldTypes.get(iFieldName));
if (iFieldValue.startsWith("{") && iFieldValue.endsWith("}")) {
return getValueAsObjectOrMap(iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
} else if (iFieldValue.startsWith("[") && iFieldValue.endsWith("]")) {
return getValueAsCollection(iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions);
}
if (iType == null)
// TRY TO DETERMINE THE CONTAINED TYPE from THE FIRST VALUE
if (iFieldValue.charAt(0) != '\"' && iFieldValue.charAt(0) != '\'') {
if (iFieldValue.equalsIgnoreCase("false") || iFieldValue.equalsIgnoreCase("true"))
iType = OType.BOOLEAN;
else {
Character c = null;
if (iFieldTypes != null) {
c = iFieldTypes.get(iFieldName);
if (c != null)
iType = ORecordSerializerStringAbstract.getType(iFieldValue + c);
}
if (c == null && !iFieldValue.isEmpty()) {
// TRY TO AUTODETERMINE THE BEST TYPE
if (ORecordId.isA(iFieldValue))
iType = OType.LINK;
else if (OStringSerializerHelper.contains(iFieldValue, '.')) {
// DECIMAL FORMAT: DETERMINE IF DOUBLE OR FLOAT
final Double v = new Double(OStringSerializerHelper.getStringContent(iFieldValue));
if (canBeTrunkedToFloat(v))
return v.floatValue();
else
return v;
} else {
final Long v = new Long(OStringSerializerHelper.getStringContent(iFieldValue));
// INTEGER FORMAT: DETERMINE IF DOUBLE OR FLOAT
if (canBeTrunkedToInt(v))
return v.intValue();
else
return v;
}
}
}
} else if (iFieldValue.startsWith("{") && iFieldValue.endsWith("}"))
iType = OType.EMBEDDED;
else {
if (ORecordId.isA(iFieldValueAsString))
iType = OType.LINK;
if (iFieldTypes != null) {
Character c = iFieldTypes.get(iFieldName);
if (c != null)
iType = ORecordSerializerStringAbstract.getType(iFieldValueAsString, c);
}
if (iType == null)
iType = OType.STRING;
}
if (iType != null)
switch (iType) {
case STRING:
return decodeJSON(iFieldValueAsString);
case LINK:
final int pos = iFieldValueAsString.indexOf('@');
if (pos > -1)
// CREATE DOCUMENT
return new ODocument(iFieldValueAsString.substring(1, pos), new ORecordId(iFieldValueAsString.substring(pos + 1)));
else {
// CREATE SIMPLE RID
return new ORecordId(iFieldValueAsString);
}
case EMBEDDED:
return fromString(iFieldValueAsString);
case DATE:
if (iFieldValueAsString == null || iFieldValueAsString.equals(""))
return null;
try {
// TRY TO PARSE AS LONG
return Long.parseLong(iFieldValueAsString);
} catch (NumberFormatException e) {
try {
// TRY TO PARSE AS DATE
return ODateHelper.getDateFormatInstance().parseObject(iFieldValueAsString);
} catch (ParseException ex) {
throw new OSerializationException("Unable to unmarshall date (format=" + ODateHelper.getDateFormat() + ") : "
+ iFieldValueAsString, e);
}
}
case DATETIME:
if (iFieldValueAsString == null || iFieldValueAsString.equals(""))
return null;
try {
// TRY TO PARSE AS LONG
return Long.parseLong(iFieldValueAsString);
} catch (NumberFormatException e) {
try {
// TRY TO PARSE AS DATETIME
return ODateHelper.getDateTimeFormatInstance().parseObject(iFieldValueAsString);
} catch (ParseException ex) {
throw new OSerializationException("Unable to unmarshall datetime (format=" + ODateHelper.getDateTimeFormat() + ") : "
+ iFieldValueAsString, e);
}
}
case BINARY:
return OStringSerializerHelper.fieldTypeFromStream(iRecord, iType, iFieldValueAsString);
default:
return OStringSerializerHelper.fieldTypeFromStream(iRecord, iType, iFieldValue);
}
return iFieldValueAsString;
}
private boolean canBeTrunkedToInt(Long v) {
return (v > 0) ? v.compareTo(MAX_INT) <= 0 : v.compareTo(MIN_INT) >= 0;
}
private boolean canBeTrunkedToFloat(Double v) {
// TODO not really correct check. Small numbers with high precision will be trunked while they shouldn't be
return (v > 0) ? v.compareTo(MAX_FLOAT) <= 0 : v.compareTo(MIN_FLOAT) >= 0;
}
/**
* OBJECT OR MAP. CHECK THE TYPE ATTRIBUTE TO KNOW IT.
*/
private Object getValueAsObjectOrMap(ODocument iRecord, String iFieldValue, OType iType, OType iLinkedType,
Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
final String[] fields = OStringParser.getWords(iFieldValue.substring(1, iFieldValue.length() - 1), ":,", true);
if (fields == null || fields.length == 0)
if (iNoMap) {
ODocument res = new ODocument();
ODocumentInternal.addOwner(res, iRecord);
return res;
} else
return new HashMap<String, Object>();
if (iNoMap || hasTypeField(fields)) {
return getValueAsRecord(iRecord, iFieldValue, iType, iOptions, fields);
} else {
return getValueAsMap(iRecord, iFieldValue, iLinkedType, iFieldTypes, false, iOptions, fields);
}
}
private Object getValueAsMap(ODocument iRecord, String iFieldValue, OType iLinkedType, Map<String, Character> iFieldTypes,
boolean iNoMap, String iOptions, String[] fields) {
if (fields.length % 2 == 1)
throw new OSerializationException("Bad JSON format on map. Expected pairs of field:value but received '" + iFieldValue + "'");
final Map<String, Object> embeddedMap = new LinkedHashMap<String, Object>();
for (int i = 0; i < fields.length; i += 2) {
String iFieldName = fields[i];
if (iFieldName.length() >= 2)
iFieldName = iFieldName.substring(1, iFieldName.length() - 1);
iFieldValue = fields[i + 1];
final String valueAsString = OStringSerializerHelper.getStringContent(iFieldValue);
embeddedMap.put(iFieldName,
getValue(iRecord, null, iFieldValue, valueAsString, iLinkedType, null, iFieldTypes, iNoMap, iOptions));
}
return embeddedMap;
}
private Object getValueAsRecord(ODocument iRecord, String iFieldValue, OType iType, String iOptions, String[] fields) {
ORID rid = new ORecordId(OStringSerializerHelper.getStringContent(getFieldValue("@rid", fields)));
boolean shouldReload = rid.isTemporary();
final ODocument recordInternal = (ODocument) fromString(iFieldValue, new ODocument(), null, iOptions, shouldReload);
if (shouldBeDeserializedAsEmbedded(recordInternal, iType))
ODocumentInternal.addOwner(recordInternal, iRecord);
else {
ODatabaseDocument database = ODatabaseRecordThreadLocal.INSTANCE.get();
if (rid.isPersistent() && database != null) {
ODocument documentToMerge = database.load(rid);
documentToMerge.merge(recordInternal, false, false);
return documentToMerge;
}
}
return recordInternal;
}
private Object getValueAsCollection(ODocument iRecord, String iFieldValue, OType iType, OType iLinkedType,
Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
// remove square brackets
iFieldValue = iFieldValue.substring(1, iFieldValue.length() - 1);
if (iType == OType.LINKBAG) {
final ORidBag bag = new ORidBag();
parseCollection(iRecord, iFieldValue, iType, OType.LINK, iFieldTypes, iNoMap, iOptions, new CollectionItemVisitor() {
@Override
public void visitItem(Object item) {
bag.add((OIdentifiable) item);
}
});
return bag;
} else if (iType == OType.LINKSET) {
return getValueAsLinkedCollection(new ORecordLazySet(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap,
iOptions);
} else if (iType == OType.LINKLIST) {
return getValueAsLinkedCollection(new ORecordLazyList(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes,
iNoMap, iOptions);
} else if (iType == OType.EMBEDDEDSET) {
return getValueAsEmbeddedCollection(new OTrackedSet<Object>(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes,
iNoMap, iOptions);
} else {
return getValueAsEmbeddedCollection(new OTrackedList<Object>(iRecord), iRecord, iFieldValue, iType, iLinkedType, iFieldTypes,
iNoMap, iOptions);
}
}
private Object getValueAsLinkedCollection(final Collection<OIdentifiable> collection, ODocument iRecord, String iFieldValue,
OType iType, OType iLinkedType, Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
parseCollection(iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions, new CollectionItemVisitor() {
@Override
public void visitItem(Object item) {
collection.add((OIdentifiable) item);
}
});
return collection;
}
private Object getValueAsEmbeddedCollection(final Collection<Object> collection, ODocument iRecord, String iFieldValue,
OType iType, OType iLinkedType, Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions) {
parseCollection(iRecord, iFieldValue, iType, iLinkedType, iFieldTypes, iNoMap, iOptions, new CollectionItemVisitor() {
@Override
public void visitItem(Object item) {
collection.add(item);
}
});
return collection;
}
private void parseCollection(ODocument iRecord, String iFieldValue, OType iType, OType iLinkedType,
Map<String, Character> iFieldTypes, boolean iNoMap, String iOptions, CollectionItemVisitor visitor) {
if (!iFieldValue.isEmpty()) {
for (String item : OStringSerializerHelper.smartSplit(iFieldValue, ',')) {
final String itemValue = item.trim();
if (itemValue.length() == 0)
continue;
final Object collectionItem = getValue(iRecord, null, itemValue, OStringSerializerHelper.getStringContent(itemValue),
iLinkedType, null, iFieldTypes, iNoMap, iOptions);
// TODO redundant in some cases, owner is already added by getValue in some cases
if (shouldBeDeserializedAsEmbedded(collectionItem, iType))
ODocumentInternal.addOwner((ODocument) collectionItem, iRecord);
if (collectionItem instanceof String && ((String) collectionItem).length() == 0)
continue;
visitor.visitItem(collectionItem);
}
}
}
private boolean shouldBeDeserializedAsEmbedded(Object record, OType iType) {
return record instanceof ODocument && !((ODocument) record).getIdentity().isTemporary()
&& !((ODocument) record).getIdentity().isPersistent() && (iType == null || !iType.isLink());
}
private String decodeJSON(String iFieldValueAsString) {
if (iFieldValueAsString == null) {
return null;
}
StringBuilder builder = new StringBuilder(iFieldValueAsString.length());
boolean quoting = false;
for (char c : iFieldValueAsString.toCharArray()) {
if (quoting) {
if (c != '\\' && c != '\"' && c != '/') {
builder.append('\\');
}
builder.append(c);
quoting = false;
} else {
if (c == '\\') {
quoting = true;
} else {
builder.append(c);
}
}
}
return builder.toString();
}
private boolean hasTypeField(final String[] fields) {
return hasField("@type", fields);
}
/**
* Checks if given collection of fields contain field with specified name.
*
* @param field
* to find
* @param fields
* collection of fields where search
* @return true if collection contain specified field, false otherwise.
*/
private boolean hasField(final String field, final String[] fields) {
return getFieldValue(field, fields) != null;
}
private String getFieldValue(final String field, final String[] fields) {
String doubleQuotes = "\"" + field + "\"";
String singleQuotes = "'" + field + "'";
for (int i = 0; i < fields.length; i = i + 2) {
if (fields[i].equals(doubleQuotes) || fields[i].equals(singleQuotes)) {
return fields[i + 1];
}
}
return null;
}
}