This is a clever implementation of AnyEncodable
. Most implementations write
func encode(to encoder: Encoder) throws {
try self.value.encode(to: encoder)
}
which is incorrect as it doesn't give the encoder the opportunity to intercept value
's type in order to apply things like encoding strategies. This actually ends up giving access to the inner type and preserving it correctly.
I guess this is the first implementation I've seen that I can largely endorse as Doing The Right Thing™.