How to build and debug JSONDecoder?

I’d like to learn more about how JSONDecoder works, and investigate whether a thing I think is a bug is actually a bug, and maybe fix it if it is. I tried pulling just the JSONDecoder-related files into an Xcode project, but it was fiddly trying to get them to build, I think because I had to import Foundation, but they’re also part of Foundation?

What’s the most straightforward way that I can run JSONDecoder and attach a debugger inside Xcode? Preferably in situ, rather than extracting it into my own project. I didn’t see any docs, but maybe I was looking in the wrong place? Is such a thing even possible? (Cue History Channel aliens guy with the hair.)

If you add the import Foundation line it should work. I remember doing this in a project at some point (unfortunately that project was closed-source). I think you may have to resolve some references by adding Foundation. in front of some type names as well. If I recall correctly, it wasn't that hard, and it was less work at the time then compiling a custom Swift toolchain.

Actually, to make sure I wasn't lying to you, I just extracted the files again, you can find them here: GitHub - GeorgeLyon/ExfiltratedJSONDecoder: JSONDecoder in its own project, so you can debug it outside of apple/swift

This might actually be useful for me to prototype New {De,En}codingContainer types to support enums with associated types (and tuples)


Amazing! Is the version you exfiltrated from main? Or from a release?

FWIW, this is roughly what I used to do when originally working on the encoders and decoders — I don't know the latest recommended way to debug the stdlib and overlays, but for quick iteration, this was the simplest and easiest method. Just keep in mind that what you're effectively doing is shadowing the Foundation types with local ones, so this won't work against pre-compiled modules which were built against the Foundation types and will link against them; if you're compiling your project entirely from source, though, this should work.

(If you're trying to match behavior against a specific OS version, you're going to want to make sure you're pulling the files from the release branch matching the Swift stdlib version shipped with your OS.)

This is the version from main, but it this file doesn't change much release-to-release: History for stdlib/public/Darwin/Foundation/JSONEncoder.swift - apple/swift · GitHub

Terms of Service

Privacy Policy

Cookie Policy