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

Add converJsonNumberToString function

parent 108dd0bf
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!49Update Bindings
import Foundation
func convertJsonNumberToString(
in input: Data,
at key: String
) -> Data {
guard var string = String(data: input, encoding: .utf8) else {
return input
}
string = string.replacingOccurrences(
of: #""\#(key)"( *):( *)([0-9]+)( *)(,*)"#,
with: #""\#(key)"$1:$2"$3"$4$5"#,
options: [.regularExpression]
)
guard let output = string.data(using: .utf8) else {
return input
}
return output
}
import CustomDump
import XCTest
@testable import XXClient
final class ConvertJsonNumberToStringTests: XCTestCase {
func testConverting() {
assert(
input: #"{"number":1234567890,"text":"hello"}"#,
key: "number",
expected: #"{"number":"1234567890","text":"hello"}"#
)
assert(
input: #"{"text":"hello","number":1234567890}"#,
key: "number",
expected: #"{"text":"hello","number":"1234567890"}"#
)
assert(
input: #"{ "number" : 1234567890 , "text" : "hello" }"#,
key: "number",
expected: #"{ "number" : "1234567890" , "text" : "hello" }"#
)
assert(
input: #"{ "text" : "hello" , "number" : 1234567890 }"#,
key: "number",
expected: #"{ "text" : "hello" , "number" : "1234567890" }"#
)
assert(
input: """
{
"number": 1234567890,
"text": "hello"
}
""",
key: "number",
expected: """
{
"number": "1234567890",
"text": "hello"
}
"""
)
assert(
input: """
{
"text": "hello",
"number": 1234567890
}
""",
key: "number",
expected: """
{
"text": "hello",
"number": "1234567890"
}
"""
)
}
}
private func assert(
input: String,
key: String,
expected: String,
file: StaticString = #file,
line: UInt = #line
) {
XCTAssertNoDifference(
String(
data: convertJsonNumberToString(
in: input.data(using: .utf8)!,
at: key
),
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