peheje / jsonserializerswift Goto Github PK
View Code? Open in Web Editor NEWA simple Json Serializer for Swift
License: MIT License
A simple Json Serializer for Swift
License: MIT License
//1. convert string to NSData
let jsonData = json.dataUsingEncoding(NSUTF8StringEncoding)!
//2. convert JSON data to JSON object
let jsonObject:AnyObject = try! NSJSONSerialization.JSONObjectWithData(jsonData, options: [])
//3. convert back to JSON data by setting .PrettyPrinted option
let prettyJsonData = try! NSJSONSerialization.dataWithJSONObject(jsonObject, options: .PrettyPrinted)
//4. convert NSData back to NSString (use NSString init for convenience), later you can convert to String.
let prettyPrintedJson = NSString(data: prettyJsonData, encoding: NSUTF8StringEncoding)!
return prettyPrintedJson as String
Can you please update the code so Int64 is treated as number, not as a string?
Optional doubles such as:
var d: Double? = 2.2
Is serialized as
"d: 2.2000000000000002"
Probably because removing the optional part is implemented by parsing the double to a string beforehand (I don't see a way to force unwrap a value of type "Any" aka "Protocol<>").
Hi @peheje It would be great of you if you host this on the Dependency Managing Tools. Since everyone in the industry tries to keep the third party lib as dependencies. So, hosting it in Cocoa Pods, Carthage should suffice.
Hello
Trying out your job, and getting json like this: "route": {"[0]": {"lat": 37.34520782, "lon": -122.0960117}, "[1]": {"lat": 37.34520782, "lon": -122.0960117}"
How could I remove these "[0]" indexes? And just leave objects {"lat": 37.34520782, "lon": -122.0960117}
Thank you
hello. I tested this class and I got some odd results.
I tested using a class to transform it to a json string then get the dictionary for it.
so lets say:
class Cat {
var name: String?
var color: String?
var age: String?
var location: Location
}
struct Location{
var latitude: Int
var longitude: Int
}
let cat = Cat()
cat.name = "Mr. Cat"
cat.color = "Orange"
cat.age = "3"
cat.location.longitude = 29.393
cat.location.latitude = -75.762523
let jsonString = JSONSerializer.toJson(cat)
print(jsonString)
the output is :
{"name":"Mr. Cat", "color":"Orange", "age":"3", "location":appName.Location(latitude: 29.393, longitude: -75.762523)}
I dont know why the var location is printed this why just because its a struct or a embeded class.
Hi @peheje,
I added this to my local to handle Multi-dimentional Arrays.
else if let array = value as? [[Any]] {
handledValue += "["
for (index, value) in array.enumerated() {
handledValue += "\(value)"
handledValue += (index < array.count-1 ? ", " : "")
}
handledValue += "]"
}
Hi,
First of thanks a lot for such an effort to build extremely useful library. It is indeed a very helpful.
This is a feature request. Can you please include one property to configure exclusion of Null properties from model object while creating the JSON string.
Let me know if I can be of any help.
Hey I would love to see this library at CocoaPods.
Is there a plan from your side?
What about having let object: MyClass = JSONSerializer.parse(jsonString)
?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.