如何在suave webpart中设置Json响应 [英] How to set a Json response in suave webpart

查看:118
本文介绍了如何在suave webpart中设置Json响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始用Suave和F#开头。
我正在尝试在我的webpart中传递一个json序列化对象来获取它。



在php中我有这个

 <?php 
header('Access-Control-Allow-Credentials:true');
header('Access-Control-Allow-Headers:Content-Type,Accept');
header('Access-Control-Allow-Methods:GET,POST,PUT,DELETE,OPTIONS');
header('Access-Control-Allow-Origin:*');
?>
{
player1Key:hdegftzj25,
gameKey:aegfhzkfszl74852
}

并且我得到了我的json对象,然后我尝试用Suave和Newtonsoft.Json做同样的事情

 输入gameCreate = {
player1Key:string
gameKey:string
}

let create = {player1Key =BadBoys2; gameKey =zLUGgtrht4456}

let json = Newtonsoft.Json.JsonConvert.SerializeObject(create)

// OK(acc |> Json.serialize |> Json .format)
让php =
请求(有趣的r - >
匹配r.queryParamplayerName与
| Choice1Of2名称 - > OK(电影|> Json) .serialize(json)|> Json.format(json))
// |> Response.response(Json.toJson(info))
// |> OK
| Choice2Of2 msg - > BAD_REQUEST msg)

让webPart =
选择[
path/> => (OKHome)
path/elm/api/create.php> => php
]

startWebServer defaultConfig webPart

所以我可以创建并序列化一个json对象,但我不知道如何在我的Web部分中将其作为http响应传递,并使用上面的代码我继续在我的let php

解决方案

看起来你已经引入了一些Json序列化库太多了 - 你似乎混合了一些Json.NET和Chiron(在本教程中使用),没有很好的效果...



让我们退后一步。 Suave带有自己的Json序列化模块,所以你可以通过使用它来获得一些工作。以下是它的外观:

 让php = 
请求(有趣的r - >
匹配r .queryParamplayerName与
| Choice1Of2名称 - >
让json:string =
创建
//这来自Suave.Json,给你一个字节数组
|> Json.toJson
//将字节数组转换为字符串
|> System.Text.Encoding.UTF8.GetString
OK json
| Choice2Of2 msg - > BAD_REQUEST msg)

现在,如果需要,可以替换 Json.toJson 使用Newtonsoft Json.NET或Chiron实现调用(但希望不是两者的混合)。只要类型一致,你应该没事。



特别是对于Chiron,你错过了要序列化的类型的 ToJson 静态成员(这个是你的教程提到的东西)。 Json.NET有一个通用的序列化函数,可以生成与记录模式相对应的json,因此开箱即可使用起来更容易一些,但如果需要,还需要更多工作来自定义输出。


i'm begining with Suave and F#. I'm trying to pass a json serialized object in my webpart to get it in my response.

In php i have this

<?php
header('Access-Control-Allow-Credentials:true');
header('Access-Control-Allow-Headers:Content-Type, Accept');
header('Access-Control-Allow-Methods:GET, POST, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Origin:*');
?>
{
 "player1Key":"hdegftzj25",
 "gameKey":"aegfhzkfszl74852"
}

and with this i get my json object, then i tried to do the same with Suave and Newtonsoft.Json

type gameCreate= {
    player1Key : string
    gameKey: string
}

let create= { player1Key = "BadBoys2"; gameKey = "zLUGgtrht4456" }

let json = Newtonsoft.Json.JsonConvert.SerializeObject(create)

//OK (acc |> Json.serialize |> Json.format )
let php =
request (fun r ->
    match r.queryParam "playerName" with
    | Choice1Of2 name ->  OK (movies |> Json.serialize(json) |> Json.format(json))
                        //|>  Response.response(Json.toJson(info))
                        //|>  OK
    | Choice2Of2 msg -> BAD_REQUEST msg)

let webPart = 
choose [
path "/" >=> (OK "Home")
path "/elm/api/create.php" >=> php
]

startWebServer defaultConfig webPart

So i can create and serialize a json object but i don't know how to pass it as http response in my web part and with the above code i keep on getting error on my expressions type in my let php

解决方案

Looks like you've pulled in a few Json serialization libraries too many - you seem to mix bits of Json.NET and Chiron (which is used in the tutorial), to no great effect...

Let's take a step back. Suave comes with its own Json serialization module, so you can get something working just by using that. Here's how it would look:

let php =
    request (fun r ->
        match r.queryParam "playerName" with
        | Choice1Of2 name ->  
            let json : string = 
                create
                // this comes from Suave.Json, gives you a byte array
                |> Json.toJson           
                // converts the byte array into a string
                |> System.Text.Encoding.UTF8.GetString
            OK json
        | Choice2Of2 msg -> BAD_REQUEST msg)

Now, if you want, you can replace the Json.toJson call with either the Newtonsoft Json.NET or Chiron implementation (but hopefully not a mix of the two). As long as the types align, you should be fine.

For Chiron in particular, you're missing the ToJson static member on the type you want to serialize (this is something that your tutorial does mention). Json.NET has a generic serialize function that produces json corresponding to the record schema, so it's a bit easier to use out of the box, but also requires more work to customize the output if needed.

这篇关于如何在suave webpart中设置Json响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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