Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONEncoder's dateEncodingStrategy ignored for default Date #45

Closed
Jeehut opened this issue Sep 13, 2021 · 1 comment
Closed

JSONEncoder's dateEncodingStrategy ignored for default Date #45

Jeehut opened this issue Sep 13, 2021 · 1 comment

Comments

@Jeehut
Copy link

Jeehut commented Sep 13, 2021

I'm using a JSONEncoder with dateEncodingStrategy set to .iso8601 like this:

extension JSONEncoder {
  public static var iso: JSONEncoder {
    let encoder = JSONEncoder()
    encoder.dateEncodingStrategy = .iso8601
    return encoder
  }
}

My Codable type has a discoverDate property which I specify as @DefaultCodable:

@DefaultCodable<Date.DefaultToNow>
public var discoverDate: Date

The DefaultCodableStrategy I use is defined like this (I'm using Date() as the default value):

extension Date {
  public enum DefaultToNow: DefaultCodableStrategy {
    public static var defaultValue: Date { Date() }
  }
}

But for some reason, when I encode the object using JSONEncoder.iso, it ignores the dateEncodingStrategy which I set to iso8601 but it renders like I didn't set it. For example, my JSON output looks something like this:

{ "discoverDate" : 7200 }

But I would expect it to look like this:

{ "discoverDate" : "2001-01-01T02:00:00Z" }

I believe this is a bug. Any ideas how I can work around this?

Jeehut added a commit to FlineDev/AnyLint that referenced this issue Sep 13, 2021
@Jeehut Jeehut changed the title JSONEncoder's dateEncodingStrategy gets ignored for defaulted Date type JSONEncoder's dateEncodingStrategy ignored for defaulted Date type Sep 13, 2021
@Jeehut Jeehut changed the title JSONEncoder's dateEncodingStrategy ignored for defaulted Date type JSONEncoder's dateEncodingStrategy ignored for default Date type Sep 13, 2021
@Jeehut Jeehut changed the title JSONEncoder's dateEncodingStrategy ignored for default Date type JSONEncoder's dateEncodingStrategy ignored for default Date Sep 13, 2021
@marksands
Copy link
Owner

This is fixed on master, now. 3ea38f9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants