休息。比较两个JSON表示

时间:2017-11-27 18:54:06

标签: json rest hal-json

我有一个标签系统:

标记

  • ID
  • 名称
  • 儿童标签

所以它们可以嵌套(最大深度为3)

例如:

  • 食品
    • 餐厅
    • 快餐
    • 中国菜
  • 医学
    • 医院
    • 药学
  • 娱乐
    • 极端
      • 滑雪
      • 溜冰
    • 家庭

我有一些有标签的资源。

放置

  • ID
  • 名称
  • 标记

我的终点很少:

/api/tags

{
  "items": [
    {
      "_links": {
        "self": {
          "href": "/api/tags/1"
        }
      },
      "id": 1,
      "name": "Food",
      "_embedded": {
        "children": [
          {
            "_links": {
              "self": {
                "href": "/api/tags/4"
              }
            },
            "id": 4,
            "name": "Restaurant",
            "_embedded": {
              "children": []
            }
          },
          {
            "_links": {
              "self": {
                "href": "/api/tags/5"
              }
            },
            "id": 5,
            "name": "Fast food",
            "_embedded": {
              "children": []
            }
          }
        ]
      }
    },
    {
      "_links": {
        "self": {
          "href": "/api/tags/2"
        }
      },
      "id": 2,
      "name": "Medicine",
      "_embedded": {
        "children": []
      }
    },
    {
      "_links": {
        "self": {
          "href": "/api/tags/3"
        }
      },
      "id": 3,
      "name": "Entertainment",
      "_embedded": {
        "children": []
      }
    }
  ]
}

/api/place/1

{
  "id": 1,
  "name": "FooBar Arena",
  "_embedded": {
    "tags": [
      {
        "_links": {
          "self": {
            "href": "/api/tags/1"
          }
        },
        "id": 1,
        "name": "Food"
      },
      {
        "_links": {
          "self": {
            "href": "/api/tags/2"
          }
        },
        "id": 2,
        "name": "Medicine"
      }
    ]
  }
}

所以我不希望标签在被列为嵌入式资源本身时具有嵌入式资源,但是由于不包括嵌入式子级,我最终得到了具有相同SELF链接的相同数据的两种不同表示,客户端应该如何比较那些?比较SELF链接可以工作,但一个表示缺少children

1 个答案:

答案 0 :(得分:1)

我相信一个好的HAL客户端认为_embedded是表示的一部分。一个好的HAL客户端只需使用_embedded中的值来加热缓存。 _embedded中显示的项目仍应显示在_links中。

因此,只要每个资源的标记都显示在_links中,就不需要多次嵌入相同的资源。