json f# tree multiway-tree

Decodificar JSON Multiway Tree en una F#Multiway Tree Discriminated Union



multiway-tree (1)

Una forma de pensar sobre esto es mirando qué datos necesita para construir un CommentMultiTreeDatabaseModel . Necesita un CommentDatabaseModel y una lista de CommentMultiTreeDatabaseModel . Entonces, necesitamos escribir las siguientes dos funciones:

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

Pero espera, la función parseTree es la que estamos tratando de escribir ahora mismo. Entonces, en lugar de escribir una nueva función, simplemente marcamos nuestra función actual con la palabra clave rec y hacemos que se llame a sí misma cuando sea necesario.

A continuación se muestra un ejemplo aproximado de cómo se podría hacer. La clave a observar es parseTree que construye los datos recursivamente llamando a sí mismo. Representé los datos de entrada de JSON con un simple DU. Una biblioteca como Chiron puede producir algo como esto.

Tenga en cuenta que este código analiza todos los JSON de una vez. Además, no es recursivo de la cola, por lo que deberá tener cuidado con la profundidad de la estructura de su árbol.

[<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";},[])]) *)

Tengo los siguientes datos JSON en un documentdb y me gustaría analizar esto en un sindicato F # multway tree discriminated

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

F # multivisión discriminada unión

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

donde CommentMultiTreeDatabaseModel se define como

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

Me refiero a Fold / Recursion sobre Multiway Tree en f # extensamente. No estoy seguro de por dónde comenzar a analizar una estructura JSON de este tipo en un árbol multipunto F #. Cualquier sugerencia será muy apreciada. Gracias