gpt4 book ai didi

f# - 如何编译 Fable.JsonConverter

转载 作者:行者123 更新时间:2023-12-01 01:46:50 28 4
gpt4 key购买 nike

我想使用 Fable.JsonConverter。

我的测试代码(几乎复制 this)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|])

paket.dependencies文件添加了 nuget Fable.JsonConverter
source https://nuget.org/api/v2
storage:none

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

src/paket.references添加文件 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

我该怎么办?

最佳答案

看这里:http://fable.io/docs/interacting.html#json-serialization

在客户端,您应该使用 Fable.Core.JsInterop功能 toJsonofJson .
Fable.JsonConverter仅用于服务器端。它使用 Newtonsoft.Json ,这是一个不在浏览器中运行的 .NET 库。你得到的编译错误是因为 Fable 不知道如何转换 Newtonsoft.Json函数调用到 JavaScript。

当您使用一种在一个运行时(例如 .NET)中运行并编译成另一种运行时(例如 JS)的语言时,这可能会令人困惑,但您应该尝试保持一个清晰的思维模型,其中所有代码都在运行因此它可以访问什么。

关于f# - 如何编译 Fable.JsonConverter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48054112/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com