0xjmp / ObjectMapper-Realm

An ObjectMapper extension that serializes arbitrary JSON into Realm's List class
Other
178 stars 43 forks source link

How can I do same with dictionary where JSON is in the form of dictionary #9

Closed santhoshs5 closed 7 years ago

santhoshs5 commented 7 years ago

This is the below code

class UserInfo:Object,Mappable { var name:String? var identifier:String? var accountType:String? var devices:[String:UserDevice]? required init?(map: Map) {

}

// Mappable func mapping(map: Map) { name <- map["username"] identifier <- map["identifier"] accountType <- map["accountType"] devices <- map["devices"] } }

class UserDevice:Object,Mappable { var deviceName:String? var deviceType:String? var deviceIdentifier:String?

required init?(map: Map) {

}

// Mappable func mapping(map: Map) { deviceName <- map["name"] deviceType <- map["type"] deviceIdentifier <- map["identifier"] } }

this is my JSON:

{ username = santhosh; "identifier" = "IDJSDJSJS"; configstatus = SET; configtype = DEFAULT; "data_center" = "evs.idrive.com"; devices = { "Ankita\LKKKK" = { identifier = LJJDFDD; name = "Ankita iPAD"; type = iPad; }; "ARUN\LKKKHJH" = { identifier = LJJDFDFFD; name = "ARUN iPhone"; type = ipHone; };

accountStatus = 2; acctype = Test;

ghost commented 7 years ago

Realm does not persist dictionary types. You will need to use a List with a custom object that contains a property for the key and another List of Objects.

santhoshs5 commented 7 years ago

I have used List and and list transform but it does not help . Could you just help me with the code for the code snippet given.

danipralea commented 7 years ago

@santhoshs5: instead of the dictionary, why don't you created another Object that follows the [String:UserDevice] data structure?

santhoshs5 commented 7 years ago

I solved it using List and transformOf