What Encoder
and Decoder
are you working with here? JSONEncoder
provided by Foundation, or your own?
If this is for JSONEncoder
/JSONDecoder
, manipulating the underlying storage directly is... not supported, at best. If we ever rename the storage and container variables, your code will crash when getting decoderMirror.descendent("storage", "containers")!
A better way to do (until we add something like Unevaluated
) would be to define an enum
which can represent a JSON hierarchy and to encode and decode self through that:
enum JSON : Codable {
case null
case number(NSNumber)
case string(String)
case array([JSON])
case dictionary([String : JSON])
// define initializers taking the above and recursively converting to JSONs if necessary
// define init(from:) and encode(to:) to write to a single-value container
}
Using that you can then encode and decode any arbitrary JSON payload and get it back as a strongly-typed JSON
enum which you can inspect or vend elsewhere, without the need for accessing private implementation details.