Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile Fable.JsonConverter

Tags:

f#

fable-f#

I'd like to use Fable.JsonConverter.

My test code (almost copy this) FableJson.fs is below,

module FableJson

open Newtonsoft.Json

// Always use the same instance of the converter
// as it will create a cache to improve performance
let private jsonConverter = Fable.JsonConverter() :> JsonConverter

// Serialization
let toJson value =
    JsonConvert.SerializeObject(value, [|jsonConverter|])

// Deserialization
let ofJson<'T> json =
    JsonConvert.DeserializeObject<'T>(json, [|jsonConverter|])

and paket.dependencies file added nuget Fable.JsonConverter

source https://nuget.org/api/v2
storage:none

clitool dotnet-fable
nuget Fable.Core
nuget Fable.Import.Browser
nuget Fable.JsonConverter

and src/paket.references file added Fable.JsonConverter

dotnet-fable
Fable.Core
Fable.Import.Browser
Fable.JsonConverter

But cannnot compile.

~~~ snip ~~~
ERROR in ./src/FableJson.fs
d:/SRC/Repos/Fable/testJsonConverter/src/FableJson.fs(11,4): (11,57) error FABLE: Cannot find replacement for Newtonsoft.Json.JsonConvert::SerializeObject
 @ ./src/App.fs 6:0-48
 @ ./src/testJsonConverter.fsproj
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/testJsonConverter.fsproj

ERROR in ./src/FableJson.fs
d:/SRC/Repos/Fable/testJsonConverter/src/FableJson.fs(15,4): (15,62) error FABLE: Cannot find replacement for Newtonsoft.Json.JsonConvert::DeserializeObject
 @ ./src/App.fs 6:0-48
 @ ./src/testJsonConverter.fsproj
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/testJsonConverter.fsproj

ERROR in ./src/FableJson.fs
d:/SRC/Repos/Fable/testJsonConverter/src/FableJson.fs(7,28): (7,49) error FABLE: Cannot find replacement for Fable.JsonConverter::.ctor
 @ ./src/App.fs 6:0-48
 @ ./src/testJsonConverter.fsproj
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/testJsonConverter.fsproj

What should I do?

like image 958
Kenji Tokudome Avatar asked Jan 01 '18 23:01

Kenji Tokudome


1 Answers

@Maslow is right, with Fable 2 we removed Fable.JsonConverter in favour of the library created by the community.

  • Thoth.Json offers an Elm-like experience where you can decode the JSON manually or automatically depending on your preference. This library also offers a good error message
  • Fable.SimpleJson a library for parsing and transforming JSON in Fable projects

Thoth.Json is complemented with Thoth.Json.Net to allow you to use the same API on the backend.

I think Fable.SimpleJson also offer supports for the backend, but I am not sure.

You can use the JavaScript native API Fable.Core.JS.JSON.stringify and Fable.Core.JS.JSON.parse(x) but you will have to use unbox/cast to force the type of your Data which is not safe and can break easily.

like image 117
Maxime Mangel Avatar answered Oct 01 '22 04:10

Maxime Mangel