evermeer / EVReflection

Reflection based (Dictionary, CKRecord, NSManagedObject, Realm, JSON and XML) object mapping with extensions for Alamofire and Moya with RxSwift or ReactiveSwift
Other
965 stars 119 forks source link

Array of Dictionaries Failed to JSONSerialize #289

Closed MrZeeee closed 6 years ago

MrZeeee commented 6 years ago

i have array of EVObjects:

request.httpBody = try JSONSerialization.data(withJSONObject: x, options: [])

but this line crash with treminating with uncaught exception how can i post body of type [EVOBject]?

evermeer commented 6 years ago

If x is the array of EVObjects, then you can just use: request.httpBody = x.toJsonData()

MrZeeee commented 6 years ago

but available suggestions of xcode is toJsonString() or toDictionaryArray()

and xcode says value of type [EVObject] has no member 'toJsonData'

if i want to explain more : class MobileProduct :EVObject { public var ProductId : Int = 0 public var ServiceId : Int = 0 public var Name : String = "" public var Ownerid : Int = 0 public var CreationDate : String = "" public var PersianCreationDate : String = "" public var IsTemplate : Bool = false public var abrType : String = "" public var TagName : String = "" public var ThumbnailUrl : String = "" public var MinPage : Int = 0 public var MaxPage : Int = 0 public var MobileProductPages:[MobileProductsPage_New] = [] } and my x variable is [MobileProduct]

evermeer commented 6 years ago

Ah, you are right... I was sure it was there... I now added a helper function for this. That will be in the next version. For now you could just use:

request.httpBody = x.toJsonString().data(using: .utf8)

MrZeeee commented 6 years ago

thank you