-
Notifications
You must be signed in to change notification settings - Fork 24
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
SWIFT-1130 Suggestion for change to JSON dates for toRelaxedExtendedJSON() #73
Comments
Hi @NotesClef! Thanks for reaching out. The formatting choice here is dictated by the extended JSON specification which defines the "relaxed" date format to be identical to the canonical one. That said, I can see how the current behavior may be unexpected and more difficult to work with. Could you share a little bit more about your use case for converting Depending on how you are running into this, there may be a workaround, or we may be able to consider making the representation configurable. |
Thanks for your response! I'm converting from BSONDocument to JSON. When I supply the resulting JSON, users have indicated that they found the formatting of the dates odd compared to JSON they are used to seeing. Many times database I see will have dates as strings, so if converted directly to mongoldb as a string wouldn't be an issue. But I'm using Swift codable structs to handle things, and the dates are therefore converted to mongo db in date format. My workaround for the moment is that I've forked the project so that I could modify the behaviour of the ExtendedJSONEncoder. However, I wondered if this were behaviour that others would want for relaxed JSON. But as you mentioned, I think it would be great to have the date formatting in this instance be configurable. |
Thanks for the info! I'm going to discuss this further with my team this week. Another thing to mention that could provide an avenue for this -- we have considered in the past making In the meantime - do you convert from If so, you could use the For example, to use ISO-8601 formatted strings: struct MyStruct: Codable {
let d: Date
}
let encoder = BSONEncoder()
encoder.dateEncodingStrategy = .iso8601
let s = MyStruct(d: Date())
let doc = try encoder.encode(s)
print(doc.toExtendedJSONString()) // prints {"d":"2021-11-08T02:29:23Z"} You can also supply a |
Thank you again for your reply! I really appreciate your feedback. The suggestion of using the date encoding strategy from BSONEncoder is a good one to keep in mind, and I can use that when going from a codable struct. However, this would be non-optimal for my current use case that spurred my original request. The flow is that we go from Codable structs in Swift to generate the Mongo DB collection records. We do want to preserve the dates as Mongo date format (which allows for efficient indexes and searches by date range). It also allows for encoding back to Swift structures handling dates in a standard way (when I need to work with data in Swift). But in the case that spurred my original suggestion, I'm retrieving data from an aggregate search - so getting BSONDocuments from Mongo. I've found that encoding back into Swift structs, and then to JSON, seems substantially slower performance than using extended JSON to return results directly from the BSONDocuments. In many instances, for example when returning results to a different microservice, we don't need to encode back to Swift structs (if we don't need to manipulate the data in Swift) - but rather just want to return the results from Mongo as fast as possible. So in some cases it seems preferable to retrieve the BSONDocuments from Mongo, manipulate them directly, and return the JSON - skipping the step of encoding to a Swift struct (which appears to add some significant overhead, especially with larger and more complex structs). |
@NotesClef Thanks for the additional information! Yeah, it makes sense that going from I have one more question. Does the extended JSON format (as compared to normal JSON) provide any value for your use case? I see the options here as being:
If there is value for you in still having some types encoded as extJSON, option 2 might be better, but if "plain" JSON would suffice we could do option 1. |
For my current use case, option 1 would work well. However, I could imagine for other instances where having control over per-type configuration could be helpful (as per option 2). |
I would like to suggest a change to the date format resulting from toRelaxedExtendedJSON() from Date+BSONValue.swift
In the context of relaxed, users being provided JSON find it odd to see dates represented as objects (which is more mongo style than typical JSON users we supply JSON to are accustomed):
For relaxed formatting, it would seem preferable to return dates instead as:
When we wish to return JSON preserving mongo formatting, we still have canonical.
Would this suggestion be agreeable, or is there a reason I'm missing for the dates being formatted this way for relaxed?
Thank you for your consideration.
The text was updated successfully, but these errors were encountered: