小编Car*_*uez的帖子

使用FsPickler对可选值进行JSON序列化

是否可以使用FsPickler序列化F#中的可选值,以便:

  • 当值为Some()时,包含的值被序列化
  • 当它是None时,它根本没有被序列化?

使用以下示例代码:

type Person = { name: string; age: int option }

let data = [
  { name = "Helena"; age = Some(24) };
  { name = "Peter"; age = None }
] 

let jsonSerializer = FsPickler.CreateJsonSerializer(true, true)
let streamWriter = new StreamWriter(@"C:\output.json")
let out = jsonSerializer.SerializeSequence(streamWriter, data)
Run Code Online (Sandbox Code Playgroud)

output.json文件包含以下JSON:

[
  {
    "name": "Helena",
    "age": {
      "Some": 24
    }
  },
  {
    "name": "Peter",
    "age": null
  }
]
Run Code Online (Sandbox Code Playgroud)

但我希望JSON文件的内容看起来像这样:

[
  {
    "name": "Helena",
    "age": 24
  },
  {
    "name": …
Run Code Online (Sandbox Code Playgroud)

serialization f# json

6
推荐指数
1
解决办法
377
查看次数

Azure函数:如何将http触发器函数的查询字符串参数绑定到Cosmos DB的SQL查询

我正在尝试使用Cosmos DB输入绑定运行http触发器Azure功能.我希望http触发器的url在查询字符串上包含几个参数,这些参数绑定到输入Cosmos DB绑定的SQL查询.我正在尝试以下绑定function.json,但它不起作用(该功能甚至没有被触发):

{
  "direction": "in",
  "type": "httpTrigger",
  "authLevel": "anonymous",
  "name": "req",
  "methods": [ "get" ],
  "route": "users/{age=age?}/{gender=gender?}"
},
{
  "direction": "in",
  "type": "documentDB",
  "name": "users",
  "databaseName": "Database",
  "collectionName": "Users",
  "sqlQuery": "SELECT * FROM x where x.age = {age} and x.gender = {gender}",
  "connection": "COSMOSDB_CONNECTION_STRING"
},
Run Code Online (Sandbox Code Playgroud)

根据此答案,路由约束users/{age=age?}/{gender=gender?}对Web API有效,根据文档, 您可以将任何Web API路径约束与您的参数一起使用.最后,我想向Azure函数发出一个GET请求api/users?age=30&gender=male.那怎么办呢?

azure azure-functions azure-cosmosdb azure-functions-runtime

6
推荐指数
1
解决办法
3768
查看次数

在C#中使用Language-Ext返回的链式异步操作

我正在使用C#的Language-Ext库,我试图链接返回Either类型的异步操作.假设我有三个函数,如果它们成功则返回一个整数,如果失败则返回一个字符串,另一个函数将前三个函数的结果相加.在下面的示例实现中Op3失败并返回一个字符串.

public static async Task<Either<string, int>> Op1()
{
    return await Task.FromResult(1);
}

public static async Task<Either<string, int>> Op2()
{
    return await Task.FromResult(2);
}

public static async Task<Either<string, int>> Op3()
{
    return await Task.FromResult("error");
}

public static async Task<Either<string, int>> Calculate(int x, int y, int z)
{
    return await Task.FromResult(x + y + z);
}
Run Code Online (Sandbox Code Playgroud)

我想链接这些操作,我试图这样做:

var res = await (from x in Op1()
                 from y in Op2()
                 from z in Op3()
                 from w in …
Run Code Online (Sandbox Code Playgroud)

c# functional-programming either async-await language-ext

5
推荐指数
1
解决办法
698
查看次数

F#Data JSON类型提供程序:如何处理可以是数组或属性的JSON属性?

我正在使用F#Data库中的JSON类型提供程序从API访问JSON文档.文档包含一个属性(让我们称之为'car'),有时候是一个对象数组,有时候是一个对象.这是:

'car': [
  { ... },
  { ... }
]
Run Code Online (Sandbox Code Playgroud)

或这个:

'car': { ... }
Run Code Online (Sandbox Code Playgroud)

{ ... }在两种情况下,对象具有相同的结构.

JSON类型提供程序指示属性的类型为:

JsonProvider<"../data/sample.json">.ArrayOrCar
Run Code Online (Sandbox Code Playgroud)

sample.json我的样本文件在哪里.

我的问题是:我怎样才能弄清楚属性是一个数组(以便我可以将它作为数组处理)还是单个对象(以便我可以将它作为一个对象处理)?

更新: 简化的示例JSON将如下所示:

{
  "set": [
    {
      "car": [
        {
          "brand": "BMW" 
        },
        {
          "brand": "Audi"
        }
      ]
    },
    {
      "car": {
        "brand": "Toyota"
      }
    } 
  ]
}
Run Code Online (Sandbox Code Playgroud)

并且使用以下代码将指出类型doc.Set.[0].CarJsonProvider<...>.ArrayOrCar:

type example = JsonProvider<"sample.json">
let doc = example.GetSample()
doc.Set.[0].Car
Run Code Online (Sandbox Code Playgroud)

f# json type-providers f#-data fsharp.data.typeproviders

3
推荐指数
1
解决办法
927
查看次数

Azure函数参数中的DocumentClient绑定

我在F#中编写一个http触发器Azure函数,我想绑定一个类型的参数,DocumentClient以便更好地控制在Cosmos DB中完成的查询.这是我到目前为止:

Function.fs

namespace Functions

open System
open System.Net
open System.Net.Http
open Newtonsoft.Json
open Microsoft.Azure.Documents
open Microsoft.Azure.Documents.Client
open Microsoft.Azure.WebJobs
open Microsoft.Azure.WebJobs.Host
open Microsoft.Azure.WebJobs.Extensions
open Microsoft.Azure.WebJobs.Extensions.DocumentDB

module Function = 
  let Run(req: HttpRequestMessage, [<DocumentDB>] client: DocumentClient, log: TraceWriter) =
    log.Info(sprintf "F# HTTP trigger function processed a request.")
    req.CreateResponse(HttpStatusCode.OK)
Run Code Online (Sandbox Code Playgroud)

function.json

{
  "disabled": false,
  "scriptFile": "..\\..\\..\\build\\Debug\\Functions\\Functions.dll",
  "entryPoint": "Functions.Function.Run",
  "bindings": [
    {
      "direction": "in",
      "type": "httpTrigger",
      "authLevel": "anonymous",
      "name": "req",
      "methods": [ "get" ],
      "route": "users"
    },
    {
      "direction": "in",
      "type": "documentDB",
      "name": …
Run Code Online (Sandbox Code Playgroud)

f# azure azure-webjobssdk azure-functions azure-cosmosdb

2
推荐指数
1
解决办法
541
查看次数