@@ -41,18 +41,7 @@ void IDictionary.Add(object key, object? value)
4141 var valStr = value as RichString ??
4242 throw new ArgumentException ( $ "unable to convert value { value ? . GetType ( ) . Name ?? "null" } to RichString",
4343 nameof ( value ) ) ;
44- if ( key is WritingSystemId keyWs )
45- {
46- Add ( keyWs , valStr ) ;
47- }
48- else if ( key is string keyStr )
49- {
50- Add ( keyStr , valStr ) ;
51- }
52- else
53- {
54- throw new ArgumentException ( "unable to convert key to writing system id" , nameof ( key ) ) ;
55- }
44+ Add ( WritingSystemId . FromUnknown ( key ) , valStr ) ;
5645 }
5746
5847 public void Add ( WritingSystemId key , RichString value )
@@ -152,28 +141,22 @@ void IDictionary.Remove(object key)
152141
153142 object ? IDictionary . this [ object key ]
154143 {
155- get =>
156- key switch
157- {
158- WritingSystemId keyWs => this [ keyWs ] ,
159- string keyStr => this [ keyStr ] ,
160- _ => throw new ArgumentException ( "unable to convert key to writing system id" , nameof ( key ) )
161- } ;
144+ get => this [ WritingSystemId . FromUnknown ( key ) ] ;
162145 set
163146 {
164- var valStr = value as RichString ??
165- throw new ArgumentException ( "unable to convert value to string" , nameof ( value ) ) ;
166- if ( key is WritingSystemId keyWs )
167- {
168- this [ keyWs ] = valStr ;
169- }
170- else if ( key is string keyStr )
147+ // value will be null if an empty string was deserialized as a RichString (e.g. from a JsonPatch operation).
148+ // Usually that's what we want, because when deserializing a whole RichMultiString it will result in the key being dropped.
149+ // So we mimic that behaviour.
150+ var wsId = WritingSystemId . FromUnknown ( key ) ;
151+ if ( value is null )
171152 {
172- this [ keyStr ] = valStr ;
153+ Remove ( wsId ) ;
173154 }
174155 else
175156 {
176- throw new ArgumentException ( "unable to convert key to writing system id" , nameof ( key ) ) ;
157+ var valStr = value as RichString ??
158+ throw new ArgumentException ( "unable to convert value to string" , nameof ( value ) ) ;
159+ this [ wsId ] = valStr ;
177160 }
178161 }
179162 }
0 commit comments