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

Remove JSONHelpers

parent 05995867
No related branches found
No related tags found
2 merge requests!102Release 1.0.0,!53Custom JSONEncoder & JSONDecoder
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
) -> 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
}
/// 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,
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
}
......@@ -17,12 +17,10 @@ extension DHKey: Codable {
}
public static func decode(_ data: Data) throws -> Self {
let data = convertJsonNumberToString(in: data, at: "Value")
return try JSONDecoder().decode(Self.self, from: data)
try JSONDecoder().decode(Self.self, from: data)
}
public func encode() throws -> Data {
let data = try JSONEncoder().encode(self)
return convertJsonStringToNumber(in: data, at: "Value")
try JSONEncoder().encode(self)
}
}
......@@ -17,24 +17,20 @@ extension GroupMember: Codable {
}
public static func decode(_ data: Data) throws -> Self {
let data = convertJsonNumberToString(in: data, at: "Value")
return try JSONDecoder().decode(Self.self, from: data)
try JSONDecoder().decode(Self.self, from: data)
}
public func encode() throws -> Data {
let data = try JSONEncoder().encode(self)
return convertJsonStringToNumber(in: data, at: "Value")
try JSONEncoder().encode(self)
}
}
extension Array where Element == GroupMember {
public static func decode(_ data: Data) throws -> Self {
let data = convertJsonNumberToString(in: data, at: "Value")
return try JSONDecoder().decode(Self.self, from: data)
try JSONDecoder().decode(Self.self, from: data)
}
public func encode() throws -> Data {
let data = try JSONEncoder().encode(self)
return convertJsonStringToNumber(in: data, at: "Value")
try JSONEncoder().encode(self)
}
}
import CustomDump
import XCTest
@testable import XXClient
final class JSONHelpersTests: XCTestCase {
func testConvertingNumberToStringByKey() {
assertConvertingJsonNumberToString(
input: #"{"number":1234567890,"text":"hello"}"#,
key: "number",
expected: #"{"number":"1234567890","text":"hello"}"#
)
assertConvertingJsonNumberToString(
input: #"{"text":"hello","number":1234567890}"#,
key: "number",
expected: #"{"text":"hello","number":"1234567890"}"#
)
assertConvertingJsonNumberToString(
input: #"{ "number" : 1234567890 , "text" : "hello" }"#,
key: "number",
expected: #"{ "number" : "1234567890" , "text" : "hello" }"#
)
assertConvertingJsonNumberToString(
input: #"{ "text" : "hello" , "number" : 1234567890 }"#,
key: "number",
expected: #"{ "text" : "hello" , "number" : "1234567890" }"#
)
assertConvertingJsonNumberToString(
input: """
{
"number": 1234567890,
"text": "hello"
}
""",
key: "number",
expected: """
{
"number": "1234567890",
"text": "hello"
}
"""
)
assertConvertingJsonNumberToString(
input: """
{
"text": "hello",
"number": 1234567890
}
""",
key: "number",
expected: """
{
"text": "hello",
"number": "1234567890"
}
"""
)
}
func testConvertingStringToNumber() {
assertConvertingJsonStringToNumber(
input: #"{"number":"1234567890","text":"hello"}"#,
key: "number",
expected: #"{"number":1234567890,"text":"hello"}"#
)
assertConvertingJsonStringToNumber(
input: #"{"text":"hello","number":"1234567890"}"#,
key: "number",
expected: #"{"text":"hello","number":1234567890}"#
)
assertConvertingJsonStringToNumber(
input: #"{ "number" : "1234567890" , "text" : "hello" }"#,
key: "number",
expected: #"{ "number" : 1234567890 , "text" : "hello" }"#
)
assertConvertingJsonStringToNumber(
input: #"{ "text" : "hello" , "number" : "1234567890" }"#,
key: "number",
expected: #"{ "text" : "hello" , "number" : 1234567890 }"#
)
assertConvertingJsonStringToNumber(
input: """
{
"number": "1234567890",
"text": "hello"
}
""",
key: "number",
expected: """
{
"number": 1234567890,
"text": "hello"
}
"""
)
assertConvertingJsonStringToNumber(
input: """
{
"text": "hello",
"number": "1234567890"
}
""",
key: "number",
expected: """
{
"text": "hello",
"number": 1234567890
}
"""
)
}
}
private func assertConvertingJsonNumberToString(
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
)
}
private func assertConvertingJsonStringToNumber(
input: String,
key: String,
expected: String,
file: StaticString = #file,
line: UInt = #line
) {
XCTAssertNoDifference(
String(
data: convertJsonStringToNumber(
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