@@ -7,7 +7,7 @@ use crate::de::Text;
77use crate :: encoding:: Decoder ;
88use crate :: errors:: serialize:: DeError ;
99use crate :: escape:: unescape;
10- use crate :: utils:: CowRef ;
10+ use crate :: utils:: { trim_xml_spaces , CowRef } ;
1111use memchr:: memchr;
1212use serde:: de:: value:: UnitDeserializer ;
1313use serde:: de:: {
@@ -25,9 +25,9 @@ macro_rules! deserialize_num {
2525 V : Visitor <' de>,
2626 {
2727 let text: & str = self . content. as_ref( ) ;
28- match text. parse( ) {
28+ match trim_xml_spaces ( text) . parse( ) {
2929 Ok ( number) => visitor. $visit( number) ,
30- Err ( _) => self . content . deserialize_str( visitor) ,
30+ Err ( _) => self . deserialize_str( visitor) ,
3131 }
3232 }
3333 } ;
@@ -146,7 +146,20 @@ impl<'de, 'a> Deserializer<'de> for AtomicDeserializer<'de, 'a> {
146146 where
147147 V : Visitor < ' de > ,
148148 {
149- self . content . deserialize_bool ( visitor)
149+ let text = self . content . as_ref ( ) ;
150+ let text = if self . escaped {
151+ unescape ( text) ?
152+ } else {
153+ Cow :: Borrowed ( text)
154+ } ;
155+ match trim_xml_spaces ( & text) {
156+ "1" | "true" => visitor. visit_bool ( true ) ,
157+ "0" | "false" => visitor. visit_bool ( false ) ,
158+ _ => match text {
159+ Cow :: Borrowed ( _) => self . content . deserialize_str ( visitor) ,
160+ Cow :: Owned ( s) => visitor. visit_string ( s) ,
161+ } ,
162+ }
150163 }
151164
152165 deserialize_num ! ( deserialize_i8 => visit_i8) ;
@@ -172,7 +185,24 @@ impl<'de, 'a> Deserializer<'de> for AtomicDeserializer<'de, 'a> {
172185 where
173186 V : Visitor < ' de > ,
174187 {
175- self . deserialize_str ( visitor)
188+ let text: & str = self . content . as_ref ( ) ;
189+ let text = if self . escaped {
190+ unescape ( text) ?
191+ } else {
192+ Cow :: Borrowed ( text)
193+ } ;
194+ let trimmed = trim_xml_spaces ( & text) ;
195+ // If string is empty or contains only XML space characters (probably only one),
196+ // deserialize as usual string and allow visitor to accept or reject it.
197+ // Otherwise trim spaces and allow visitor to accept or reject the rest.
198+ if trimmed. is_empty ( ) {
199+ match text {
200+ Cow :: Borrowed ( _) => self . content . deserialize_str ( visitor) ,
201+ Cow :: Owned ( s) => visitor. visit_string ( s) ,
202+ }
203+ } else {
204+ visitor. visit_str ( trimmed)
205+ }
176206 }
177207
178208 /// Supply to the visitor borrowed string, string slice, or owned string
0 commit comments