Skip to content

Support for customizing/removing nested key for enums with associated values? #30

Description

@Sajjon

Hey! Great lib! Does it already support this use case?

struct StateHolder: Codable {
	let state: State
	enum State: Codable {
		case on(On)
		case off(Off)
		struct On: Codable {
			let onOnlyProperty: String
		}
		struct Off: Codable {
			let offOnlyProperty: String
		}
	}
}

when I JSON encode StateHolder(state: .on(.init(onOnlyProperty: "On!"))) Swift produces:

{
	"state" : {
		"on" : {
			"_0" : {
				"onOnlyProperty": "On!"
			}
		}
	}
}

Or using .off respectively:

{
	"state" : {
		"off" : {
			"_0" : {
				"offOnlyProperty": "Off!"
			}
		}
	}
}

Which is... well I think that was strange design decision by SE-0295.

I always manually change this to:

{
	"state" : {
		"discriminator": "on",
		"on" : {
			"onOnlyProperty": "On!"
		}
	}
}

Is this support by the lib? Or something you would consider adding?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions