Attr:反序列化深层嵌套的json?

时间:2018-03-19 08:57:38

标签: python-attrs

我有一个深层嵌套的JSON结构:

  json_data =  """{
      "title: "...",
      "links": [
        {
          "href": "string",
          "method": {
            "method": "string"
          },
          "rel": "string"
        }
      ]
    }"""

我的班级:

import attr
import typing as ty
import enum
    class HttpMethod(enum.Enum):
        GET = 0
        HEAD = 1
        POST = 2
        PUT = 3
        DELETE = 4
        CONNECT = 5
        OPTIONS = 6
        TRACE = 7
        PATCH = 8


@attr.s(frozen=True, auto_attribs=True)
class LinkInfo:
    href: str = attr.ib()
    link: str = attr.ib(converter=ensure_cls)
    method: HttpMethod = attr.ib(converter=lambda x: x.name)

@attr.s(frozen=True, auto_attribs=True)
class AnalysisTemplateGetModel:
    title: x: str = attr.ib()
    links: ty.List[mlinks.LinkInfo]= attr.ib(default=attr.Factory(list))

我希望将它们反序列化为attr类,

js = json.loads(**json_data)

但是js的links字段仍然是字典,而不是LinkInfo对象?

str(js)
AnalysisTemplateGetModel(..., links=[{'href': 'string', 'method': {'method': 'string'}, 'rel': 'string'}])

0 个答案:

没有答案