Top-level \(T.self) encoded as number JSON fragment

I tried to directly encode an enum to json. I get the following error: “Top-level (T.self) encoded as number JSON fragment.”.
I looked up the corresponding source line in the Swift sources:

Judging from the code, it looks like it’s only possible to encode structs/dictionaries/arrays to json with the JSONEncoder. Is this true?

Would you mind to provide a small code snippet to play around with the issue?

Here you go:

enum E: Int, Codable {
	case one

do {
	try JSONEncoder().encode(
catch {
	print("\(error)") // "Top-level E encoded as number JSON fragment."

Yes, this is true for now. JSONEncoder uses JSONSerialization to serialize into JSON, and JSONSerialization requires the top-level values to not be fragments (i.e. they must be containers).

If you can, please file a bug and we can consider expanding this, either at the JSONSerialization or JSONEncoder level.


Sure, I love filing bugs, as long as it’s no black-hole radar ;-)


@itaiferber Do we have any updates on the progress of (which dupes)?

This seems like it might not be such a trivial update to .allowFragments since encoding/decoding requires the existence of containers. Though you might consider this case to use a singleValueContainer. Let's at least start discussing this again.

Yes, @bendjones has worked on this internally, though this is not aligned with Swift 5 proper. No specific promises, but we’d like to land this in the Swift 5.x timeframe.


Thanks for the update. Glad to know that it hasn't been forgotten.

Terms of Service

Privacy Policy

Cookie Policy