Created
April 23, 2020 16:15
-
-
Save xmonader/0e6c05c4e9a9c0ae49e41605ceb6f855 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "json" | |
# class Location | |
# JSON.mapping( | |
# lat: Float64, | |
# lng: Float64, | |
# ) | |
# end | |
# class House | |
# JSON.mapping( | |
# "Address": {type: String, setter: false}, | |
# location: {type: Location, nilable: true}, | |
# ) | |
# end | |
# house = House.from_json(%({"address": "Crystal Road 1234", "location": {"lat": 12.3, "lng": 34.5}})) | |
# house.address = "asasasa" | |
# house.address # => "Crystal Road 1234" | |
# house.location # => #<Location:0x10cd93d80 @lat=12.3, @lng=34.5> | |
# print(house.to_json) # = | |
chapter_j = %( | |
{ | |
"Chapter": { | |
"name": "webinterface", | |
"content": " | |
", | |
"number": null, | |
"sub_items": [], | |
"path": "index.md", | |
"parent_names": [] | |
} | |
} | |
) | |
bj = %({ | |
"sections": [ | |
{ | |
"Chapter": { | |
"name": "webinterface", | |
"content": "# Webinterface wiki\n\nThis wiki covers all of the information about \n\n- [Webinterface](webinterface/index.md)\n- [Chatbot](chatbot/index.md)\n- [wikis](wikis/index.md)\n", | |
"number": null, | |
"sub_items": [], | |
"path": "index.md", | |
"parent_names": [] | |
} | |
} | |
] | |
}) | |
class Chapter | |
include JSON::Serializable | |
property name : String | |
property content : String | |
property number : Int32 | Nil | |
property path : String | |
property parent_names : Array(String) | |
property sub_items : Array(Chapter) | |
end | |
class BookItem | |
include JSON::Serializable | |
@[JSON::Field(key: "Chapter")] | |
property chapter : Chapter | |
end | |
# "name" => @name, | |
# "content" => @content, | |
# "number" => @number, | |
# "path" => @path, | |
# "parent_names" => @parent_names, | |
# "sub_items" => @sub_items.map { |chapter| chapter.to_json(*args, **kwargs).as(Hash(String, Array(JSON::Any) }, end | |
# end | |
# end | |
# return { | |
# "Chapter" => { | |
# "name" => @name, | |
# "content" => @content, | |
# "number" => @number, | |
# "path" => @path, | |
# "parent_names" => @parent_names, | |
# "sub_items" => @sub_items.map { |chapter| chapter.to_json(*args, **kwargs).as(Hash(String, Array(JSON::Any) }, | |
# }, | |
# } | |
class Book | |
# because we cannot use upper case field names with JSON.mapping macro | |
# we will use a converter | |
include JSON::Serializable | |
property sections : Array(BookItem) | |
@[JSON::Field(key: "__non_exhaustive")] | |
property non_exhaustive : String | Nil | |
end | |
# book = Book.from_json(bj) | |
# puts book.to_json | |
puts chapter_j | |
item = BookItem.from_json(chapter_j) | |
puts item.to_json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment