SAFE-BookStore icon indicating copy to clipboard operation
SAFE-BookStore copied to clipboard

Use manual encoders/decoders

Open MangelMaxime opened this issue 7 years ago • 4 comments

Asked by @forki on Gitter

I wonder if you could send a pr to bookstore that actually uses Toth decoders and removes the generic ones

Possible improvements:

  • Use an alias for Encode.toString. Like that in dev mode we have readible JSON and in production they are optimized
    module Encode =
        let inline toString (value: Value) : string =
        #if DEBUG
        Encode.toString 4 value
        #else
        Encode.toString value
        #endif
    

Because we can't set compiler directive for intellisense at least in Ionide we need to reference Thoth.Json.Net for some special place in the client. Perhaps, there is another way to do it but I don't know it. If you want to try something here is the place where I needed this:

About this limitation some people in the past mentioned that we could perhaps create a Common library to use the same "type references". for example. However, I don't know how to write such a common layer.

I decided to go fully with using manual decoders but a solution to avoid this intellisense problem could be to use AutoDecoders only for the SSR part. And use manual decoders for the rest of the Project. It can be a solution and don't impact too much the bundle size because we would need to generation the reflection data only at one place in the Client.

MangelMaxime avatar Oct 03 '18 09:10 MangelMaxime

LGTM, if this is the way that we want to bookstore to go :-) The only question is that the indentation for some |> looks a bit atypical to me - but that could just be the github diff.

isaacabraham avatar Oct 04 '18 09:10 isaacabraham

@isaacabraham Feel free to correct it (you have the right to commit to my branch as a maintainer) or make a review of with annotations I will update accordingly.

MangelMaxime avatar Oct 04 '18 10:10 MangelMaxime

And yes for F# to F# JSON deserialization/serialization it's probably not needed to use manual encoders/decoders as long as you share the same Type definition between both projects.

However, if you are interacting with an external JSON provider then using manual encoders/decoders is the way to go so you don't need to do a 1 to 1 mapping between the JSON and F# types.

For example, at work, I decode the JSON data directly into a structure that is meaningful for my Model which isn't following the JSON structure. I flatten the data structure or add "state information" like (isLoading, isEditing, etc.) which isn't part of the JSON.

I sent this PR because I was asked for and because it's important to test it to know when we should use manual VS auto decoders with SAFE.

MangelMaxime avatar Oct 04 '18 10:10 MangelMaxime

@Krzysztof-Cieslak could you please look at this an saturnize it?! that would be really great

forki avatar Oct 04 '18 13:10 forki