         let nl = pretty ? "\n" : ""
 
         let encoder = JSONEncoder()
+        if #available(OSX 10.13, *) {
+            encoder.outputFormatting = .sortedKeys
+        } else {
+            // Fallback on earlier versions
+        }
         var fieldCount = 1
         for (field, fieldProps) in properties {
             propertyStr.append("\(sp)\"\(field)\": ")

    ///
    public func serializeAPIToJSON() throws -> String? {
        let encoder = JSONEncoder()
        encoder.outputFormatting = .prettyPrinted
        let encodedData = try encoder.encode(self)
        var search = "}"
        var json = String(data: encodedData, encoding: .utf8)
        if var unwrappedJson = json {
            if encoder.outputFormatting == .prettyPrinted {
                search = "\n}"
            }
            if let insertionIndex = unwrappedJson.range(of: search, options: .backwards)?.lowerBound {
                let definitions = try JSONEncodeDefinitions(pretty: encoder.outputFormatting == .prettyPrinted)
                unwrappedJson.replaceSubrange(insertionIndex..., with: definitions)
            }
            json = unwrappedJson

            contentStr.append("\(sp)\"type\": \"\(modelRef.type)\",\(nl)")
            if modelRef.required.count > 0 {
                let encoder = JSONEncoder()
                if #available(OSX 10.13, *) {
                    encoder.outputFormatting = .sortedKeys
                } else {
                    // Fallback on earlier versions
                }
                do {
                    let encodedData = try encoder.encode(modelRef.required.sorted())
                    if let json = String(data: encodedData, encoding: .utf8) {
                        contentStr.append("\(sp)\"required\": \(json),\(nl)")
                    } else {
