StrongJSON is a Swift framework to help deserialize complex JSON objects.
Its biggest advantage over other frameworks is that during deserialization, types are checked and data is validated. If no exception occured during deserialization, the resulting Swift objects are guarenteed to be well formed.
With StrongJSON, you'll handle all JSON errors at once.
No more checking for nil
, or accessing a value using .string
, .int
or crazy operators!
For more information, check out the docs.
// The type annotations are not required, they're just
// here to make the results explicit.
let n: Int = try Int.parseJSON("0")
let digits: [UInt8] = try JSONArray<UInt8>.parseJSON(
"[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]"
)
let ascii: [String: UInt8] = try JSONDictionary<UInt8>.parseJSON(
"{ \"A\": 65, \"B\": 66 }"
)
The following examples all throw exceptions:
try UInt.parseJSON("-1")
try UInt.parseJSON("true")
try UInt8.parseJSON("256")
For an example with a class, see UsageTests.swift.
For one with an enum, see JSONRawRepresentable.
Format: Deserializer[: OutputType]
, where OutputType
defaults to Deserializer
.
Bool
Float32
(aka.Float
),Float64
(aka.Double
)Int
,Int8
,Int16
,Int32
,Int64
String
UInt
,UInt8
,UInt16
,UInt32
,UInt64
URL
JSONArray<T>
:[T]
JSONDictionary<T>
:[String: T]
JSONNull<T>
:nil: T?
(use when the value isnull
)JSONNullable<T>
:T?
(use when the value can benull
)JSONOptional<T>
:T?
(use when the value might not be in the JSON)
DiscardJSON
:Void
JSONRawRepresentable
: a protocol to auto-implementJSONDeserializable
for your enums with raw valuesJSONRepresentable
: a protocol to reduce boiler-plate for self-deserializing types to atypealias
and an initialiser