使用 Elm 解析 JSON 多态记录

Parsing JSON polymorphic records with Elm

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

{
    "records": [{
        "id": 0,
        "object": {
            "id": "vertex1"
        }
    }, {
        "id": 1,
        "object": {
            "id": "vertex2"
        }

    }, {
        "id": 2,
        "object": {
            "from": "vertex1",
            "to": "vertex2"
        }
    }]
}

如你所见,它们都有id,但顶点和边有不同的记录结构。

我试图找到有关解析此类结构的内容,但我唯一找到的是 Handling records with shared substructure in Elm,但我无法将答案翻译成 Elm 0.17(将 data 简单重命名为 type 没有帮助)

一般有2个挑战:

  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
    }
  }

但编译器抱怨

Naming multiple top-level values VertexRecord makes things ambiguous.

显然 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 的字段名称。您可以简单地使用联合类型。这是一个例子。您可以通过几种不同的方式来塑造它,要理解的重要部分是将两种类型的数据作为单一记录类型进行拟合。

type Record
  = Vertex RecordID VertexContents
  | Edge RecordID EdgeContents

您可以定义一个函数,returns 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"
        }
    }]
}
"""