Skip to content

matthewcheok/JSONSerialization

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

#JSONSerialization

Natively implemented alternative to NSJSONSerialization. No Foundation required! Work with native Swift types such as Int, Double and Bool instead of NSNumber.

Here's some JSON in text form:

let JSON = "{\"menu\": {  \"id\": \"file\",  \"value\": \"File\",  \"popup\": {    \"menuitem\": [{\"value\": \"New\", \"onclick\": \"CreateNewDoc()\"},      {\"value\": \"Open\", \"onclick\": \"OpenDoc()\"},{\"value\": \"Close\", \"onclick\": \"CloseDoc()\"}    ]  }}}"

We can try to get an object from the string:

var object: Any = JSONSerialization.JSONNull()
do {
    object = try JSONSerialization.decode(JSON)
    print(object)
}
catch {
    print(error)
}

Result:

[menu: [id: "file", popup: [menuitem: [[onclick: "CreateNewDoc()", value: "New"], [onclick: "OpenDoc()", value: "Open"], [onclick: "CloseDoc()", value: "Close"]]], value: "File"]]

We can pretty print the JSON from compatible objects:

do {
    let text = try JSONSerialization.encode(object, prettyPrint: true)
    print(text)
}
catch {
    print(error)
}

Result:

{
   "menu": {
      "id": "file",
      "popup": {
         "menuitem": [
            {
               "onclick": "CreateNewDoc()",
               "value": "New"
            },
            {
               "onclick": "OpenDoc()",
               "value": "Open"
            },
            {
               "onclick": "CloseDoc()",
               "value": "Close"
            }
         ]
      },
      "value": "File"
   }
}

License

JSONSerialization is under the MIT license.

About

Natively implemented JSON serialization in Swift

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages