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

Migrating to System.Text.Json #176

Merged
merged 16 commits into from
Jan 29, 2021

Conversation

xperiandri
Copy link
Contributor

As long as I don't have .NET 4.6.1 targets installed I moved to .NET 4.8, but I suppose we can stay on .NET 4.6.1 (previously it was .NET 4.6.0)

@sergey-tihon
Copy link
Member

sergey-tihon commented Jan 16, 2021

I cannot tell (yet) if we are are ready to merge it

If you like to proceed this work:
-Let's revert .NET version to net46 or net461

  • What's the reason to use latest FSharp.Core version? can use 4.3.4? let's use lowest possible
  • We need to fix CI (revert to net461 may help to build code)
  • Remove JSON.NET related code (at least from this branch)
  • Fix tests (I bet that deserializers behave differently and hope that tests reveal the difference)

# Conflicts:
#	paket.dependencies
#	paket.lock
@sergey-tihon sergey-tihon changed the base branch from master to net5 January 17, 2021 18:19
@sergey-tihon sergey-tihon reopened this Jan 17, 2021
if isNull options then
let options = JsonSerializerOptions()
[
JsonFSharpConverter(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why here used not default converters config JsonFSharpConverter()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only exists in .NET 5 as far as I know and it is not as powerful as FSharp.SystemtextJson

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, why do you use

            let options = JsonSerializerOptions()
            [
                JsonFSharpConverter(
                    JsonUnionEncoding.InternalTag
                    ||| JsonUnionEncoding.NamedFields
                    ||| JsonUnionEncoding.UnwrapSingleCaseUnions
                    ||| JsonUnionEncoding.UnwrapRecordCases
                    ||| JsonUnionEncoding.UnwrapOption) :> JsonConverter
            ]
            |> List.iter options.Converters.Add

as a default configuration, instead of

            let options = JsonSerializerOptions()
            [
                JsonFSharpConverter() :> JsonConverter
            ]
            |> List.iter options.Converters.Add

for some reason, author of FSharp.SystemTextJson choose different default configuration for JsonFSharpConverter

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can go with the default configuration.
I don't know why he did that.
I see his defaults too verbose in JSON

nuget Newtonsoft.Json
nuget System.Net.Http.Json
nuget System.Text.Json
nuget FSharp.SystemTextJson
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need 3 explicitly referenced packages here?

 nuget System.Net.Http.Json
 nuget System.Text.Json
 nuget FSharp.SystemTextJson

can we reference only FSharp.SystemTextJson?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought that we will use JSON extensions to HttpClient and HttpResponseMessage

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose we can

BaseConstructorCall = fun args -> (baseCtor, args))
ProvidedConstructor(
[ProvidedParameter("options", typeof<JsonSerializerOptions>, optionalValue = true)],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do optional parameters not work?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optionalValue is not the the bool argument - see here

SDK expect default value for provided parameter here, you code means true :> JsonSerializerOptions.
You may try to fix default parameters, but it may be tricky with null as default value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, got it. I'll fix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try this

[
    ProvidedConstructor(
        [ProvidedParameter("httpClient", typeof<HttpClient>);
            ProvidedParameter("options", typeof<JsonSerializerOptions>, optionalValue = (null:JsonSerializerOptions))],
        invokeCode = (fun args ->
            match args with
            | [] -> failwith "Generated constructors should always pass the instance as the first argument!"
            | _ -> <@@ () @@>),
        BaseConstructorCall = fun args -> (baseCtor, args))
    ProvidedConstructor(
        [ProvidedParameter("options", typeof<JsonSerializerOptions>, optionalValue = (null:JsonSerializerOptions))],
        invokeCode = (fun args -> <@@ () @@>),
        BaseConstructorCall = fun args ->
            let httpClient = <@ RuntimeHelpers.getDefaultHttpClient defaultHost @>
            let args' = args @ [httpClient]
            (baseCtor, args'))
] |> ty.AddMembers

@xperiandri xperiandri marked this pull request as ready for review January 19, 2021 22:23
@sergey-tihon sergey-tihon reopened this Jan 20, 2021
let httpClient = <@@ RuntimeHelpers.getDefaultHttpClient defaultHost @@>
let args' = args @ [ httpClient; <@@ null @@> ]
let httpClient = <@ RuntimeHelpers.getDefaultHttpClient defaultHost @>
let args' = args @ [httpClient]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This arguments order is incorrect

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the correct one?

Copy link
Member

@sergey-tihon sergey-tihon Jan 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The one that you revered in your commit [this; httpClient; options] as base class ctor expects

8459021#diff-4b6d8e966f147a1fd45efd232f8c3d4d993b57ddb43baa435c99211e4bb3e2ddL273-L276

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still no, you build [this; options; httpClient; null] instead of [this; httpClient; options]

Copy link
Contributor Author

@xperiandri xperiandri Jan 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this?

let httpClient = <@ RuntimeHelpers.getDefaultHttpClient defaultHost @> :> Expr
let this :: tail = args
let args' = this :: httpClient :: tail

@sergey-tihon sergey-tihon changed the title WIP migrating to System.Text.Json Migrating to System.Text.Json Jan 21, 2021
@sergey-tihon sergey-tihon merged commit bdfbd94 into fsprojects:net5 Jan 29, 2021
@sergey-tihon
Copy link
Member

@xperiandri let me know about the result of your testing of NuGet package built from this branch

@sergey-tihon sergey-tihon mentioned this pull request Jan 29, 2021
4 tasks
@xperiandri
Copy link
Contributor Author

It works. Just max version FSharp.System.Text.Json is strange
image

@sergey-tihon
Copy link
Member

Basically it is the version that is used to build and test the package.
https://github.com/fsprojects/SwaggerProvider/blob/net5/src/SwaggerProvider.Runtime/paket.template#L33

What version you propose to use?

@xperiandri
Copy link
Contributor Author

xperiandri commented Jan 30, 2021

< 1.0.0 I suppose

@xperiandri
Copy link
Contributor Author

Also have a look at warnings from NuGe Package Explorer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants