gpt4 book ai didi

ios - 是否有一个 swift 库可以让您自定义 JSON 中键的顺序?

转载 作者:行者123 更新时间:2023-11-29 05:09:19 35 4
gpt4 key购买 nike

我有以下情况:

struct RequestCommand: Codable {
var event: String
var uuid: String
var type: String
var data: [String]
}

var data = try! JSONEncoder().encode(RequestCommand(event: "click", uuid: "123456", type: "Button", data: ["A", "B", "C"]))

print(String(data: data, encoding: String.Encoding.utf8)!)

打印语句的结果如下:

{
"data": [
"A",
"B",
"C"
],
"event": "click",
"type": "Button",
"uuid": "123456"
}

正如您所看到的,JSON 中的键顺序与 RequestCommand 中的字段顺序不同。我知道 JSONEncoder 不能保证顺序,而且使用 .sortedKeys 对我来说也不起作用,因为我必须保持 RequestCommand 中字段的确切顺序,并且无法重命名它们。我的问题是,swift 中是否有一个库可以让我选择与 JSON 中的 swift 对象保持完全相同的顺序?

最佳答案

for (key, value) in OrderedDictionary 将保持顺序。

struct OrderedDictionary<KeyType:Hashable, ValueType> {
private var _dictionary:Dictionary<KeyType, ValueType>
private var _keys:Array<KeyType>

init() {
_dictionary = [:]
_keys = []
}

init(minimumCapacity:Int) {
_dictionary = Dictionary<KeyType, ValueType>(minimumCapacity:minimumCapacity)
_keys = Array<KeyType>()
}

init(_ dictionary:Dictionary<KeyType, ValueType>) {
_dictionary = dictionary
_keys = map(dictionary.keys) { $0 }
}

subscript(key:KeyType) -> ValueType? {
get {
return _dictionary[key]
}
set {
if newValue == nil {
self.removeValueForKey(key)
}
else {
self.updateValue(newValue!, forKey: key)
}
}
}

mutating func updateValue(value:ValueType, forKey key:KeyType) -> ValueType? {
let oldValue = _dictionary.updateValue(value, forKey: key)
if oldValue == nil {
_keys.append(key)
}
return oldValue
}

mutating func removeValueForKey(key:KeyType) {
_keys = _keys.filter { $0 != key }
_dictionary.removeValueForKey(key)
}

mutating func removeAll(keepCapacity:Int) {
_keys = []
_dictionary = Dictionary<KeyType,ValueType>(minimumCapacity: keepCapacity)
}

var count: Int { get { return _dictionary.count } }

// keys isn't lazy evaluated because it's just an array anyway
var keys:[KeyType] { get { return _keys } }

// values is lazy evaluated because of the dictionary lookup and creating a new array
var values:GeneratorOf<ValueType> {
get {
var index = 0
return GeneratorOf<ValueType> {
if index >= self._keys.count {
return nil
}
else {
let key = self._keys[index]
index++
return self._dictionary[key]
}
}
}
}
}

extension OrderedDictionary : SequenceType {
func generate() -> GeneratorOf<(KeyType, ValueType)> {
var index = 0
return GeneratorOf<(KeyType, ValueType)> {
if index >= self._keys.count {
return nil
}
else {
let key = self._keys[index]
index++
return (key, self._dictionary[key]!)
}
}
}
}

func ==<Key: Equatable, Value: Equatable>(lhs: OrderedDictionary<Key, Value>, rhs: OrderedDictionary<Key, Value>) -> Bool {
return lhs._keys == rhs._keys && lhs._dictionary == rhs._dictionary
}

func !=<Key: Equatable, Value: Equatable>(lhs: OrderedDictionary<Key, Value>, rhs: OrderedDictionary<Key, Value>) -> Bool {
return lhs._keys != rhs._keys || lhs._dictionary != rhs._dictionary
}

关于ios - 是否有一个 swift 库可以让您自定义 JSON 中键的顺序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59836213/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com