I'm implementing my own JSON encoder/decoder, à la Swift's Encoder
& Decoder
protocols.
The thing is, I only want to override a small subset of the functionality already provided by JSONEncoder
in Foundation. JSONEncoder
, contrary to its name, is not an actual Swift Encoder
. Instead, it wraps a fileprivate class _JSONEncoder
, which implements all the required Encoder
functions.
To write my own customized encoder, I could either:
- Write my own JSON encoder from scratch.
- Copy-paste and modify
_JSONEncoder
from the source itself.
Neither are particularly appealing solutions. Writing my own encoder is an arduous and daunting task, and I have no doubt that it will be riddled with bugs. Copy-pasting _JSONEncoder
, on the other hand, forces me to awkwardly check and port fixes each time there's an update to the original source file.
I'd rather just have _JSONEncoder
exposed, and wrap its Encoder
-related functions while injecting my own code as necessary.