Skip to content
Snippets Groups Projects

Add macOS support

6 files
+ 247
31
Compare changes
  • Side-by-side
  • Inline

Files

import Foundation
/// Replaces all numbers at provided key with string equivalents
///
/// Example input:
/// {
/// "key": 123,
/// "object": {
/// "hello": "world",
/// "key": 321
/// }
/// }
///
/// Example output:
/// {
/// "key": "123",
/// "object": {
/// "hello": "world",
/// "key": "321"
/// }
/// }
///
/// - Parameters:
/// - input: JSON data
/// - key: the key which values should be converted
/// - Returns: JSON data
func convertJsonNumberToString(
in input: Data,
at key: String
@@ -18,16 +42,40 @@ func convertJsonNumberToString(
return output
}
func convertJsonNumberToString(
/// Replaces all strings at provided key with number equivalents
///
/// Example input:
/// {
/// "key": "123",
/// "object": {
/// "hello": "world",
/// "key": "321"
/// }
/// }
///
/// Example output:
/// {
/// "key": 123,
/// "object": {
/// "hello": "world",
/// "key": 321
/// }
/// }
///
/// - Parameters:
/// - input: JSON data
/// - key: the key which values should be converted
/// - Returns: JSON data
func convertJsonStringToNumber(
in input: Data,
minNumberLength: Int
at key: String
) -> Data {
guard var string = String(data: input, encoding: .utf8) else {
return input
}
string = string.replacingOccurrences(
of: #":( *)([0-9]{\#(minNumberLength),})( *)(,*)"#,
with: #":$1"$2"$3$4"#,
of: #""\#(key)"( *):( *)"([0-9]+)"( *)(,*)"#,
with: #""\#(key)"$1:$2$3$4$5"#,
options: [.regularExpression]
)
guard let output = string.data(using: .utf8) else {
Loading