簡體   English   中英

Rails 序列化 - fast_jsonapi / active_model_serializers

[英]Rails Serialization - fast_jsonapi / active_model_serializers

我有點迷失在使用 fast_jsonapi / active_model_serializers 來構建 API 的過程中。 我掌握了基礎知識,但似乎堅持使用自定義解決方案。

我有這個作為序列化程序:

class AreaSerializer
  include FastJsonapi::ObjectSerializer
  attributes :id, :name, :cost_center, :notes
  has_many :children
end

在我的區域模型中,我有:

  has_many :children, -> { Area.where(ancestry: id) }

我的控制器看起來像:

class Api::V1::AreasController < ApiController

  def index
    render json: AreaSerializer.new(Area.root).serialized_json
  end

end

區域嵌套在具有祖先寶石的層次結構中。 輸出是:

{
"data": [{
    "id": "1",
    "type": "area",
    "attributes": {
        "id": 1,
        "name": "Calgary",
        "cost_center": "123456",
        "notes": ""
    },
    "relationships": {
        "children": {
            "data": [{
                "id": "3",
                "type": "child"
            }]
        }
    }
}, {
    "id": "2",
    "type": "area",
    "attributes": {
        "id": 2,
        "name": "Edmonton",
        "cost_center": "78946",
        "notes": ""
    },
    "relationships": {
        "children": {
            "data": []
        }
    }
}]

}

我正在尋找這樣的輸出:

{
"data": [{
    "id": "1",
    "type": "area",
    "attributes": {
        "id": 1,
        "name": "Calgary",
        "cost_center": "123456",
        "notes": ""
    },
    "relationships": {
        "areas": {
            "data": [{
                "id": "3",
                "type": "area",
                "attributes": {
                    "id": 3,
                    "name": "Child Area",
                    "cost_center": "123456",
                    "notes": ""
                }
            }]
        }
    }
}, {
    "id": "2",
    "type": "area",
    "attributes": {
        "id": 2,
        "name": "Edmonton",
        "cost_center": "78946",
        "notes": ""
    }
}]

}

這個想法是嵌套關系顯示細節等。

我剛開始在我的 rails 項目中使用fast_jsonapi

fast_jsonapi遵循JSON:API 規范,你可以在這里看到。

因此,您將無法使用關系輔助函數 (:has_many, :belongs_to) 來實現您想要的輸出,即在relationships鍵內嵌套 area 的屬性。

"relationships": {
        "areas": {
            "data": [{
                "id": "3",
                "type": "area",
                "attributes": { // nesting attributes of area
                    "id": 3,
                    "name": "Child Area",
                    "cost_center": "123456",
                    "notes": ""
                }
            }]
        }
    }

JSON:API 指定:

為了減少 HTTP 請求的數量,服務器可以允許包含相關資源以及請求的主要資源的響應。 此類響應稱為“復合文檔”。

因此,相反,你將有屬性area內稱為密鑰included

為了在您的響應中獲得included鍵,您需要在控制器中向序列化程序提供一個options哈希。

我不太明白你的模型Area關系,但假設一個Area有很多SubArea

class Api::V1::AreasController < ApiController

  def index
    // serializer options
    options = {
      include: [:sub_area],
      collection: true
    }
    render json: AreaSerializer.new(Area.root, options).serialized_json
  end

end

您不能在 fast_jsonapi 中使用 Association 。 以嵌套格式獲取響應。 您需要添加方法並需要創建另一個序列化程序。

class AreaSerializer
  include FastJsonapi::ObjectSerializer

  set_type 'Area'

  attributes :id, :name, :cost_center, :notes

 attribute :childrens do |area, params|
     ChildrenSerializer.new(area.childrens, {params: 
     params})
  end
end

class ChildrenSerilizer
   include FastJsonapi::ObjectSerializer
    set_type 'Area'
    attributes :id, :name ... 
end

我開始使用上面列出的技術,但最終分叉並重新編寫了 jsonapi-serializer gem,因此它允許嵌套(最多 4 層深)並消除了具有relationshipsattributes鍵的概念。 我也很沮喪它只輸出 ID 和 TYPE 鍵,其中 TYPE 大多數時候是多余的,因為對象通常在數組中保持相同的類作為示例,人們很少使用真正的多態性(盡管它仍然支持這個並輸出 ID /type 用於多態關系)。

我重寫的最好的部分可能是它允許通過新fields輸入在 json 密鑰樹中的任何位置進行確定性字段選擇。

https://github.com/rubesMN/jsonapi-serializer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM