用Elm解析JSON多态记录



这可能是初学者的问题。我有一个JSON数据格式,持有多态记录,我需要解析它。这些是图的顶点或边

{
    "records": [{
        "id": 0,
        "object": {
            "id": "vertex1"
        }
    }, {
        "id": 1,
        "object": {
            "id": "vertex2"
        }
    }, {
        "id": 2,
        "object": {
            "from": "vertex1",
            "to": "vertex2"
        }
    }]
}

你可以看到它们都有id,但是顶点和边有不同的记录结构。

我试图在解析这样的结构上找到一些东西,但我发现的唯一一件事是在Elm中处理具有共享子结构的记录,但我无法将答案翻译为Elm 0.17(简单地将data重命名为type没有帮助)

一般来说有两个挑战:

  1. 定义多态记录
  2. 将JSON动态解码为顶点或边

这是我所得到的:

type alias RecordBase =
    { id : Int
    }
type Records = List (Record RecordBase)
type Record o =
    VertexRecord o
    | EdgeRecord o
type alias VertexRecord o =
    { o | object : {
      id : Int
    }
  }
type alias EdgeRecord o =
    { o | object : {
      from : Int
      , to : Int
    }
  }

但是编译器报错了

命名多个顶级值VertexRecord使事情模糊。

显然union已经定义了VertexRecordEdgeRecord类型。

我真的不知道该如何从这里开始。

由于您在多个地方和多个类型中都有标签id,因此我认为使用类型别名和字段名来指示每个id的用途会使事情变得更清晰。

编辑2016-12-15:更新为elm-0.18

type alias RecordID = Int
type alias VertexID = String
type alias VertexContents =
  { vertexID : VertexID }
type alias EdgeContents = 
  { from : VertexID
  , to : VertexID
  }

您的Record类型实际上不需要在任何地方包含object的字段名称。您可以简单地使用联合类型。这里有一个例子。您可以用几种不同的方式来塑造它,要理解的重要部分是将两种类型的数据拟合为单个Record类型。

type Record
  = Vertex RecordID VertexContents
  | Edge RecordID EdgeContents

你可以定义一个函数,在给定顶点或边的情况下返回recordID,如下所示:

getRecordID : Record -> RecordID
getRecordID r =
  case r of
    Vertex recordID _ -> recordID
    Edge recordID _ -> recordID

现在开始解码。使用Json.Decode.andThen,您可以解码公共记录ID字段,然后将JSON传递给另一个解码器以获得其余内容:

recordDecoder : Json.Decoder Record
recordDecoder =
  Json.field "id" Json.int
    |> Json.andThen recordID ->
      Json.oneOf [ vertexDecoder recordID, edgeDecoder recordID ]
vertexDecoder : RecordID -> Json.Decoder Record
vertexDecoder recordID =
  Json.object2 Vertex
    (Json.succeed recordID)
    (Json.object1 VertexContents (Json.at ["object", "id"] Json.string))
edgeDecoder : RecordID -> Json.Decoder Record
edgeDecoder recordID =
  Json.object2 Edge
    (Json.succeed recordID)
    (Json.object2 EdgeContents
      (Json.at ["object", "from"] Json.string)
      (Json.at ["object", "to"] Json.string))
recordListDecoder : Json.Decoder (List Record)
recordListDecoder =
  Json.field "records" Json.list recordDecoder

把它们放在一起,你可以像这样解码你的例子:

import Html exposing (text)
import Json.Decode as Json
main =
  text <| toString <| Json.decodeString recordListDecoder testData
testData =
  """
{
    "records": [{
        "id": 0,
        "object": {
            "id": "vertex1"
        }
    }, {
        "id": 1,
        "object": {
            "id": "vertex2"
        }
    }, {
        "id": 2,
        "object": {
            "from": "vertex1",
            "to": "vertex2"
        }
    }]
}
"""

最新更新