CustomJSONCodable.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. // Sources/SwiftProtobuf/CustomJSONCodable.swift - Custom JSON support for WKTs
  2. //
  3. // Copyright (c) 2014 - 2017 Apple Inc. and the project authors
  4. // Licensed under Apache License v2.0 with Runtime Library Exception
  5. //
  6. // See LICENSE.txt for license information:
  7. // https://github.com/apple/swift-protobuf/blob/main/LICENSE.txt
  8. //
  9. // -----------------------------------------------------------------------------
  10. ///
  11. /// Custom protocol for the WKTs to support their custom JSON encodings.
  12. ///
  13. // -----------------------------------------------------------------------------
  14. /// Allows WKTs to provide their custom JSON encodings.
  15. internal protocol _CustomJSONCodable {
  16. func encodedJSONString(options: JSONEncodingOptions) throws -> String
  17. mutating func decodeJSON(from: inout JSONDecoder) throws
  18. /// Called when the JSON `null` literal is encountered in a position where
  19. /// a message of the conforming type is expected. The message type can then
  20. /// handle the `null` value differently, if needed; for example,
  21. /// `Google_Protobuf_Value` returns a special instance whose `kind` is set to
  22. /// `.nullValue(.nullValue)`.
  23. ///
  24. /// The default behavior is to return `nil`, which indicates that `null`
  25. /// should be treated as the absence of a message.
  26. static func decodedFromJSONNull() throws -> Self?
  27. }
  28. extension _CustomJSONCodable {
  29. internal static func decodedFromJSONNull() -> Self? {
  30. // Return nil by default. Concrete types can provide custom logic.
  31. nil
  32. }
  33. }