JSONValue.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2024 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. import Foundation
  15. /// A collection of name-value pairs representing a JSON object.
  16. ///
  17. /// This may be decoded from, or encoded to, a
  18. /// [`google.protobuf.Struct`](https://protobuf.dev/reference/protobuf/google.protobuf/#struct).
  19. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  20. public typealias JSONObject = [String: JSONValue]
  21. /// Represents a value in one of JSON's data types.
  22. ///
  23. /// This may be decoded from, or encoded to, a
  24. /// [`google.protobuf.Value`](https://protobuf.dev/reference/protobuf/google.protobuf/#value).
  25. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  26. public enum JSONValue: Sendable {
  27. /// A `null` value.
  28. case null
  29. /// A numeric value.
  30. case number(Double)
  31. /// A string value.
  32. case string(String)
  33. /// A boolean value.
  34. case bool(Bool)
  35. /// A JSON object.
  36. case object(JSONObject)
  37. /// An array of `JSONValue`s.
  38. case array([JSONValue])
  39. }
  40. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  41. extension JSONValue: Decodable {
  42. public init(from decoder: Decoder) throws {
  43. let container = try decoder.singleValueContainer()
  44. if container.decodeNil() {
  45. self = .null
  46. } else if let numberValue = try? container.decode(Double.self) {
  47. self = .number(numberValue)
  48. } else if let stringValue = try? container.decode(String.self) {
  49. self = .string(stringValue)
  50. } else if let boolValue = try? container.decode(Bool.self) {
  51. self = .bool(boolValue)
  52. } else if let objectValue = try? container.decode(JSONObject.self) {
  53. self = .object(objectValue)
  54. } else if let arrayValue = try? container.decode([JSONValue].self) {
  55. self = .array(arrayValue)
  56. } else {
  57. throw DecodingError.dataCorruptedError(
  58. in: container,
  59. debugDescription: "Failed to decode JSON value."
  60. )
  61. }
  62. }
  63. }
  64. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  65. extension JSONValue: Encodable {
  66. public func encode(to encoder: Encoder) throws {
  67. var container = encoder.singleValueContainer()
  68. switch self {
  69. case .null:
  70. try container.encodeNil()
  71. case let .number(numberValue):
  72. // Convert to `Decimal` before encoding for consistent floating-point serialization across
  73. // platforms. E.g., `Double` serializes 3.14159 as 3.1415899999999999 in some cases and
  74. // 3.14159 in others. See
  75. // https://forums.swift.org/t/jsonencoder-encodable-floating-point-rounding-error/41390/4 for
  76. // more details.
  77. try container.encode(Decimal(numberValue))
  78. case let .string(stringValue):
  79. try container.encode(stringValue)
  80. case let .bool(boolValue):
  81. try container.encode(boolValue)
  82. case let .object(objectValue):
  83. try container.encode(objectValue)
  84. case let .array(arrayValue):
  85. try container.encode(arrayValue)
  86. }
  87. }
  88. }
  89. @available(iOS 15.0, macOS 12.0, macCatalyst 15.0, tvOS 15.0, watchOS 8.0, *)
  90. extension JSONValue: Equatable {}