将JSON多路树解码为F#多路树识别联盟

时间:2017-01-05 20:56:39

标签: json f# tree multiway-tree

我在documentdb中有以下JSON数据,我想将其解析为F#多路树歧视联盟

"commentTree": {
    "commentModel": {
        "commentId": "",
        "userId": "",
        "message": ""
      },
      "forest": []
    }

F#多道歧视联盟

type public CommentMultiTreeDatabaseModel = 
| CommentDatabaseModelNode of CommentDatabaseModel * list<CommentMultiTreeDatabaseModel>

其中CommentMultiTreeDatabaseModel定义为

type public CommentDatabaseModel =
{ commentId : string
  userId : string
  message : string
}

我广泛引用Fold / Recursion over Multiway Tree in f#。我不知道从哪里开始将这样的JSON结构解析为F#多路树。任何建议将不胜感激。感谢

1 个答案:

答案 0 :(得分:3)

考虑这一点的一种方法是查看构建CommentMultiTreeDatabaseModel所需的数据。它需要CommentDatabaseModelCommentMultiTreeDatabaseModel列表。所以我们需要编写以下两个函数:

let parseComment (input : JSON) : CommentDatabaseModel =
    ...

let parseTree (input : JSON) : CommentMultiTreeDatabaseModel =
    ...

但等等,parseTree功能是我们现在正在尝试编写的功能!因此,我们不是编写新函数,而是使用rec关键字标记当前函数,并让它在需要时自行调用。

下面是一个如何完成的粗略示例。要看的关键是parseTree,它通过递归调用自身来构建数据。我用简单的DU表示了JSON输入数据。像Chiron这样的库可以产生类似的东西。

请注意,此代码一次性解析所有JSON。此外,它不是尾递归的,因此您必须小心树结构的深度。

[<RequireQualifiedAccess>]
type JSON =
    | String of string
    | Object of (string * JSON) list
    | Array of JSON list

type public CommentDatabaseModel = {
    commentId : string
    userId : string
    message : string
}

type public CommentMultiTreeDatabaseModel = 
    | CommentDatabaseModelNode of CommentDatabaseModel * list<CommentMultiTreeDatabaseModel>


let parseComment = function
    | JSON.Object [ "commentId", JSON.String commentId; "userId", JSON.String userId; "message", JSON.String message ] ->
        {
            commentId = commentId
            userId = userId
            message = message
        }
    | _ -> failwith "Bad data"

let rec parseTree (input : JSON) : CommentMultiTreeDatabaseModel =
    match input with
    | JSON.Object [ "commentModel", commentModel; "forest", JSON.Array forest ] ->
        CommentDatabaseModelNode (parseComment commentModel, List.map parseTree forest)
    | _ -> failwith "Bad data"

let parse (input : JSON) : CommentMultiTreeDatabaseModel =
    match input with
    | JSON.Object [ "commentTree", commentTree ] ->
        parseTree commentTree
    | _ -> failwith "Bad data"


let comment text =    
    JSON.Object [
        "commentId", JSON.String ""
        "userId", JSON.String ""
        "message", JSON.String text
    ]

let sampleData =
    JSON.Object [
        "commentTree", JSON.Object [
            "commentModel", comment "one"
            "forest", JSON.Array [
                JSON.Object [
                    "commentModel", comment "two"
                    "forest", JSON.Array []
                ]

                JSON.Object [
                    "commentModel", comment "three"
                    "forest", JSON.Array []
                ]
            ]
        ]
    ]

parse sampleData

(*
val it : CommentMultiTreeDatabaseModel =
  CommentDatabaseModelNode
    ({commentId = "";
      userId = "";
      message = "one";},
     [CommentDatabaseModelNode ({commentId = "";
                                 userId = "";
                                 message = "two";},[]);
      CommentDatabaseModelNode ({commentId = "";
                                 userId = "";
                                 message = "three";},[])])
*)