JSONValue.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. public typealias JSONObject = [String: JSONValue]
  20. /// Represents a value in one of JSON's data types.
  21. ///
  22. /// This may be decoded from, or encoded to, a
  23. /// [`google.protobuf.Value`](https://protobuf.dev/reference/protobuf/google.protobuf/#value).
  24. public enum JSONValue {
  25. /// A `null` value.
  26. case null
  27. /// A numeric value.
  28. case number(Double)
  29. /// A string value.
  30. case string(String)
  31. /// A boolean value.
  32. case bool(Bool)
  33. /// A JSON object.
  34. case object(JSONObject)
  35. /// An array of `JSONValue`s.
  36. case array([JSONValue])
  37. }
  38. extension JSONValue: Decodable {
  39. public init(from decoder: Decoder) throws {
  40. let container = try decoder.singleValueContainer()
  41. if container.decodeNil() {
  42. self = .null
  43. } else if let numberValue = try? container.decode(Double.self) {
  44. self = .number(numberValue)
  45. } else if let stringValue = try? container.decode(String.self) {
  46. self = .string(stringValue)
  47. } else if let boolValue = try? container.decode(Bool.self) {
  48. self = .bool(boolValue)
  49. } else if let objectValue = try? container.decode(JSONObject.self) {
  50. self = .object(objectValue)
  51. } else if let arrayValue = try? container.decode([JSONValue].self) {
  52. self = .array(arrayValue)
  53. } else {
  54. throw DecodingError.dataCorruptedError(
  55. in: container,
  56. debugDescription: "Failed to decode JSON value."
  57. )
  58. }
  59. }
  60. }
  61. extension JSONValue: Encodable {
  62. public func encode(to encoder: Encoder) throws {
  63. var container = encoder.singleValueContainer()
  64. switch self {
  65. case .null:
  66. try container.encodeNil()
  67. case let .number(numberValue):
  68. // Convert to `Decimal` before encoding for consistent floating-point serialization across
  69. // platforms. E.g., `Double` serializes 3.14159 as 3.1415899999999999 in some cases and
  70. // 3.14159 in others. See
  71. // https://forums.swift.org/t/jsonencoder-encodable-floating-point-rounding-error/41390/4 for
  72. // more details.
  73. try container.encode(Decimal(numberValue))
  74. case let .string(stringValue):
  75. try container.encode(stringValue)
  76. case let .bool(boolValue):
  77. try container.encode(boolValue)
  78. case let .object(objectValue):
  79. try container.encode(objectValue)
  80. case let .array(arrayValue):
  81. try container.encode(arrayValue)
  82. }
  83. }
  84. }
  85. extension JSONValue: Equatable {}