An easy way to create parameters of request.
- iOS 8.0+
- Swift 5.1+
- Xcode 11.0+
Installation with CocoaPods:
pod 'XYJSON'
Installation with Carthage:
github "RayJiang16/XYJSON"
Struct or Class conform to JSONParameters, you can use requestParameters for free.
structEmployee:JSONParameters{varname:Stringvarage:Int}letemployee=Employee(name:"Tom", age:21)print(employee.requestParameters)
// {"name":"Tom", "age":21}The property name in your model (name in this case), might not same name in request. You can use @JSONProperty to rename that.
structEmployee:JSONParameters{@JSONProperty(name:"employee_name")varname:Stringvarage:Int}letemployee=Employee(name:"Tom", age:21)print(employee.requestParameters)
// {"employee_name":"Tom", "age":21}There has an other situation is some API might use int property to represent bool in the request.
eg. {"is_vip":0} or {"is_vip":1}
In the code, you should use bool instead of int. You can use convert to resolve the problem.
structTestConvert:JSONParameters{@JSONProperty(convert: convertIntToString)varid:Int@JSONProperty(name:"is_vip", convert: convertBoolToInt)varisVip:Bool@JSONProperty(convert:{ obj inreturn obj ?"Yes":"No"})varcustom:Bool}letobj=TestConvert(id:233, isVip:true, custom:false)print(obj.requestParameters)
// {"id": "233", "is_vip": 1, "custom": "No"}convert is a closure: (T) -> Any. T is type of property. XYJSON offers some common convert.
- convertBoolToInt
- convertBoolToIntString
- convertIntToString
- convertStringToInt
- convertDoubleToString
You can use @JSONIgnore to ignore the property that you don't want it in request parameters.
structEmployee:JSONParameters{@JSONProperty(name:"employee_name")varname:Stringvarage:Int@JSONIgnore()varother:String=""}letemployee=Employee(name:"Tom", age:21)print(employee.requestParameters)
// {"employee_name":"Tom", "age":21}If you want to use enum in your model, you need to make the enum conform to JSONValue.
enumSex:Int,JSONValue{case girl =0case boy
}structEmployee:JSONParameters{varname:Stringvarsex:Sex}letemployee=Employee(name:"Tom", sex:.boy)print(employee.requestParameters)
// {"name":"Tom", "sex":1}if you want to use another struct or class in your model, you need to make the model conform to JSONValue and implement var jsonValue: JSONValue.
enumSex:Int,JSONValue{case girl =0case boy
}structEmployee:JSONParameters{varname:Stringvarsex:Sexvartest:Test=Test()}structTest:JSONValue,JSONParameters{vart1:Int=1vart2:String="t2"varjsonValue:JSONValue{
// You can return Int, Double, Array, Dictionary...
return requestParameters
}}letemployee=Employee(name:"Tom", sex:.boy)print(employee.requestParameters)
// {"name":"Tom", "sex":1, "test":{"t1":1, "t2":"t2"}}You need to rewrite init function when you use @JSONProperty or @JSONIgnore. The default init function dosen't work, because @propertyWrapper is struct in real.
In the init function, you should set property which is @JSONProperty or @JSONIgnore in the last if you don't set the default value of the property in your model.
structEmployee:JSONParameters{@JSONProperty(name:"employee_name")varname:Stringvarage:Intinit(name:String,
age:Int){self.age = age
self.name = name
}}DO NOT use XYJSON and HandyJSON in one model, otherwise HandyJSON dosen't work. Because @propertyWrapper is struct in real.
XYJSON is under MIT license. See the LICENSE file for more info.