使用JSONProvider重用类型定义吗? [英] Reusing type definitions with JSONProvider?

查看:80
本文介绍了使用JSONProvider重用类型定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FSharp-Data中的JSONProvider为我正在使用的来自服务的示例响应所使用的Web服务自动创建类型.

I'm using the JSONProvider from FSharp-Data to automatically create types for a webservice that I'm consuming using sample responses from the service.

但是,对于在服务中重用的类型,我有点困惑,例如,有一个api方法返回一个X类型的项,而另一个返回X列表,依此类推.我是否真的必须为此生成多个定义,并且这是否意味着我将为同一件事创建重复的类型?

However I'm a bit confused when it comes to types that are reused in the service, like for example there is one api method that return a single item of type X while another returns a list of X and so on. Do I really have to generate multiple definitions for this, and won't that mean that I will have duplicate types for the same thing?

所以,我想我真正要问的是,有没有一种方法可以根据JSON样本生成的类型来创建复合类型?

So, I guess what I'm really asking, is there a way to create composite types from types generated from JSON samples?

推荐答案

如果使用单独的示例分别调用JsonProvider,则对于示例中的相同内容,您将获得重复的类型.不幸的是,F#数据库对此无能为力.

If you call JsonProvider separately with separate samples, then you will get duplicate types for the same things in the sample. Sadly, there is not much that the F# Data library can do about this.

您可以使用的一个选择是同时将多个样本传递给JsonProvider(使用SampleIsList参数).在这种情况下,它会尝试为您提供的所有样本查找一种类型-但也会在所有样本中共享具有相同结构的类型.

One option that you have would be to pass multiple samples to the JsonProvider at the same time (using the SampleIsList parameters). In that case, it tries to find one type for all the samples you provide - but it will also share types with the same structure among all the samples.

我假设您不想为所有样本获取一种类型-在这种情况下,您可以使用其他JSON对象包装单个样本(此处,实际样本是嵌套在一个"和两个"):

I assume you do not want to get one type for all your samples - in that case, you can wrap the individual samples with additional JSON object like this (here, the real samples are the records nested under "one" and "two"):

type J = JsonProvider<"""
  [ { "one": { "person": {"name": "Tomas"} } },
    { "two": { "num": 42, "other":  {"name": "Tomas"} } } ]""", SampleIsList=true>

现在,您可以运行Parse方法,并根据要处理的样本,使用一个"或两个"将样本包装在新的JSON对象中.

Now, you can run the Parse method and wrap the samples in a new JSON object using "one" or "two", depending on which sample you are processing:

let j1 = """{ "person": {"name": "Tomas"} }"""
let o1 = J.Parse("""{"one":""" + j1 + "}").One.Value

let j2 = """{ "num": 42, "other": {"name": "Tomas"} }"""
let o2 = J.Parse("""{"two":""" + j2 + "}").Two.Value

一个"和两个"记录是完全任意的(我刚刚将它们添加为具有两个单独的名称).我们先解析JSON,然后再使用OneTwo属性对其进行访问.但是,这意味着o1.Persono2.Other现在具有相同的类型:

The "one" and "two" records are completely arbitrary (I just added them to have two separate names). We wrap the JSON before parsing it and then we access it using the One or Two property. However, it means that o1.Person and o2.Other are now of the same type:

o1.Person = o2.Other

这将返回false,因为我们没有在F#Data中的JSON值上实现相等性,但是它进行类型检查-因此类型相同.

This returns false because we do not implement equality on JSON values in F# Data, but it type checks - so the types are the same.

这相当复杂,因此我可能会寻找其他方法来完成您需要的工作-但这是在多个JSON样本之间获取共享类型的一种方法.

This is fairly complicated, so I would probably look for other ways of doing what you need - but it is one way to get shared types among multiple JSON samples.

这篇关于使用JSONProvider重用类型定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆