16
16
use Symfony \Component \PropertyAccess \PropertyAccessor ;
17
17
use Symfony \Component \PropertyInfo \Extractor \PhpDocExtractor ;
18
18
use Symfony \Component \PropertyInfo \Extractor \ReflectionExtractor ;
19
+ use Symfony \Component \Serializer \Encoder \CsvEncoder ;
19
20
use Symfony \Component \Serializer \Encoder \JsonEncoder ;
21
+ use Symfony \Component \Serializer \Encoder \XmlEncoder ;
20
22
use Symfony \Component \Serializer \Exception \InvalidArgumentException ;
21
23
use Symfony \Component \Serializer \Exception \LogicException ;
22
24
use Symfony \Component \Serializer \Exception \NotNormalizableValueException ;
@@ -631,6 +633,7 @@ public function testDeserializeScalarFromJson()
631
633
$ serializer = new Serializer ([new ScalarDenormalizer ()], ['json ' => new JsonEncoder ()]);
632
634
633
635
$ this ->assertSame (42 , $ serializer ->deserialize ('42 ' , 'int ' , 'json ' ));
636
+ $ this ->assertSame (-42 , $ serializer ->deserialize ('-42 ' , 'int ' , 'json ' ));
634
637
$ this ->assertTrue ($ serializer ->deserialize ('true ' , 'bool ' , 'json ' ));
635
638
$ this ->assertSame (3.14 , $ serializer ->deserialize ('3.14 ' , 'float ' , 'json ' ));
636
639
$ this ->assertSame (3.14 , $ serializer ->deserialize ('31.4e-1 ' , 'float ' , 'json ' ));
@@ -639,6 +642,54 @@ public function testDeserializeScalarFromJson()
639
642
$ this ->assertSame ('@Ca$e% ' , $ serializer ->deserialize ('"@Ca$e%" ' , 'string ' , 'json ' ));
640
643
}
641
644
645
+ public function testDeserializeScalarFromXml ()
646
+ {
647
+ $ serializer = new Serializer ([new ScalarDenormalizer ()], ['xml ' => new XmlEncoder ()]);
648
+
649
+ $ deserialize = function ($ value , string $ type ) use ($ serializer ) {
650
+ return $ serializer ->deserialize ("<?xml version='1.0'?> \n<tag> $ value</tag> " , $ type , 'xml ' );
651
+ };
652
+
653
+ $ this ->assertSame (42 , $ deserialize ('42 ' , 'int ' ));
654
+ $ this ->assertSame (-42 , $ deserialize ('-42 ' , 'int ' ));
655
+ $ this ->assertTrue ($ deserialize ('true ' , 'bool ' ));
656
+ $ this ->assertTrue ($ deserialize ('1 ' , 'bool ' ));
657
+ $ this ->assertFalse ($ deserialize ('false ' , 'bool ' ));
658
+ $ this ->assertFalse ($ deserialize ('0 ' , 'bool ' ));
659
+ $ this ->assertSame (3.14 , $ deserialize ('3.14 ' , 'float ' ));
660
+ $ this ->assertSame (3.14 , $ deserialize ('31.4e-1 ' , 'float ' ));
661
+ $ this ->assertSame (3.0 , $ deserialize ('3 ' , 'float ' )); // '3' === json_encode(3.0)
662
+ $ this ->assertNan ($ deserialize ('NaN ' , 'float ' ));
663
+ $ this ->assertSame (\INF , $ deserialize ('INF ' , 'float ' ));
664
+ $ this ->assertSame (-\INF , $ deserialize ('-INF ' , 'float ' ));
665
+ $ this ->assertSame (' spaces ' , $ deserialize (' spaces ' , 'string ' ));
666
+ $ this ->assertSame ('@Ca$e% ' , $ deserialize ('@Ca$e% ' , 'string ' ));
667
+ }
668
+
669
+ public function testDeserializeScalarFromCsv ()
670
+ {
671
+ $ serializer = new Serializer ([new ArrayDenormalizer (), new ScalarDenormalizer ()], ['csv ' => new CsvEncoder ()]);
672
+
673
+ $ deserialize = function ($ value , string $ type ) use ($ serializer ) {
674
+ return $ serializer ->deserialize ("0 \n$ value \n" , $ type , 'csv ' , [CsvEncoder::AS_COLLECTION_KEY => false ]);
675
+ };
676
+
677
+ $ this ->assertSame (42 , $ deserialize ('42 ' , 'int[] ' )[0 ]);
678
+ $ this ->assertSame (-42 , $ deserialize ('-42 ' , 'int[] ' )[0 ]);
679
+ $ this ->assertTrue ($ deserialize ('true ' , 'bool[] ' )[0 ]);
680
+ $ this ->assertTrue ($ deserialize ('1 ' , 'bool[] ' )[0 ]);
681
+ $ this ->assertFalse ($ deserialize ('false ' , 'bool[] ' )[0 ]);
682
+ $ this ->assertFalse ($ deserialize ('0 ' , 'bool[] ' )[0 ]);
683
+ $ this ->assertSame (3.14 , $ deserialize ('3.14 ' , 'float[] ' )[0 ]);
684
+ $ this ->assertSame (3.14 , $ deserialize ('31.4e-1 ' , 'float[] ' )[0 ]);
685
+ $ this ->assertSame (3.0 , $ deserialize ('3 ' , 'float[] ' )[0 ]); // '3' === json_encode(3.0)
686
+ $ this ->assertNan ($ deserialize ('NaN ' , 'float[] ' )[0 ]);
687
+ $ this ->assertSame (\INF , $ deserialize ('INF ' , 'float[] ' )[0 ]);
688
+ $ this ->assertSame (-\INF , $ deserialize ('-INF ' , 'float[] ' )[0 ]);
689
+ $ this ->assertSame (' spaces ' , $ deserialize (' spaces ' , 'string[] ' )[0 ]);
690
+ $ this ->assertSame ('@Ca$e% ' , $ deserialize ('@Ca$e% ' , 'string[] ' )[0 ]);
691
+ }
692
+
642
693
public function testDeserializeLegacyScalarType ()
643
694
{
644
695
$ this ->expectException (NotNormalizableValueException::class);
0 commit comments