fsprojects / FSharp.Json

F# JSON Reflection based serialization library
Apache License 2.0
222 stars 26 forks source link

Support System.Uri as a native type #8

Closed danyx23 closed 6 years ago

danyx23 commented 6 years ago

This is probably a bit of a slippery slope because it open the question of how far do you want to go with this, but I think Uris are common enough in serialization types that I think it would be nice if they were supported directly by FSharp.Json (like Guid is atm e.g.). What do you think about it? Would you be willing to accept a PR?

vsapronov commented 6 years ago

Hi Daniel! Currently you can support Uris with Type Transforms. I assume that you want to represent Uri as a string type in JSON. Then you need to implement some UriTransform for transformation: Uri <-> String. Then you will annotate Uri member with [<JsonField(Transform=typeof<UriTransform>)>] and it should just work. See example here: https://vsapronov.github.io/FSharp.Json/transform.html. I would definetly accpt such transform and include it into standard transforms here: https://github.com/vsapronov/FSharp.Json/blob/master/src/FSharp.Json/Transforms.fs

Since you have mentioned Guid I can tell you what's the bigger plan there. Current support for Guid is not flexible enough. Some needs Guid with dashes, others need it without dashes. And I obviously can't keep adding flags for different types to JsonField or JsonConfig. So the plan is to use Transforms for all types that are not supported in JSON natively: Guid and DateTime - most popular. To avoid cumbersome annotations on memebers of these types there will be global transforms setting provided through JsonConfig. This way you can say once in config that you want to use GuidTransform, DateTimeEpochTransform and (let's say) UriTransform when you are configuring the library...

danyx23 commented 6 years ago

Hey Vladimir, thanks for your quick response! Very cool, I'll send you a PR to add a standard transform as you suggest. I really like your plan to have a way to customize these other serializations nicely via a config, looking forward to that. Thanks for the very nice library btw!

vsapronov commented 6 years ago

Pull request was released in 0.3.2