JsonResponse.swift 962 B

1234567891011121314151617181920212223242526272829
  1. //
  2. // JsonResponse.swift
  3. // Swifter
  4. //
  5. // Created by Dawid Szymczak on 15/08/16.
  6. // Copyright © 2016 Damian Kołakowski. All rights reserved.
  7. //
  8. import Foundation
  9. public class JsonResponse: Response {
  10. public override func content() -> (contentLength: Int, contentString: String) {
  11. #if os(Linux)
  12. let data = [UInt8]("Not ready for Linux.".utf8)
  13. return (data.count, {
  14. try $0.write(data)
  15. })
  16. #else
  17. guard NSJSONSerialization.isValidJSONObject(self.contentObject) else {
  18. throw SerializationError.InvalidObject
  19. }
  20. let json = try NSJSONSerialization.dataWithJSONObject(self.contentObject, options: NSJSONWritingOptions.PrettyPrinted)
  21. let data = Array(UnsafeBufferPointer(start: UnsafePointer<UInt8>(json.bytes), count: json.length))
  22. // To be fixed
  23. return (data.count, String(json))
  24. #endif
  25. }
  26. }