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

Allow handling arbitrary Map keys as JSON object properties #162

Closed
Tarmil opened this issue Jun 16, 2023 · 1 comment · Fixed by #169
Closed

Allow handling arbitrary Map keys as JSON object properties #162

Tarmil opened this issue Jun 16, 2023 · 1 comment · Fixed by #169
Labels
enhancement New feature or request

Comments

@Tarmil
Copy link
Owner

Tarmil commented Jun 16, 2023

Since .NET 6, the handling of dictionary keys can be customized using JsonConverter.Write/ReadAsPropertyName. Our Map converter should try use these methods in the same way, and only fall back to array-of-pairs in JSON if it can't.

@Tarmil Tarmil added the enhancement New feature or request label Jun 16, 2023
@Tarmil
Copy link
Owner Author

Tarmil commented Aug 4, 2023

It's actually not really possible to choose the format based on whether the key type's converter supports {Read|Write}AsPropertyName. There isn't a flag that tells whether they're supported; just these two methods, that will either succeed or fail when called.

So a better option would be to follow what @cmeeren suggests in #152 and allow choosing the format in JsonFSharpOptions.

  • A version, corresponding to the current default, where Maps whose keys are string and single-case-union-of-string are serialized as objects, and other keys are serialized as array-of-pairs.
  • A new version where all keys are handled with {Read|Write}AsPropertyName. This will therefore always use JSON objects, but only support key types that support {Read|Write}AsPropertyName. This includes types such as int, Guid, DateTime, etc. With Allow using single-case unions as dictionary keys #161 implemented, this also includes single-case-union-of-string. In other words, the set of key types supported by this format is a superset of the key types that are serialized as JSON objects in the current version.
  • Maybe even a version using array-of-pairs regardless of the key type, because it's easy to do and may be appealing to some users for both supporting all key types and being consistent across key types.

Tarmil added a commit that referenced this issue Aug 13, 2023
[#162] Allow choosing the map format as object or array-of-pairs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant