Skip to content

Commit

Permalink
Use System.Text.Json to serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Jan 31, 2024
1 parent 3c43ec1 commit 066f2e5
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 31 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
root = true

[*.{fsproj,props}]
indent_size = 2

[*.{fs,fsi,fsx}]
fsharp_space_before_uppercase_invocation = true
fsharp_space_before_member = true
Expand Down
23 changes: 12 additions & 11 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="StreamJsonRpc" Version="2.16.36" />
<PackageVersion Include="Fable.Compiler" Version="4.0.0-alpha-004" />
<PackageVersion Include="Thoth.Json.Core" Version="0.1.0" />
<PackageVersion Include="FSharp.SystemTextJson" Version="1.2.42"/>
<PackageVersion Include="StreamJsonRpc" Version="2.16.36"/>
<PackageVersion Include="Fable.Compiler" Version="4.0.0-alpha-004"/>
<PackageVersion Include="Thoth.Json.Core" Version="0.1.0"/>
<!-- lock transitive dependencies -->
<PackageVersion Include="System.Collections" Version="4.3.0" />
<PackageVersion Include="System.Diagnostics.Debug" Version="4.3.0" />
<PackageVersion Include="System.IO.FileSystem.Primitives" Version="4.3.0" />
<PackageVersion Include="System.Runtime.Extensions" Version="4.3.1" />
<PackageVersion Include="System.Collections" Version="4.3.0"/>
<PackageVersion Include="System.Diagnostics.Debug" Version="4.3.0"/>
<PackageVersion Include="System.IO.FileSystem.Primitives" Version="4.3.0"/>
<PackageVersion Include="System.Runtime.Extensions" Version="4.3.1"/>
<!-- unit test project -->
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageVersion Include="Nerdbank.Streams" Version="2.10.72" />
<PackageVersion Include="NUnit" Version="3.14.0" />
<PackageVersion Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.1.0"/>
<PackageVersion Include="Nerdbank.Streams" Version="2.10.72"/>
<PackageVersion Include="NUnit" Version="3.14.0"/>
<PackageVersion Include="NUnit3TestAdapter" Version="4.2.1"/>
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions Fable.Daemon/Fable.Daemon.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FSharp.SystemTextJson" />
<PackageReference Include="StreamJsonRpc"/>
<PackageReference Include="Thoth.Json.Core" />
</ItemGroup>
Expand Down
21 changes: 16 additions & 5 deletions Fable.Daemon/Program.fs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
open System
open System.IO
open System.Threading.Tasks
open Fable
open Newtonsoft.Json.Serialization
open System.Text.Json
open System.Text.Json.Serialization
open StreamJsonRpc
open Fable
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.SourceCodeServices
open Fable.Compiler.ProjectCracker
Expand Down Expand Up @@ -144,11 +145,21 @@ let tryCompileFile (model : Model) (fileName : string) : Async<Result<Map<string
}

type FableServer(sender : Stream, reader : Stream) as this =
let jsonMessageFormatter = new JsonMessageFormatter ()
let jsonMessageFormatter = new SystemTextJsonFormatter ()

do
jsonMessageFormatter.JsonSerializer.ContractResolver <-
DefaultContractResolver (NamingStrategy = CamelCaseNamingStrategy ())
jsonMessageFormatter.JsonSerializerOptions <-
let options =
JsonSerializerOptions (PropertyNamingPolicy = JsonNamingPolicy.CamelCase)

let jsonFSharpOptions =
JsonFSharpOptions
.Default()
.WithUnionTagName("case")
.WithUnionFieldsName ("fields")

options.Converters.Add (JsonUnionConverter (jsonFSharpOptions))
options

let handler =
new HeaderDelimitedMessageHandler (sender, reader, jsonMessageFormatter)
Expand Down
3 changes: 2 additions & 1 deletion Fable.Daemon/Thoth/Decode.fs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module Decode =

member _.isIntegralValue jsonValue =
jsonValue.ValueKind = JsonValueKind.Number
&& not (fst (jsonValue.TryGetDouble ()))

member _.asString jsonValue = jsonValue.GetString ()
member _.asBoolean jsonValue = jsonValue.GetBoolean ()
Expand All @@ -49,7 +50,7 @@ module Decode =
member _.getProperties jsonValue =
jsonValue.EnumerateObject () |> Seq.map (fun prop -> prop.Name)

member _.getProperty (fieldName : string, jsonValue : JsonElement) = jsonValue.GetProperty (fieldName)
member _.getProperty (fieldName : string, jsonValue : JsonElement) = jsonValue.GetProperty fieldName

member _.anyToString jsonValue =
// Serializing the JsonElement to a string
Expand Down
28 changes: 14 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,18 @@ export default function fablePlugin(config = {}) {
buildStart: async function (options) {
this.info(`[buildStart] Initial compile started of ${fsproj}`);
/** @typedef {object} json
* @property {string} Case
* @property {string[]} Fields
* @property {string} case
* @property {string[]} fields
*/
const projectResponse = await getProjectFile(fsproj);
if (
projectResponse.Case === "Success" &&
projectResponse.Fields &&
projectResponse.Fields.length === 2
projectResponse.case === "Success" &&
projectResponse.fields &&
projectResponse.fields.length === 2
) {
this.info(`[buildStart] Initial compile completed of ${fsproj}`);
projectOptions = projectResponse.Fields[0];
const compiledFSharpFiles = projectResponse.Fields[1];
projectOptions = projectResponse.fields[0];
const compiledFSharpFiles = projectResponse.fields[1];
// for proj file
projectOptions.sourceFiles.forEach((file) => {
this.addWatchFile(file);
Expand Down Expand Up @@ -111,19 +111,19 @@ export default function fablePlugin(config = {}) {
this.info(`[watchChange] ${id} changed`);
try {
/** @typedef {object} json
* @property {string} Case
* @property {string[]} Fields
* @property {string} case
* @property {string[]} fields
*/
const compilationResult = await endpoint.send("fable/compile", {
fileName: id,
});
if (
compilationResult.Case === "Success" &&
compilationResult.Fields &&
compilationResult.Fields.length > 0
compilationResult.case === "Success" &&
compilationResult.fields &&
compilationResult.fields.length > 0
) {
this.info(`[watchChange] ${id} compiled`);
const compiledFSharpFiles = compilationResult.Fields[0];
const compiledFSharpFiles = compilationResult.fields[0];
const loadPromises = Object.keys(compiledFSharpFiles).map(
(fsFile) => {
compilableFiles.set(fsFile, compiledFSharpFiles[fsFile]);
Expand All @@ -135,7 +135,7 @@ export default function fablePlugin(config = {}) {
this.warn({
message: `[watchChange] compilation of ${id} failed`,
meta: {
error: compilationResult.Fields[0],
error: compilationResult.fields[0],
},
});
}
Expand Down

0 comments on commit 066f2e5

Please sign in to comment.