F# 如何编译Fable.JsonConverter
我想使用Fable.JsonConverter 下面是我的测试代码(几乎是副本)F# 如何编译Fable.JsonConverter,f#,fable-f#,F#,Fable F#,我想使用Fable.JsonConverter 下面是我的测试代码(几乎是副本)FableJson.fs 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() :>
FableJson.fs
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|])
和src/paket.references
文件添加了Fable.JsonConverter
source https://nuget.org/api/v2
storage:none
clitool dotnet-fable
nuget Fable.Core
nuget Fable.Import.Browser
nuget Fable.JsonConverter
dotnet-fable
Fable.Core
Fable.Import.Browser
Fable.JsonConverter
但无法编译。
~~~ 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
我该怎么办?请参见此处:
在客户端,您应该使用json的Fable.Core.JsInterop
函数toJson
和ofJson
Fable.JsonConverter
仅用于服务器端。它使用Newtonsoft.Json
,这是一个不在浏览器中运行的.NET库。您遇到的编译错误是因为Fable不知道如何将Newtonsoft.Json
函数调用转换为JavaScript
当您使用一种语言在一个运行时(例如.NET)中运行,同时又编译成另一个运行时(例如JS)时,可能会感到困惑,但您应该尝试保持一个清晰的心智模型,在其中运行所有代码,因此它可以访问什么。接受的答案不再有效。对于较新的Fable,我唯一能找到的就是调用浏览器内置序列化程序的
Fable.Core.JS.JSON.stringify
。
另外,
Fable.Core.JSON.JSON.parse(x)
返回obj
@Maslow是正确的,在Fable 2中,我们删除了Fable.JsonConverter,支持社区创建的库
- 提供类似于Elm的体验,您可以根据自己的喜好手动或自动解码JSON。该库还提供了一条很好的错误消息
- Fable项目中用于解析和转换JSON的库
您可以使用JavaScript本机API
Fable.Core.JSON.JSON.stringify
和Fable.Core.JSON.JSON.parse(x)
,但您必须使用unbox
/cast强制执行不安全且容易破坏的数据类型。。我不能很好地阅读这份文件。初学者很难知道在哪里阅读。。。