Given below is my custom swift class. My Question is how to convert an object of this class to a json string using SwiftyJson?
class Equipment{
var UniqueItemId:String? = ""
var ItemNo:String? = ""
var EquipmentType:String? = ""
var EquipmentDescription:String? = ""
var Length:String? = ""
var Wll:String? = ""
var EquipmentLocation:String? = ""
var EquipmentManufacture:String? = ""
var SerialNo:String? = ""
var Condition:String? = ""
var Remarks:String? = ""
var InspectionDate:String? = ""
var Inspector:String? = ""
}
For example, like this:
var jsonString = JSON(equipmentObject);
UPDATE
OP is now happy with EVReflection. So I assume that at this moment it's the best choice.
ORIGINAL ANSWER
Since Swift reflection possibilities is not yet such rich there is no ultimate solution now like google-gson for Java.
Libraries like SwiftJSON and Swift ObjectMapper are just sugared NSJSONSerialization
and require you to define mappings from json fields to object properties.
But things are changing and you can try out JsonSerializerSwift. I have not yet used it but it use Swift reflection and seems to work fine.
Also check out Swift Mirrors and JSON by Chris Eidhof article. It explains how JSON serialization using reflection possibilities works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With