Handling 2 keys for same value in single class - json

I have a use case in which I get score form JSON using
let score = json["score_test"].arrayValue.map {Score.decode(json: $0)}
I have to reuse this class for a response in which only key for the value changes i.e
let score = json["score"].arrayValue.map {Score.decode(json: $0)}
Is there a way to achieve this so that I get the data of Score object whether the key is score_test or score depending on the JSON using the same class?
Also I tried using nil check but since the object is initialized that is not working.
Model of Score:
class Score: Object, Decoder {
dynamic var id: String = ""
dynamic var title: String = ""
dynamic var body: String = ""
dynamic var cardOrder: Int = 0
dynamic var video: Video? = nil
override static func primaryKey() -> String? {
return "id"
}
typealias T = Score
// MARK: Decoder method
static func decode(json: JSON) -> Score {
let id = json["_id"].stringValue
let title = json["title"].stringValue
let body = json["data"].stringValue
let cardOrder = json["card_order"].intValue
var video: Video?
if (json["video"].exists()) {
video = Video.decode(json: json["video"])
}
let score = Score()
score.id = id
score.title = title
score.body = body
score.video = video
score.cardOrder = cardOrder
return score
}
}

From what I understand, score is of type [Score] so what I'd do is:
var score = json["score_test"].arrayValue.map {Score.decode(json: $0)}
if score.isEmpty {
score = json["score"].arrayValue.map {Score.decode(json: $0)}
}

Related

Updating json model and retaining/converting existing data - Swift

I have a data model which looks as such:
struct MacroLog : Codable {
var date: Date = Date()
var type: MacroType
var beforeValue: Int
var afterValue: Int
var description: String
var isOverwrite: Bool = false
func valueDifference() -> Int {
return afterValue - beforeValue
}
}
And I need to update it with adding a timestamp property as such:
struct MacroLog : Codable {
let timestamp: Date
var date: Date = Date()
var type: MacroType
var beforeValue: Int
var afterValue: Int
var description: String
var isOverwrite: Bool = false
func valueDifference() -> Int {
return afterValue - beforeValue
}
}
What would I need to do, to have my existing MacroLog entities retained or converted to the new model data? I know it stays retained so this is basically me asking how I properly manage making model changes of any kind in these situations.
I update new entries the following way:
private func updateLogs(logs: [MacroLog]) {
var logsData = Data()
do {
let encoder = JSONEncoder()
logsData = try encoder.encode(logs)
_ = (logsData as NSData).write(to: fileUrl as URL, atomically: true)
} catch {
print("failed - \(error)")
}
}
My question was solved by the answer in the comment about making the timestamp optional. That way, all my previous log entries are retained and loaded going forward.

Create collection of JSONObjects from list of Kotlin objects

I have a room database from which I can get a List<LearningEvent>, which I then have to convert into a Collection<JSONObject> and return it. How can I efficiently do that?
Here's the LearningEvent class:
#Entity(tableName = "learningEvents")
data class LearningEvent(
#ColumnInfo(name = "learningeventid")
#PrimaryKey(autoGenerate = true)
var id: Int? = null,
var sessionId: Long,
var time: Float,
var eventType: String,
var description: String,
var feedback: String
)
Here's the DAO:
#Query("SELECT * FROM learningEvents WHERE sessionId = :sessionId")
suspend fun getAllLearningEvents(sessionId: Long?): List<LearningEvent>
And here's my non-working/non-building code for the getEvents() function:
override suspend fun getEvents(): Collection<JSONObject> =
withContext(Dispatchers.IO) {
Log.d("\ngetEvents(eventType)", currentSessionId.toString())
if (currentSessionId === null) {
throw Exception("Current session Id is null; Session cannot be retrieved.")
}
var gson = Gson();
// Return JSON object collection of table rows.
var learningEvents = db.learningEventDao().getAllLearningEvents(currentSessionId);
var returnCollection = emptyList<JSONObject>();
learningEvents.forEach{element ->
var singleObject = gson.toJsonTree(element);
returnCollection += singleObject;
}
return#withContext returnCollection;
}
In my humble opinion, you can convert LearningEvent to JSONObject using .toJson. Here the sample code:
var learningEvents = db.learningEventDao().getAllLearningEvents(currentSessionId);
var returnCollection = emptyList<JSONObject>();
learningEvents.forEach{ element ->
val singleObject = gson.toJson(element);
returnCollection += JSONObject(singleObject);
}

How to Display data from JSON in Alphabetical Sections Swift?

I've been using JSONParsing to display my data when you search for a term. Now I want to list out all of those terms in an alphabetized list. But am having trouble getting the code to work correctly. I've replicated some code from someone else that was having the same problem and got that to work but I'm having trouble implementing my own code.
I currently am parsing my JSON with this code:
func parseJSONSignDictionary() {
if let url = Bundle.main.url(forResource: "csvjson", withExtension: "json") {
do {
let date = Date()
let data = try Data(contentsOf: url)
if let json = try JSONSerialization.jsonObject(with: data, options: []) as? [String:Any] {
(json["results"] as? [[String:Any]])?.forEach { j in
if let name = j["identifier"] as? String, let id = j["id"] as? Int {
let sign = Signs(name: name, number: id)
signsArray.append(sign)
}
}
}
print("Took", Date().timeIntervalSince(date))
} catch {
print(error.localizedDescription)
}
}
}
Edit to add some more code, this is my Signs class, which would replace the Restaurant Array/Class:
class Signs: NSObject, Decodable, NSCoding {
private var _signName: String!
private var _signNumber: Int!
var signName: String {
return _signName
}
var signNumber: Int {
return _signNumber
}
func encode(with aCoder: NSCoder) {
aCoder.encode(signName, forKey: "signNameKey")
}
required init?(coder aDecoder: NSCoder) {
print("Trying to turn Data into Sign")
self._signName = aDecoder.decodeObject(forKey: "signNameKey") as? String
}
init(name: String, number: Int) {
self._signName = name
self._signNumber = number
}
}
The code from another StackOverflow that I'm trying to use is from here. question:Display data from JSON in alphabetical sections in Table View in Swift
func makeDataSource(names:[String:[AnyObject]]) {
var dict = [String:[Restaurant]]()
let letters = NSCharacterSet.letters
for (_,value) in names {
//Iterating Restaurants
for resObj in value {
if let restaurantName = resObj["name"] as? String {
let restaurant = Restaurant(name: restaurantName)
var key = String(describing: restaurant.name.first!)
//To check whether key is alphabet or not
key = isKeyCharacter(key: key, letters: letters) ? key : "#"
if let keyValue = dict[key] {
//Already value exists for that key
var filtered = keyValue
filtered.append(restaurant)
//Sorting of restaurant names alphabetically
//filtered = filtered.sorted(by: {$0.0.name < $0.1.name})
dict[key] = filtered
} else {
let filtered = [restaurant]
dict[key] = filtered
}
}
}
}
//To sort the key header values
self.dataArray = Array(dict).sorted(by: { $0.0 < $1.0 })
//Logic to just shift the # category to bottom
let temp = self.dataArray[0]
self.dataArray.removeFirst()
self.dataArray.append(temp)
self.indexTitles = Array(dict.keys.sorted(by: <))
let tempIndex = self.indexTitles[0]
self.indexTitles.removeFirst()
self.indexTitles.append(tempIndex)
}
I have my own array that would replace Restaurant, called Signs.
if let restaurantName = resObj["name"] as? String {
I'm also wondering where this "name" is being pulled from? Is it the array/model which has the var name?
I'm not sure since I have a way to access the JSON data with my own function if I even need to try to use the getdata() function.
I just wanna understand what I'm missing, and how to do it on my own to get the code to work properly.

Store JSON into array

I need to store each part of the json into a data structure, maybe Array?
This is what I have when I call Firebase realtime:
ref.observeSingleEvent(of: .value, with: { (snapshot) in
for child in snapshot.children {
let snap = child as! DataSnapshot
let key = snap.key
let value = snap.value
print("######################################################\n\n")
print("key = \(key) value = \(value!)")
}
})
when printed it looks like this:
What would be the best way to accomplish this?
I need to store the title, description and postUrl
EDIT:
If I do this:
let userID = Auth.auth().currentUser?.uid
ref.child("post").child(userID!).observeSingleEvent(of: .value, with: { (snapshot) in
// Get user value
print(snapshot)
}) { (error) in
print(error.localizedDescription)
}
that would give me the snapshot id but not the values because the user is not in that DB, how can I print the snapshots without saying current user?
If you can't for some reason access the snapshot's value parameter as above, you will probably need to manually parse the value string (as it's not proper json). The code below will allow you to initialise the data structure with the value text. This assumes value is a nicely formed string as in the example below. If not you may need to play with it a bit more:
let valueString = """
{
description = dawdled
postUrl = http://darrengillman.com
timestamp = 1572735278831
title = testy
}
"""
struct DataStruct {
let decription: String
let postUrl: URL
let timestamp: Int
let test: String
init?(from value: String) {
let strings = Array(value.components(separatedBy: .newlines).dropLast().dropFirst()).map{$0.components(separatedBy: "=")}.map{$0.dropFirst().first!.trimmingCharacters(in: .whitespaces)}
guard strings.count == 4 else {return nil}
self.decription = strings[0]
self.postUrl = URL(string: strings[1])!
self.timestamp = Int(strings[2]) ?? 0
self.test = strings[3]
}
}
let myData = DataStruct(from: valueString)

Convert json response to Realm object

hi I am trying to convert json response from a web service to realm object and insert it into realm database using object mapper but returns objects with null values
Alamofire.request(url).responseJSON { response in
let products = Mapper<ProRealm>().map(JSONObject:response.result.value)
print("products",products) // object with null values
}
My ProRealm class is
class ProRealm: Object, Mappable {
dynamic var additives_count: String?
var rating: String?
var updated: Bool = false;
var name: String?
var barcode: String?
var product_key: String?
var hazard_count: String?
var state: String?
var no_of_users_rated: String?
var thumbnail: String?
var overall_rating : String?
var is_food_or_beverage : Bool = false
//Impl. of Mappable protocol
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
additives_count <- map["additives_count"]
rating <- map["rating"]
updated <- map["updated"]
name <- map["name"]
barcode <- map["barcode"]
product_key <- map["product_key"]
hazard_count <- map["hazard_count"]
state <- map["state"]
no_of_users_rated <- map["no_of_users_rated"]
thumbnail <- map["thumbnail"]
overall_rating <- map["overall_rating"]
is_food_or_beverage <- map["is_food_or_beverage"]
}
}
In Json response, the products are inside the products key. please advice how to convert it into realm object
["kind": aWareInternalAPI#productsItem, "products":(
{
"additives_count" = 3;
barcode = 12345;
"hazard_count" = 0;
"is_food_or_beverage" = 1;
name = "Water Bottle";
"no_of_users_rated" = 1;
"overall_rating" = "0.0";
"product_key" = "ahdzfmF3YXJlLWJhY2tlbmQtc3RhZ2luZ3IlCxIEVXNlchiAgICgvoOTCQwLEgdQcm9kdWN0GICAgIDE_JAKDA";
rating = 0;
state = "OCR_PROCESSING";
thumbnail = "http://lh3.googleusercontent.com/dMxwgSQB02osZJJex4S57iupaMT9tjDYZaD7mweJUjYmI1KNEcZZe1syBwrRs1GbYdZNrRUtQwRYUwXiAEscGNYH-J9f3gJOXYO1rQ=s150";
updated = 1;
},
{
"additives_count" = 0;
barcode = 53647825898248485;
"hazard_count" = 0;
"is_food_or_beverage" = 1;
"no_of_users_rated" = 0;
"overall_rating" = "0.0";
"product_key" = ahdzfmF3YXJlLWJhY2tlbmQtc3RhZ2luZ3IlCxIEVXNlchiAgICgvoOTCQwLEgdQcm9kdWN0GICAgICumYAJDA;
rating = "";
state = "OCR_PROCESSING";
thumbnail = "http://lh3.googleusercontent.com/0D55ZXkG8Ua5ULDK69Po-IHeDPIfXZHOi7LlLURoc1qZzmNst57xUMQSPzWTW5miSDglc5wKDA4QlvLvnD6aMOqIHcwlj_HY-Hs=s150";
updated = 1;
})]
Please advice
All your properties except updated and is_food_or_beverage are nilable, so there is a possibility that they might be nil. If you know that they should have a value and you´re still getting nil make sure to check the keys in your mapping function.
Update:
I´m not 100% sure of how this works with the syntax with Mappable, but you need to do it something like this:
class Products: Object, Mappable {
let products: [Product]?
//Impl. of Mappable protocol
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
products <- map["products"]
}
}
class Product: Object, Mappable {
dynamic var additives_count: String?
var rating: String?
var updated: Bool = false;
var name: String?
var barcode: String?
var product_key: String?
var hazard_count: String?
var state: String?
var no_of_users_rated: String?
var thumbnail: String?
var overall_rating : String?
var is_food_or_beverage : Bool = false
//Impl. of Mappable protocol
required convenience init?(map: Map) {
self.init()
}
func mapping(map: Map) {
additives_count <- map["additives_count"]
rating <- map["rating"]
updated <- map["updated"]
name <- map["name"]
barcode <- map["barcode"]
product_key <- map["product_key"]
hazard_count <- map["hazard_count"]
state <- map["state"]
no_of_users_rated <- map["no_of_users_rated"]
thumbnail <- map["thumbnail"]
overall_rating <- map["overall_rating"]
is_food_or_beverage <- map["is_food_or_beverage"]
}
}
So create two different classes one that holds an array of products and the other is a Product.