@@ -362,21 +362,19 @@ impl<'de, 'a> Deserializer<'de> for JSONValueDeserializer<'a> {
362
362
// in user code.
363
363
use serde:: de:: IntoDeserializer ;
364
364
match self . input {
365
- JSONValue :: Identifier ( ident) => {
365
+ JSONValue :: Identifier ( ident) | JSONValue :: SingleQuotedString ( ident ) | JSONValue :: DoubleQuotedString ( ident ) => {
366
366
// We'll treat the entire enum as a single variant with no payload:
367
367
visitor. visit_enum ( ident. into_deserializer ( ) )
368
368
}
369
369
JSONValue :: JSONObject { key_value_pairs } if !key_value_pairs. is_empty ( ) => {
370
370
// Possibly the first key is your variant, the value is the data.
371
371
// Example pattern for `Tag: { /* contents */ }` style
372
372
let kv = & key_value_pairs[ 0 ] ;
373
- if let JSONValue :: Identifier ( ref variant) = kv. key {
374
- visitor. visit_enum ( EnumDeserializer {
375
- variant,
376
- content : & kv. value ,
377
- } )
378
- } else {
379
- Err ( de:: Error :: custom ( "Invalid enum representation" ) )
373
+ match kv. key {
374
+ JSONValue :: Identifier ( variant) | JSONValue :: SingleQuotedString ( variant) | JSONValue :: DoubleQuotedString ( variant) => {
375
+ visitor. visit_enum ( EnumDeserializer { variant, content : & kv. value } )
376
+ }
377
+ _ => Err ( de:: Error :: custom ( "Invalid enum representation" ) )
380
378
}
381
379
}
382
380
_ => Err ( de:: Error :: custom ( "Unsupported enum representation" ) ) ,
@@ -488,7 +486,13 @@ impl<'de, 'a> VariantAccess<'de> for JSONValueDeserializer<'a> {
488
486
489
487
fn unit_variant ( self ) -> Result < ( ) , Self :: Error > {
490
488
// If the variant is expected to have no value, do nothing:
491
- Ok ( ( ) )
489
+ match self . input {
490
+ JSONValue :: Null => { Ok ( ( ) ) }
491
+ _ => {
492
+ Err ( de:: Error :: custom ( "Unit variants must be null" ) )
493
+ }
494
+ }
495
+
492
496
}
493
497
494
498
fn newtype_variant_seed < T > ( self , seed : T ) -> Result < T :: Value , Self :: Error >
@@ -636,7 +640,7 @@ mod test {
636
640
mod json5_compat_tests {
637
641
use super :: * ; // Bring in your `from_str` parser.
638
642
use std:: collections:: BTreeMap ; // Or HashMap, if preferred.
639
- use serde:: Deserialize ;
643
+ use serde:: { Deserialize , Serialize } ;
640
644
641
645
/// A minimal JSON5-like "value" type for testing.
642
646
/// Adjust as needed for your parser’s feature set.
@@ -1166,4 +1170,34 @@ mod json5_compat_tests {
1166
1170
_ => panic ! ( "Expected empty object after whitespace" ) ,
1167
1171
}
1168
1172
}
1173
+
1174
+ #[ test]
1175
+ fn test_gh_11 ( ) {
1176
+ use crate :: from_str;
1177
+ #[ derive( Debug , Clone , serde:: Deserialize , Serialize , PartialEq ) ]
1178
+ #[ serde( rename_all="snake_case" ) ]
1179
+ enum SomeEnum {
1180
+ VariantA ( String ) ,
1181
+ VariantB ,
1182
+ }
1183
+
1184
+ #[ derive( Debug , Clone , serde:: Deserialize , Serialize ) ]
1185
+ struct SomeStruct {
1186
+ some_enum : SomeEnum
1187
+ }
1188
+
1189
+
1190
+ let json5 = r#"{some_enum: "variant_b" }"# ;
1191
+ let parsed: SomeStruct = from_str ( json5) . unwrap ( ) ;
1192
+ assert_eq ! ( parsed. some_enum, SomeEnum :: VariantB ) ;
1193
+
1194
+ let json5 = r#"{some_enum: {"variant_b": null} }"# ;
1195
+ let parsed: SomeStruct = from_str ( json5) . unwrap ( ) ;
1196
+ assert_eq ! ( parsed. some_enum, SomeEnum :: VariantB ) ;
1197
+
1198
+
1199
+ let json5 = r#"{some_enum: { "variant_b": {} }}"# ;
1200
+ let maybe_res: Result < SomeStruct , _ > = from_str ( json5) ;
1201
+ let err = maybe_res. unwrap_err ( ) ;
1202
+ }
1169
1203
}
0 commit comments