我想用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|])和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.JsonConvertersrc/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我该怎么办?
发布于 2020-03-30 13:17:26
@Maslow是对的,“寓言2”中我们删除了Fable.JsonConverter,转而支持社区创建的图书馆。
Thoth.Json与Thoth.Json.Net相辅相成,允许您在后端使用相同的API。
我认为Fable.SimpleJson也为后端提供了支持,但我不确定。
您可以使用JavaScript、本机API、Fable.Core.JS.JSON.stringify和Fable.Core.JS.JSON.parse(x),但是您必须使用unbox/cast来强制您的数据类型,这是不安全的,而且很容易破坏。
发布于 2018-01-02 10:53:30
见此处:http://fable.io/docs/interacting.html#json-serialization
在客户端,您应该使用Fable.Core.JsInterop函数toJson和ofJson。
Fable.JsonConverter只适用于服务器端.它使用Newtonsoft.Json,这是一个在浏览器中不运行的.NET库。您所得到的编译错误是因为Fable不知道如何将Newtonsoft.Json函数调用转换为JavaScript。
当您使用一种在一个运行时工作的语言(例如.NET)并且也编译成另一种语言(例如JS)时,您可能会感到困惑,但是您应该尝试保持一个清晰的心智模型,让您的所有代码都在运行,从而使它能够访问什么。
发布于 2020-03-23 01:53:43
接受的答案不再有效。对于较新的寓言,我唯一能找到的是Fable.Core.JS.JSON.stringify,它调用浏览器的内置序列化程序。也是Fable.Core.JS.JSON.parse(x),它返回obj。
https://stackoverflow.com/questions/48054112
复制相似问题