update readme
JSON model decoding based on yyjson.
We have JSON as follow:
{ "profile": { "nickname": "NIX", "username": "@nixzhu@mastodon.social", "avatar_url": "https://files.mastodon.social/accounts/avatars/109/329/064/034/222/219/original/371901c6daa01207.png" }, "toots": [ { "id": 1, "content": "Hello World!", "created_at": "1674127714" }, { "id": 2, "content": "How do you do?", "created_at": "1674127720" } ] }
And we create models conforms to AnandaModel protocol as follow:
AnandaModel
import Foundation import Ananda struct Mastodon: AnandaModel { let profile: Profile let toots: [Toot] init(json: AnandaJSON) { profile = .init(json: json.profile) toots = json.toots.array().map { .init(json: $0) } } } extension Mastodon { struct Profile: AnandaModel { let nickname: String let username: String let avatarURL: URL init(json: AnandaJSON) { username = json.username.string() nickname = json.nickname.string() avatarURL = json.avatar_url.url() } } } extension Mastodon { struct Toot: AnandaModel { let id: UInt let content: String let createdAt: Date init(json: AnandaJSON) { id = json.id.uInt() content = json.content.string() createdAt = json.created_at.date() } } }
Then, we can initialize a Mastodon as follow:
Mastodon
let model = Mastodon(jsonString: "...")
You may use Ducky Model Editor to generate AnandaModel from JSON to save your time.
©Copyright 2023 CCF 开源发展委员会 Powered by Trustie& IntelliDE 京ICP备13000930号
Ananda
JSON model decoding based on yyjson.
Example
We have JSON as follow:
And we create models conforms to
AnandaModel
protocol as follow:Then, we can initialize a
Mastodon
as follow:Tool
You may use Ducky Model Editor to generate AnandaModel from JSON to save your time.