New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ignore nil properties in toJsonString() #70
Comments
I ended up writing an extension that does what I needed. Any feedback? If you'd like I can do a pull request for this (without the debugging statements). Thanks!, extension EVObject {
func toJsonStringWithoutEmptyKeys(performKeyCleanup: Bool) -> String {
let dict = EVReflection.toDictionary(self, performKeyCleanup: performKeyCleanup).0
let cleanDict = NSMutableDictionary()
for (key, value) in dict {
if let key = key as? String {
if let value = value as? String where value.characters.count == 0 {
print("Ignoring \(key)")
continue
}
else if let value = value as? NSArray where value.count == 0 {
print("Ignoring \(key)")
continue
}
else if value is NSNull {
print("Ignoring \(key)")
continue
}
else {
cleanDict[key] = value
}
}
}
//Get json string from element dictionary
do {
let jsonData = try NSJSONSerialization.dataWithJSONObject(cleanDict, options: NSJSONWritingOptions.PrettyPrinted)
return String(data: jsonData, encoding: NSASCIIStringEncoding) ?? ""
} catch let error as NSError {
print(error)
return ""
}
}
} |
This will work if you do not have any sub objects. I was thinking of adding an extra parameter to the toDictionary function which will do about the same. I first have 2 annoying bugs to solve. I will pick this up after that. |
No worries, and thanks for your work, Edwin! |
I just pushed version 2.26.0 to GitHub and Cocoapods with support for ignoring certain values. See the test at:
Here is the code:
You will get this in your output window for this test:
|
Nice!, thank you Edwin. I'll check it out right now. |
Somehow adding this to my BaseClass:EVObject, is messing up deserialization of classes that have arrays of other BaseClass derivatives. Apparently, the Thanks |
ah, true, deserialization is also effected. I think I know how to fix this. Let me work on it.. |
Thanks Edwin! |
OK, Almost there... Now every function has an extra parameter (or replaced the performKeyCleanup) where you can specify the options from:
Most functions have the default set to .Default. The NSCoding (plus save and load) have it set to .None I still need to add a lot of unit tests to capture all variations. I think I need an other day to make it solid. |
Cool, thanks! Thanks!! |
It's an optionset which is better in this case. You Will be able to pass on multiple options in an array notation. See the Default item. It has all options. In your case i was enumerating properties after a cleanup which lead to skipping values from the json. I am now already calling that function with the right options. So the skipvalues function will always be skipped during enumeration, but the actual set can be influenced using this new parameter. Its a little hard to explain without code. You will see the unit tests soon. :-) |
I have pushed the new code to github. It's not a new version yet. EVReflection/EVReflection/EVReflectionTests/EVReflectionConversionOptionsTest.swift Line 39 in c076b3d
|
Cool! Will try in a sec. |
Nice work, it's working perfectly!! |
And it's now pushed as version 2.27.0 to GitHub and CocoaPods |
Some aditional info (now also in the readme) Almost any EVReflection functions have a property for ConversionOptions. In most cases the default value of this is set to .Default. In case of NSCoding and related functions (like save and load) the default is set to .None. The available options are:
You can use multiple options at the same type by specifying them in array notation. Like the .Default will be all options enabled like: [PropertyConverter, PropertyMapping, SkipPropertyValue, KeyCleanup] |
Thank you! I was actually just reading the updates to the readme :) |
Is there any way to set up EVReflection to ignore properties that are nil when generating JSON?
eg. I have a model
if prop1 is nil, I would like toJsonString() to return a string ignoring prop1 completely. (This is consistent with the way JAYL-Entity works, and I'm in the process of replacing it with EVReflection).
Thanks!,
Raf.
The text was updated successfully, but these errors were encountered: