@@ -363,23 +363,17 @@ function transformAtom(atom, force, options) {
363
363
objectId : atom . objectId
364
364
} ;
365
365
}
366
- if ( atom . __type == 'Date' ) {
367
- return new Date ( atom . iso ) ;
366
+ if ( DateCoder . isValidJSON ( atom ) ) {
367
+ return DateCoder . JSONToDatabase ( atom ) ;
368
368
}
369
- if ( atom . __type == 'GeoPoint' ) {
370
- if ( ! inArray && ! inObject ) {
371
- return [ atom . longitude , atom . latitude ] ;
372
- }
373
- return atom ;
369
+ if ( BytesCoder . isValidJSON ( atom ) ) {
370
+ return BytesCoder . JSONToDatabase ( atom ) ;
374
371
}
375
- if ( atom . __type == 'Bytes' ) {
376
- return new mongodb . Binary ( new Buffer ( atom . base64 , 'base64' ) ) ;
372
+ if ( GeoPointCoder . isValidJSON ( atom ) ) {
373
+ return ( inArray || inObject ? atom : GeoPointCoder . JSONToDatabase ( atom ) ) ;
377
374
}
378
- if ( atom . __type == 'File' ) {
379
- if ( ! inArray && ! inObject ) {
380
- return atom . name ;
381
- }
382
- return atom ;
375
+ if ( FileCoder . isValidJSON ( atom ) ) {
376
+ return ( inArray || inObject ? atom : FileCoder . JSONToDatabase ( atom ) ) ;
383
377
}
384
378
385
379
if ( force ) {
@@ -620,11 +614,8 @@ function untransformObject(schema, className, mongoObject) {
620
614
return Parse . _encode ( mongoObject ) ;
621
615
}
622
616
623
- if ( mongoObject instanceof mongodb . Binary ) {
624
- return {
625
- __type : 'Bytes' ,
626
- base64 : mongoObject . buffer . toString ( 'base64' )
627
- } ;
617
+ if ( BytesCoder . isValidDatabaseObject ( mongoObject ) ) {
618
+ return BytesCoder . databaseToJSON ( mongoObject ) ;
628
619
}
629
620
630
621
var restObject = untransformACL ( mongoObject ) ;
@@ -699,20 +690,14 @@ function untransformObject(schema, className, mongoObject) {
699
690
//} else if (mongoObject[key] === null) {
700
691
//break;
701
692
} else {
702
- var expected = schema . getExpectedType ( className , key ) ;
703
- if ( expected == 'file' && mongoObject [ key ] ) {
704
- restObject [ key ] = {
705
- __type : 'File' ,
706
- name : mongoObject [ key ]
707
- } ;
693
+ var expectedType = schema . getExpectedType ( className , key ) ;
694
+ var value = mongoObject [ key ] ;
695
+ if ( expectedType === 'file' && FileCoder . isValidDatabaseObject ( value ) ) {
696
+ restObject [ key ] = FileCoder . databaseToJSON ( value ) ;
708
697
break ;
709
698
}
710
- if ( expected == 'geopoint' ) {
711
- restObject [ key ] = {
712
- __type : 'GeoPoint' ,
713
- latitude : mongoObject [ key ] [ 1 ] ,
714
- longitude : mongoObject [ key ] [ 0 ]
715
- } ;
699
+ if ( expectedType === 'geopoint' && GeoPointCoder . isValidDatabaseObject ( value ) ) {
700
+ restObject [ key ] = GeoPointCoder . databaseToJSON ( value ) ;
716
701
break ;
717
702
}
718
703
}
@@ -726,6 +711,94 @@ function untransformObject(schema, className, mongoObject) {
726
711
}
727
712
}
728
713
714
+ var DateCoder = {
715
+ JSONToDatabase ( json ) {
716
+ return new Date ( json . iso ) ;
717
+ } ,
718
+
719
+ isValidJSON ( value ) {
720
+ return ( typeof value === 'object' &&
721
+ value !== null &&
722
+ value . __type === 'Date'
723
+ ) ;
724
+ }
725
+ } ;
726
+
727
+ var BytesCoder = {
728
+ databaseToJSON ( object ) {
729
+ return {
730
+ __type : 'Bytes' ,
731
+ base64 : object . buffer . toString ( 'base64' )
732
+ } ;
733
+ } ,
734
+
735
+ isValidDatabaseObject ( object ) {
736
+ return ( object instanceof mongodb . Binary ) ;
737
+ } ,
738
+
739
+ JSONToDatabase ( json ) {
740
+ return new mongodb . Binary ( new Buffer ( json . base64 , 'base64' ) ) ;
741
+ } ,
742
+
743
+ isValidJSON ( value ) {
744
+ return ( typeof value === 'object' &&
745
+ value !== null &&
746
+ value . __type === 'Bytes'
747
+ ) ;
748
+ }
749
+ } ;
750
+
751
+ var GeoPointCoder = {
752
+ databaseToJSON ( object ) {
753
+ return {
754
+ __type : 'GeoPoint' ,
755
+ latitude : object [ 1 ] ,
756
+ longitude : object [ 0 ]
757
+ }
758
+ } ,
759
+
760
+ isValidDatabaseObject ( object ) {
761
+ return ( object instanceof Array &&
762
+ object . length == 2
763
+ ) ;
764
+ } ,
765
+
766
+ JSONToDatabase ( json ) {
767
+ return [ json . longitude , json . latitude ] ;
768
+ } ,
769
+
770
+ isValidJSON ( value ) {
771
+ return ( typeof value === 'object' &&
772
+ value !== null &&
773
+ value . __type === 'GeoPoint'
774
+ ) ;
775
+ }
776
+ } ;
777
+
778
+ var FileCoder = {
779
+ databaseToJSON ( object ) {
780
+ return {
781
+ __type : 'File' ,
782
+ name : object
783
+ }
784
+ } ,
785
+
786
+ isValidDatabaseObject ( object ) {
787
+ return ( typeof object === 'string' ) ;
788
+ } ,
789
+
790
+ JSONToDatabase ( json ) {
791
+ return json . name ;
792
+ } ,
793
+
794
+ isValidJSON ( value ) {
795
+ return ( typeof value === 'object' &&
796
+ value !== null &&
797
+ value . __type === 'File'
798
+ ) ;
799
+ }
800
+ } ;
801
+
729
802
module . exports = {
730
803
transformKey : transformKey ,
731
804
transformCreate : transformCreate ,
0 commit comments