15
15
*/
16
16
package com .orientechnologies .orient .core .serialization .serializer .record .string ;
17
17
18
- import java .io .IOException ;
19
- import java .io .StringWriter ;
20
- import java .text .ParseException ;
21
- import java .util .Collection ;
22
- import java .util .HashMap ;
23
- import java .util .LinkedHashMap ;
24
- import java .util .List ;
25
- import java .util .Map ;
26
- import java .util .Set ;
27
-
28
18
import com .orientechnologies .common .parser .OStringParser ;
29
19
import com .orientechnologies .orient .core .Orient ;
30
20
import com .orientechnologies .orient .core .config .OGlobalConfiguration ;
59
49
import com .orientechnologies .orient .core .util .ODateHelper ;
60
50
import com .orientechnologies .orient .core .version .ODistributedVersion ;
61
51
52
+ import java .io .IOException ;
53
+ import java .io .StringWriter ;
54
+ import java .text .ParseException ;
55
+ import java .util .Collection ;
56
+ import java .util .HashMap ;
57
+ import java .util .LinkedHashMap ;
58
+ import java .util .List ;
59
+ import java .util .Map ;
60
+ import java .util .Set ;
61
+
62
62
@ SuppressWarnings ("serial" )
63
63
public class ORecordSerializerJSON extends ORecordSerializerStringAbstract {
64
64
65
65
public static final String NAME = "json" ;
66
66
public static final ORecordSerializerJSON INSTANCE = new ORecordSerializerJSON ();
67
67
public static final String ATTRIBUTE_FIELD_TYPES = "@fieldTypes" ;
68
68
public static final char [] PARAMETER_SEPARATOR = new char [] { ':' , ',' };
69
+ public static final int INITIAL_SIZE = 5000 ;
69
70
private static final Long MAX_INT = (long ) Integer .MAX_VALUE ;
70
71
private static final Long MIN_INT = (long ) Integer .MIN_VALUE ;
71
72
private static final Double MAX_FLOAT = (double ) Float .MAX_VALUE ;
72
73
private static final Double MIN_FLOAT = (double ) Float .MIN_VALUE ;
73
74
75
+ private interface CollectionItemVisitor {
76
+ void visitItem (Object item );
77
+ }
78
+
74
79
public class FormatSettings {
75
80
public boolean includeVer ;
76
81
public boolean includeType ;
@@ -327,7 +332,7 @@ public StringBuilder toString(final ORecordInternal<?> iRecord, final StringBuil
327
332
final OUserObject2RecordHandler iObjHandler , final Set <ODocument > iMarshalledRecords , boolean iOnlyDelta ,
328
333
boolean autoDetectCollectionType ) {
329
334
try {
330
- final StringWriter buffer = new StringWriter ();
335
+ final StringWriter buffer = new StringWriter (INITIAL_SIZE );
331
336
final OJSONWriter json = new OJSONWriter (buffer , iFormat );
332
337
final FormatSettings settings = new FormatSettings (iFormat );
333
338
@@ -411,7 +416,7 @@ private Object getValue(final ODocument iRecord, String iFieldName, String iFiel
411
416
else if (OStringSerializerHelper .contains (iFieldValue , '.' )) {
412
417
// DECIMAL FORMAT: DETERMINE IF DOUBLE OR FLOAT
413
418
final Double v = new Double (OStringSerializerHelper .getStringContent (iFieldValue ));
414
-
419
+
415
420
if (canBeTrunkedToFloat (v ))
416
421
return v .floatValue ();
417
422
else
@@ -530,19 +535,20 @@ else if (OStringSerializerHelper.contains(iFieldValue, '.')) {
530
535
}
531
536
532
537
private boolean canBeTrunkedToInt (Long v ) {
533
- return (v > 0 )? v .compareTo (MAX_INT ) <= 0 : v .compareTo (MIN_INT ) >= 0 ;
538
+ return (v > 0 ) ? v .compareTo (MAX_INT ) <= 0 : v .compareTo (MIN_INT ) >= 0 ;
534
539
}
535
540
536
541
private boolean canBeTrunkedToFloat (Double v ) {
537
- //TODO not really correct check. Small numbers with high precision will be trunked while they shouldn't be
542
+ // TODO not really correct check. Small numbers with high precision will be trunked while they shouldn't be
538
543
539
- return (v > 0 )? v .compareTo (MAX_FLOAT ) <= 0 : v .compareTo (MIN_FLOAT ) >= 0 ;
544
+ return (v > 0 ) ? v .compareTo (MAX_FLOAT ) <= 0 : v .compareTo (MIN_FLOAT ) >= 0 ;
540
545
}
541
546
542
547
/**
543
548
* OBJECT OR MAP. CHECK THE TYPE ATTRIBUTE TO KNOW IT.
544
549
*/
545
- private Object getValueAsObjectOrMap (ODocument iRecord , String iFieldValue , OType iType , OType iLinkedType , Map <String , Character > iFieldTypes , boolean iNoMap , String iOptions ) {
550
+ private Object getValueAsObjectOrMap (ODocument iRecord , String iFieldValue , OType iType , OType iLinkedType ,
551
+ Map <String , Character > iFieldTypes , boolean iNoMap , String iOptions ) {
546
552
final String [] fields = OStringParser .getWords (iFieldValue .substring (1 , iFieldValue .length () - 1 ), ":," , true );
547
553
548
554
if (fields == null || fields .length == 0 )
@@ -558,10 +564,10 @@ private Object getValueAsObjectOrMap(ODocument iRecord, String iFieldValue, OTyp
558
564
}
559
565
}
560
566
561
- private Object getValueAsMap (ODocument iRecord , String iFieldValue , OType iLinkedType , Map <String , Character > iFieldTypes , boolean iNoMap , String iOptions , String [] fields ) {
567
+ private Object getValueAsMap (ODocument iRecord , String iFieldValue , OType iLinkedType , Map <String , Character > iFieldTypes ,
568
+ boolean iNoMap , String iOptions , String [] fields ) {
562
569
if (fields .length % 2 == 1 )
563
- throw new OSerializationException ("Bad JSON format on map. Expected pairs of field:value but received '"
564
- + iFieldValue + "'" );
570
+ throw new OSerializationException ("Bad JSON format on map. Expected pairs of field:value but received '" + iFieldValue + "'" );
565
571
566
572
final Map <String , Object > embeddedMap = new LinkedHashMap <String , Object >();
567
573
@@ -586,7 +592,8 @@ private Object getValueAsRecord(ODocument iRecord, String iFieldValue, OType iTy
586
592
return recordInternal ;
587
593
}
588
594
589
- private Object getValueAsCollection (ODocument iRecord , String iFieldValue , OType iType , OType iLinkedType , Map <String , Character > iFieldTypes , boolean iNoMap , String iOptions ) {
595
+ private Object getValueAsCollection (ODocument iRecord , String iFieldValue , OType iType , OType iLinkedType ,
596
+ Map <String , Character > iFieldTypes , boolean iNoMap , String iOptions ) {
590
597
// remove square brackets
591
598
iFieldValue = iFieldValue .substring (1 , iFieldValue .length () - 1 );
592
599
@@ -602,17 +609,22 @@ public void visitItem(Object item) {
602
609
603
610
return bag ;
604
611
} else if (iType == OType .LINKSET ) {
605
- return getValueAsLinkedCollection (new OMVRBTreeRIDSet (iRecord ), iRecord , iFieldValue , iType , iLinkedType , iFieldTypes , iNoMap , iOptions );
612
+ return getValueAsLinkedCollection (new OMVRBTreeRIDSet (iRecord ), iRecord , iFieldValue , iType , iLinkedType , iFieldTypes ,
613
+ iNoMap , iOptions );
606
614
} else if (iType == OType .LINKLIST ) {
607
- return getValueAsLinkedCollection (new ORecordLazyList (iRecord ), iRecord , iFieldValue , iType , iLinkedType , iFieldTypes , iNoMap , iOptions );
615
+ return getValueAsLinkedCollection (new ORecordLazyList (iRecord ), iRecord , iFieldValue , iType , iLinkedType , iFieldTypes ,
616
+ iNoMap , iOptions );
608
617
} else if (iType == OType .EMBEDDEDSET ) {
609
- return getValueAsEmbeddedCollection (new OTrackedSet <Object >(iRecord ), iRecord , iFieldValue , iType , iLinkedType , iFieldTypes , iNoMap , iOptions );
618
+ return getValueAsEmbeddedCollection (new OTrackedSet <Object >(iRecord ), iRecord , iFieldValue , iType , iLinkedType , iFieldTypes ,
619
+ iNoMap , iOptions );
610
620
} else {
611
- return getValueAsEmbeddedCollection (new OTrackedList <Object >(iRecord ), iRecord , iFieldValue , iType , iLinkedType , iFieldTypes , iNoMap , iOptions );
621
+ return getValueAsEmbeddedCollection (new OTrackedList <Object >(iRecord ), iRecord , iFieldValue , iType , iLinkedType , iFieldTypes ,
622
+ iNoMap , iOptions );
612
623
}
613
624
}
614
625
615
- private Object getValueAsLinkedCollection (final Collection <OIdentifiable > collection , ODocument iRecord , String iFieldValue , OType iType , OType iLinkedType , Map <String , Character > iFieldTypes , boolean iNoMap , String iOptions ) {
626
+ private Object getValueAsLinkedCollection (final Collection <OIdentifiable > collection , ODocument iRecord , String iFieldValue ,
627
+ OType iType , OType iLinkedType , Map <String , Character > iFieldTypes , boolean iNoMap , String iOptions ) {
616
628
617
629
parseCollection (iRecord , iFieldValue , iType , iLinkedType , iFieldTypes , iNoMap , iOptions , new CollectionItemVisitor () {
618
630
@ Override
@@ -624,7 +636,8 @@ public void visitItem(Object item) {
624
636
return collection ;
625
637
}
626
638
627
- private Object getValueAsEmbeddedCollection (final Collection <Object > collection , ODocument iRecord , String iFieldValue , OType iType , OType iLinkedType , Map <String , Character > iFieldTypes , boolean iNoMap , String iOptions ) {
639
+ private Object getValueAsEmbeddedCollection (final Collection <Object > collection , ODocument iRecord , String iFieldValue ,
640
+ OType iType , OType iLinkedType , Map <String , Character > iFieldTypes , boolean iNoMap , String iOptions ) {
628
641
629
642
parseCollection (iRecord , iFieldValue , iType , iLinkedType , iFieldTypes , iNoMap , iOptions , new CollectionItemVisitor () {
630
643
@ Override
@@ -637,16 +650,15 @@ public void visitItem(Object item) {
637
650
}
638
651
639
652
private void parseCollection (ODocument iRecord , String iFieldValue , OType iType , OType iLinkedType ,
640
- Map <String , Character > iFieldTypes , boolean iNoMap , String iOptions ,
641
- CollectionItemVisitor visitor ) {
653
+ Map <String , Character > iFieldTypes , boolean iNoMap , String iOptions , CollectionItemVisitor visitor ) {
642
654
if (!iFieldValue .isEmpty ()) {
643
655
for (String item : OStringSerializerHelper .smartSplit (iFieldValue , ',' )) {
644
656
final String itemValue = item .trim ();
645
657
646
- final Object collectionItem = getValue (iRecord , null , itemValue , OStringSerializerHelper .getStringContent (itemValue ), iLinkedType , null , iFieldTypes , iNoMap ,
647
- iOptions );
658
+ final Object collectionItem = getValue (iRecord , null , itemValue , OStringSerializerHelper .getStringContent (itemValue ),
659
+ iLinkedType , null , iFieldTypes , iNoMap , iOptions );
648
660
649
- //TODO redundant in some cases, owner is already added by getValue in some cases
661
+ // TODO redundant in some cases, owner is already added by getValue in some cases
650
662
if (shouldBeDeserializedAsEmbedded (collectionItem , iType ))
651
663
((ODocument ) collectionItem ).addOwner (iRecord );
652
664
@@ -657,10 +669,6 @@ private void parseCollection(ODocument iRecord, String iFieldValue, OType iType,
657
669
}
658
670
}
659
671
}
660
-
661
- private interface CollectionItemVisitor {
662
- void visitItem (Object item );
663
- }
664
672
665
673
private boolean shouldBeDeserializedAsEmbedded (Object record , OType iType ) {
666
674
return record instanceof ODocument && !((ODocument ) record ).getIdentity ().isTemporary ()
0 commit comments