Skip to content
Snippets Groups Projects
Commit a08505ec authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Update convertJsonNumberToString utility

parent 1be4f7c1
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!51Remove problematic bigint encoding
...@@ -17,3 +17,21 @@ func convertJsonNumberToString( ...@@ -17,3 +17,21 @@ func convertJsonNumberToString(
} }
return output return output
} }
func convertJsonNumberToString(
in input: Data,
minNumberLength: Int
) -> Data {
guard var string = String(data: input, encoding: .utf8) else {
return input
}
string = string.replacingOccurrences(
of: #":( *)([0-9]{\#(minNumberLength),})( *)(,*)"#,
with: #":$1"$2"$3$4"#,
options: [.regularExpression]
)
guard let output = string.data(using: .utf8) else {
return input
}
return output
}
...@@ -59,6 +59,28 @@ final class ConvertJsonNumberToStringTests: XCTestCase { ...@@ -59,6 +59,28 @@ final class ConvertJsonNumberToStringTests: XCTestCase {
} }
""" """
) )
assert(
input: """
{
"text": "hello",
"number1": 123456789,
"number2": 1234567890,
"number3": 123456789,
"number4": 1234567890
}
""",
minNumberLength: 10,
expected: """
{
"text": "hello",
"number1": 123456789,
"number2": "1234567890",
"number3": 123456789,
"number4": "1234567890"
}
"""
)
} }
} }
...@@ -82,3 +104,24 @@ private func assert( ...@@ -82,3 +104,24 @@ private func assert(
line: line line: line
) )
} }
private func assert(
input: String,
minNumberLength: Int,
expected: String,
file: StaticString = #file,
line: UInt = #line
) {
XCTAssertNoDifference(
String(
data: convertJsonNumberToString(
in: input.data(using: .utf8)!,
minNumberLength: minNumberLength
),
encoding: .utf8
)!,
expected,
file: file,
line: line
)
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment