Skip to content

Commit

Permalink
Added options to serialize to skip null keys
Browse files Browse the repository at this point in the history
  • Loading branch information
davidahouse committed Feb 3, 2017
1 parent f79ddc8 commit ede3aa5
Showing 1 changed file with 23 additions and 6 deletions.
29 changes: 23 additions & 6 deletions Sources/JSONSerializing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

import Foundation

// MARK: - Serialize Options

/// An `OptionSet` used to represent the different options available for serializing `JSON` with `null` values.
/// * `.nullSkipsKey` - Skip keys with `null` values
public struct SerializeOptions: OptionSet {
public let rawValue: Int
public init(rawValue: Int) {
self.rawValue = rawValue
}

/// Skip keys with `null` values
public static let nullSkipsKey = SerializeOptions(rawValue: 1 << 0)
}

// MARK: - Serialize JSON

extension JSON {
Expand All @@ -10,16 +24,16 @@ extension JSON {
/// - returns: A byte-stream containing the `JSON` ready for wire transfer.
/// - throws: Errors that arise from `JSONSerialization`.
/// - see: Foundation.JSONSerialization
public func serialize() throws -> Data {
return try JSONSerialization.data(withJSONObject: toJSONSerializationValue(), options: [])
public func serialize(options: SerializeOptions = SerializeOptions(rawValue: 0)) throws -> Data {
return try JSONSerialization.data(withJSONObject: toJSONSerializationValue(options: options), options: [])
}

/// Attempt to serialize `JSON` into a `String`.
/// - returns: A `String` containing the `JSON`.
/// - throws: A `JSON.Error.StringSerializationError` or errors that arise from `JSONSerialization`.
/// - see: Foundation.JSONSerialization
public func serializeString() throws -> String {
let data = try self.serialize()
public func serializeString(options: SerializeOptions = SerializeOptions(rawValue: 0)) throws -> String {
let data = try self.serialize(options: options)
guard let json = String(data: data, encoding: String.Encoding.utf8) else {
throw Error.stringSerializationError
}
Expand All @@ -28,14 +42,17 @@ extension JSON {

/// A function to help with the serialization of `JSON`.
/// - returns: An `Any` suitable for `JSONSerialization`'s use.
private func toJSONSerializationValue() -> Any {
private func toJSONSerializationValue(options: SerializeOptions = SerializeOptions(rawValue: 0)) -> Any {
switch self {
case .array(let jsonArray):
return jsonArray.map { $0.toJSONSerializationValue() }
case .dictionary(let jsonDictionary):
var cocoaDictionary = Swift.Dictionary<Swift.String, Any>(minimumCapacity: jsonDictionary.count)
for (key, json) in jsonDictionary {
cocoaDictionary[key] = json.toJSONSerializationValue()

if json != .null || (json == .null && !options.contains(.nullSkipsKey)) {
cocoaDictionary[key] = json.toJSONSerializationValue()
}
}
return cocoaDictionary
case .string(let str):
Expand Down

0 comments on commit ede3aa5

Please sign in to comment.