Advertisement
DRVTiny

use_json_discriminator

Apr 12th, 2022 (edited)
1,061
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.64 KB | None | 0 0
  1. require "json"
  2. # Big thanks to @hzhzhzhz1111 for this useful example!
  3. class Result
  4.   include JSON::Serializable
  5.   use_json_discriminator "type", {tenant: Tenant, dcim: DCIM}
  6.   property type : String
  7. end
  8.  
  9. class Tenant < Result  
  10.   property name : String
  11. end
  12.  
  13. class DCIM < Result
  14.   property name : String
  15. end
  16.  
  17. class Results
  18.   include JSON::Serializable
  19.   property count : Int32
  20.   property next : Int32?
  21.   property previous : Int32?
  22.   property results : Array(Result)
  23. end
  24.  
  25. j = %({"count": 32, "next": null, "previous": null, "results": [{"type": "tenant", "name": "abc"}, {"type": "dcim", "name": "abc"}]})
  26.  
  27. p Results.from_json(j)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement