@@ -32,6 +32,102 @@ public enum JSON:
3232 }
3333 }
3434
35+ public subscript( key: String ) -> [ Any ? ] ? {
36+ get {
37+ guard case let . dictionary( dict) = self else { return nil }
38+ return dict [ key] ? . arrayValue
39+ }
40+ set {
41+ guard case var . dictionary( dict) = self else { return }
42+ if let newValue {
43+ dict [ key] = . array( newValue. compactMap ( \. json) )
44+ } else {
45+ dict. removeValue ( forKey: key)
46+ }
47+ self = . dictionary( dict)
48+ }
49+ }
50+
51+ public subscript( key: String ) -> Bool ? {
52+ get {
53+ guard case let . dictionary( dict) = self else { return nil }
54+ return dict [ key] ? . boolValue
55+ }
56+ set {
57+ guard case var . dictionary( dict) = self else { return }
58+ if let newValue {
59+ dict [ key] = . boolean( newValue)
60+ } else {
61+ dict. removeValue ( forKey: key)
62+ }
63+ self = . dictionary( dict)
64+ }
65+ }
66+
67+ public subscript( key: String ) -> [ String : Any ? ] ? {
68+ get {
69+ guard case let . dictionary( dict) = self else { return nil }
70+ return dict [ key] ? . dictionaryValue
71+ }
72+ set {
73+ guard case var . dictionary( dict) = self else { return }
74+ if let newValue {
75+ dict [ key] = . dictionary( newValue. compactMapValues ( \. json) )
76+ } else {
77+ dict. removeValue ( forKey: key)
78+ }
79+ self = . dictionary( dict)
80+ }
81+ }
82+
83+ public subscript( key: String ) -> Double ? {
84+ get {
85+ guard case let . dictionary( dict) = self else { return nil }
86+ return dict [ key] ? . doubleValue
87+ }
88+ set {
89+ guard case var . dictionary( dict) = self else { return }
90+ if let newValue {
91+ dict [ key] = . number( newValue)
92+ } else {
93+ dict. removeValue ( forKey: key)
94+ }
95+ self = . dictionary( dict)
96+ }
97+ }
98+
99+ public subscript( key: String ) -> Int ? {
100+ get {
101+ guard case let . dictionary( dict) = self else { return nil }
102+ return dict [ key] ? . integerValue
103+ }
104+ set {
105+ guard case var . dictionary( dict) = self else { return }
106+ if let newValue {
107+ dict [ key] = . number( Double ( newValue) )
108+ } else {
109+ dict. removeValue ( forKey: key)
110+ }
111+ self = . dictionary( dict)
112+ }
113+ }
114+
115+ public subscript( key: String ) -> String ? {
116+ get {
117+ guard case let . dictionary( dict) = self else { return nil }
118+ return dict [ key] ? . stringValue
119+ }
120+ set {
121+ guard case var . dictionary( dict) = self else { return }
122+ if let newValue {
123+ dict [ key] = . string( newValue)
124+ } else {
125+ dict. removeValue ( forKey: key)
126+ }
127+ self = . dictionary( dict)
128+ }
129+ }
130+
35131 public subscript( index: Int ) -> JSON ? {
36132 get {
37133 guard case let . array( arr) = self , index < arr. count
@@ -216,6 +312,102 @@ public extension JSON? {
216312 }
217313 }
218314
315+ subscript( key: String ) -> [ Any ? ] ? {
316+ get {
317+ guard case let . dictionary( dict) = self else { return nil }
318+ return dict [ key] ? . arrayValue
319+ }
320+ set {
321+ guard case var . dictionary( dict) = self else { return }
322+ if let newValue {
323+ dict [ key] = . array( newValue. compactMap ( \. json) )
324+ } else {
325+ dict. removeValue ( forKey: key)
326+ }
327+ self = . dictionary( dict)
328+ }
329+ }
330+
331+ subscript( key: String ) -> Bool ? {
332+ get {
333+ guard case let . dictionary( dict) = self else { return nil }
334+ return dict [ key] ? . boolValue
335+ }
336+ set {
337+ guard case var . dictionary( dict) = self else { return }
338+ if let newValue {
339+ dict [ key] = . boolean( newValue)
340+ } else {
341+ dict. removeValue ( forKey: key)
342+ }
343+ self = . dictionary( dict)
344+ }
345+ }
346+
347+ subscript( key: String ) -> [ String : Any ? ] ? {
348+ get {
349+ guard case let . dictionary( dict) = self else { return nil }
350+ return dict [ key] ? . dictionaryValue
351+ }
352+ set {
353+ guard case var . dictionary( dict) = self else { return }
354+ if let newValue {
355+ dict [ key] = . dictionary( newValue. compactMapValues ( \. json) )
356+ } else {
357+ dict. removeValue ( forKey: key)
358+ }
359+ self = . dictionary( dict)
360+ }
361+ }
362+
363+ subscript( key: String ) -> Double ? {
364+ get {
365+ guard case let . dictionary( dict) = self else { return nil }
366+ return dict [ key] ? . doubleValue
367+ }
368+ set {
369+ guard case var . dictionary( dict) = self else { return }
370+ if let newValue {
371+ dict [ key] = . number( newValue)
372+ } else {
373+ dict. removeValue ( forKey: key)
374+ }
375+ self = . dictionary( dict)
376+ }
377+ }
378+
379+ subscript( key: String ) -> Int ? {
380+ get {
381+ guard case let . dictionary( dict) = self else { return nil }
382+ return dict [ key] ? . integerValue
383+ }
384+ set {
385+ guard case var . dictionary( dict) = self else { return }
386+ if let newValue {
387+ dict [ key] = . number( Double ( newValue) )
388+ } else {
389+ dict. removeValue ( forKey: key)
390+ }
391+ self = . dictionary( dict)
392+ }
393+ }
394+
395+ subscript( key: String ) -> String ? {
396+ get {
397+ guard case let . dictionary( dict) = self else { return nil }
398+ return dict [ key] ? . stringValue
399+ }
400+ set {
401+ guard case var . dictionary( dict) = self else { return }
402+ if let newValue {
403+ dict [ key] = . string( newValue)
404+ } else {
405+ dict. removeValue ( forKey: key)
406+ }
407+ self = . dictionary( dict)
408+ }
409+ }
410+
219411 subscript( index: Int ) -> JSON ? {
220412 get {
221413 guard case let . array( arr) = self , index < arr. count
0 commit comments