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

4

9

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);
Albinus answered 14/1, 2016 at 7:10 Comment(5)
You question is misframed: SwiftlyJson is a library that reads JSON. It does not produce JSON.Unmeriting
if you are looking for functionality like GSON for java, i dont think you can do that in swiftHartzell
@Gwendal Roué: goodness.. I was totally lost. Thank you so much for the heads up, is there any other alternative in swift to do this?Albinus
@Hartzell Thanks. Yes I need a solution that works in swiftAlbinus
Possible duplicate of convert swift objects to JsonBusse
B
9

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.

Busse answered 14/1, 2016 at 7:44 Comment(4)
Thanks a lot. This explains almost all the options that I've here. I'll try to use JsonSerializerSwift.Albinus
It looks like JsonSerializerSwift has some issues with converting complex objects that contains nested objects in them. The best alternative I came up with is "EVReflection" (github.com/evermeer/EVReflection). Can convert object->json and Json->object as well. This works like a charm.Albinus
Thx! I added link to my answer.Busse
Asa: I would like to improve upon JsonSerializerSwift. Would you mind elaborating on the data structure that causes the problems? I'm on Twitter.Underdeveloped
A
3

May be this will helps: https://github.com/PonyCui/PPJSONSerialization

You use serialize to serialize PPJSONSerialization classes to JSON String or JSON Data, it's a perfect way to deliver data to server.

Appaloosa answered 14/1, 2016 at 8:4 Comment(0)
M
1

In Swift 4, there's now the Codable framework that's baked in. From my research this is the best option for those who can upgrade to the latest version of the language.

If the property names are identical it's just a few lines to call up a json decoder, and if they're different you just have to write a small switch statement to map them.

I'm sure in the coming months a library will pop up to take care of standard mapping situations but other than that things are looking pretty good out of the box.

check it here: http://benscheirman.com/2017/06/ultimate-guide-to-json-parsing-with-swift-4/

Maternal answered 19/11, 2017 at 5:44 Comment(0)
T
1

From Swift4 we can use JSONEncoder itself to convert Object model to Json String.

For that we just needs to extends the Object with Codable if the json keys are standard.

Reference :

Temblor answered 21/2, 2018 at 18:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.