mirror of
https://github.com/wahyd4/libr-ios.git
synced 2026-08-09 05:06:46 +10:00
133 lines
3.8 KiB
Swift
133 lines
3.8 KiB
Swift
//
|
|
// JsonPaser.swift
|
|
// librx
|
|
//
|
|
// Created by Yunlong Zheng on 15/9/17.
|
|
// Copyright (c) 2015年 librx. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
import SwiftyJSON
|
|
|
|
struct JsonPaser{
|
|
|
|
private static let defaultThumail = "http://7xolhk.com1.z0.glb.clouddn.com/Icon-Small@2x.png"
|
|
private static let defaultAvatar = "http://7xolhk.com1.z0.glb.clouddn.com/Icon-Small@2x.png"
|
|
|
|
static func parseProfile(json: JSON) -> Profile {
|
|
|
|
let id: Int = json["id"].int!
|
|
|
|
var avatar = defaultAvatar
|
|
if let _avatar = json["avatar"].string {
|
|
avatar = _avatar
|
|
}
|
|
|
|
let name:String = json["name"].string!
|
|
let email:String = json["email"].string!
|
|
let followed:Bool = json["followed"].bool!
|
|
let likeCount: Int = json["likes_count"].int!
|
|
let postsCount: Int = json["posts_count"].int!
|
|
let followsCount: Int = json["follows_count"].int!
|
|
let followersCount: Int = json["followers_count"].int!
|
|
|
|
return Profile(id: id, name: name, avatar: avatar, email: email, followed: followed, postsCount: postsCount, likeCount: likeCount, followsCount: followsCount, followersCount: followersCount)
|
|
|
|
}
|
|
|
|
static func paseExpolre(json: JSON) -> Explore {
|
|
|
|
let id: Int = json["id"].int!
|
|
let iconUrl:String = json["icon_url"].string!
|
|
let resourceUrl:String = json["resource_url"].string!
|
|
let name:String = json["name"].string!
|
|
let description: String = json["description"].string!
|
|
|
|
return Explore(
|
|
id: id, name: name, resourceUrl: resourceUrl,
|
|
iconUrl: iconUrl, description: description
|
|
)
|
|
|
|
}
|
|
|
|
static func parsePost(json: JSON) -> Post {
|
|
|
|
let id: Int = json["id"].int!
|
|
|
|
let processed: Bool = json["processed"].bool!
|
|
let goodFormat: Bool = json["good_format"].bool!
|
|
let viewTimes = json["viewed_times"].int
|
|
let likesCount = json["likes_count"].int
|
|
let type = json["type"].string
|
|
let created_at = json["created_at"].string
|
|
let url = json["url"].string
|
|
var title = json["title"].string
|
|
var content = json["content"].string
|
|
var summary = json["summary"].string
|
|
|
|
var liked = false
|
|
var bookmarked = false
|
|
|
|
if summary == nil {
|
|
summary = ""
|
|
}
|
|
|
|
var thumbnail = json["thumbnail"].string
|
|
|
|
if thumbnail == nil {
|
|
thumbnail = self.defaultThumail
|
|
}
|
|
|
|
if content == nil {
|
|
content = ""
|
|
}
|
|
|
|
if title == nil{
|
|
title = url
|
|
}
|
|
|
|
var shareId = json["first_sharer"]["id"].int
|
|
var shareName = json["first_sharer"]["name"].string
|
|
let shareAvatar = json["first_sharer"]["avatar"].string
|
|
|
|
if let _ = shareId {
|
|
|
|
} else {
|
|
shareId = 0
|
|
}
|
|
|
|
if let _ = shareName {
|
|
|
|
} else {
|
|
shareName = ""
|
|
}
|
|
|
|
if let _bookmarked = json["bookmarked"].bool {
|
|
bookmarked = _bookmarked
|
|
}
|
|
|
|
if let _liked = json["liked"].bool {
|
|
liked = _liked
|
|
}
|
|
|
|
return Post(
|
|
id: id,
|
|
processed: processed,
|
|
goodFormat:goodFormat,
|
|
content: content!,
|
|
viewedTimes: viewTimes!,
|
|
title: title!,
|
|
createdAt: created_at!,
|
|
type: type!,
|
|
url: url!,
|
|
thumbnail: thumbnail!,
|
|
likeCount: likesCount!,
|
|
summary: summary!,
|
|
liked: liked,
|
|
bookmarked: bookmarked,
|
|
firstSharer: FirstSharer(id: shareId!, name: shareName!, avatar: shareAvatar!)
|
|
)
|
|
|
|
}
|
|
}
|