JSON Deserialization

beginners

#1

What is the best way to deal with deserializing JSON? For example, if the JSON you are trying to deserialize has a bunch of nested properties, modelling it with Types seems really, really verbose and cumbersome. Is there a better way?

How about some sort of dynamic (gasp) type in F#? Also, to complicate things, I am using .NET Core and FSharp.Data doesn’t work yet, so no JSON Type Converter. Which would be perfect!

So… thoughts? Suggestions? Thanks!


#2

Take a look at Thoth.Json: https://mangelmaxime.github.io/Thoth/json/decode.html. It works with .NET Core, just use Thoth.Json.Net package. I used it few days ago in core project.

Side note: FSharp.Data should work with core if you use prerelease version (nuget Fsharp.Data prerelease in paket.dependencies).


#3

I think FSharp.Data works with .NET Core (I’m using it right now), but it’s in beta. If you’re using Paket, you’ll have to pin it to the pre-release beta version (https://www.nuget.org/packages/FSharp.Data/3.0.0-beta3).

I don’t know that there’s a “best” way…Json.NET suits my needs about 90% of the time. Sure, it struggles a bit with more complicated F# types (something like Map<(RandomRecord1 * RandomRecord2), int>), but you can either have a function that converts your domain type into a less complicated “POFO” that Json.NET can handle or write a custom JsonConverter.


#4

I think FSharp.Json is good. Today I just used it for my own stuff, and it works fine so far.


#5

FSharp.Json looks great


#6

I like Thoth.Json. It can be used for both .NET and Fable code.
It’s a bit more work to define decoders and encoders for your types but you get type safety.


#7

You should really have a look at Fleece since AFAIK provides all the functionality of the above mentioned libs plus the possibility to use overloading (for implicit codecs), a single synchronized method for serialization/deserialization and choice of FSharp.Data, Newtonsoft or System.Json.


#8

I didn’t realize Fleece was still around. The link appears broken, btw.


#9

Link updated, thanks !