Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is SwiftyJson capable of converting a custom swift class to a json string?

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);
like image 299
Asanga Dewaguru Avatar asked Jan 14 '16 07:01

Asanga Dewaguru


1 Answers

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.

like image 138
mixel Avatar answered Nov 10 '22 05:11

mixel