1
+ using System ;
2
+ using System . Collections . Generic ;
3
+ using NUnit . Framework ;
4
+
5
+ namespace ServiceStack . Text . Tests . Issues
6
+ {
7
+ public class Container < TKey >
8
+ {
9
+ public IDictionary < TKey , string [ ] > Data { get ; set ; } = new Dictionary < TKey , string [ ] > ( ) ;
10
+ }
11
+
12
+ public struct CompositeKey
13
+ {
14
+ public string Name { get ; set ; }
15
+ public bool Value { get ; set ; }
16
+ public CompositeKey ( string name , bool value )
17
+ {
18
+ Name = name ;
19
+ Value = value ;
20
+ }
21
+
22
+ public CompositeKey ( string jsonKey )
23
+ {
24
+ Name = jsonKey . LeftPart ( ':' ) ;
25
+ Value = jsonKey . RightPart ( ':' ) . ConvertTo < bool > ( ) ;
26
+ }
27
+
28
+ public bool Equals ( CompositeKey other ) =>
29
+ Name == other . Name && Value == other . Value ;
30
+
31
+ public override bool Equals ( object obj ) =>
32
+ obj is CompositeKey other && Equals ( other ) ;
33
+
34
+ public override int GetHashCode ( )
35
+ {
36
+ unchecked
37
+ {
38
+ return ( ( Name != null ? Name . GetHashCode ( ) : 0 ) * 397 )
39
+ ^ Value . GetHashCode ( ) ;
40
+ }
41
+ }
42
+
43
+ public override string ToString ( ) => $ "{ Name } :{ Value } ";
44
+ }
45
+
46
+
47
+ public class CompositeKeyIssue
48
+ {
49
+ [ Test ]
50
+ public void Can_serialize_CompositeKey ( )
51
+ {
52
+ var dto = new Container < CompositeKey >
53
+ {
54
+ Data = new Dictionary < CompositeKey , string [ ] >
55
+ {
56
+ { new CompositeKey ( "abc" , false ) , new [ ] { "1" , "2" , "3" } } ,
57
+ { new CompositeKey ( "bdf" , true ) , new [ ] { "b" , "c" , "d" } } ,
58
+ { new CompositeKey ( "ceg" , false ) , new [ ] { "4" , "5" , "6" } } ,
59
+ }
60
+ } ;
61
+
62
+ var serialized = JsonSerializer . SerializeToString ( dto ) ;
63
+ var fromJson = JsonSerializer . DeserializeFromString < Container < CompositeKey > > ( serialized ) ;
64
+
65
+ Assert . That ( fromJson . Data . Count , Is . EqualTo ( dto . Data . Count ) ) ;
66
+ Assert . That ( fromJson . Data , Is . EqualTo ( dto . Data ) ) ;
67
+ }
68
+ }
69
+ }
0 commit comments