Skip to content

Use reference type NSNumber for JSONSerialization #1633

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Foundation/JSONEncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ open class JSONDecoder {
/// - throws: `DecodingError.dataCorrupted` if values requested from the payload are corrupted, or if the given data is not valid JSON.
/// - throws: An error if any value throws an error during decoding.
open func decode<T : Decodable>(_ type: T.Type, from data: Data) throws -> T {
let topLevel = try JSONSerialization.jsonObject(with: data, options: [.useReferenceNumericTypes])
let topLevel = try JSONSerialization.jsonObject(with: data, options: [])
let decoder = _JSONDecoder(referencing: topLevel, options: self.options)
return try T(from: decoder)
}
Expand Down
13 changes: 4 additions & 9 deletions Foundation/JSONSerialization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ extension JSONSerialization {
public static let mutableContainers = ReadingOptions(rawValue: 1 << 0)
public static let mutableLeaves = ReadingOptions(rawValue: 1 << 1)
public static let allowFragments = ReadingOptions(rawValue: 1 << 2)
internal static let useReferenceNumericTypes = ReadingOptions(rawValue: 1 << 15)
internal static let useNativeNumericTypes = ReadingOptions(rawValue: 1 << 15)
}

public struct WritingOptions : OptionSet {
Expand Down Expand Up @@ -841,7 +841,7 @@ private struct JSONReader {
return nil
}

let shouldUseReferenceType = opt.contains(.useReferenceNumericTypes)
let shouldUseReferenceType = !opt.contains(.useNativeNumericTypes)

if intDistance == doubleDistance {
return (shouldUseReferenceType ? NSNumber(value: intResult) : intResult,
Expand All @@ -851,11 +851,6 @@ private struct JSONReader {
return nil
}

if doubleResult == doubleResult.rounded() {
return (shouldUseReferenceType ? NSNumber(value: Int(doubleResult)) : Int(doubleResult),
doubleDistance)
}

return (shouldUseReferenceType ? NSNumber(value: doubleResult) : doubleResult,
doubleDistance)
}
Expand Down Expand Up @@ -887,11 +882,11 @@ private struct JSONReader {
return (value, parser)
}
else if let parser = try consumeASCIISequence("true", input: input) {
let result: Any = opt.contains(.useReferenceNumericTypes) ? NSNumber(value: true) : true
let result: Any = opt.contains(.useNativeNumericTypes) ? true : NSNumber(value: true)
return (result, parser)
}
else if let parser = try consumeASCIISequence("false", input: input) {
let result: Any = opt.contains(.useReferenceNumericTypes) ? NSNumber(value: false) : false
let result: Any = opt.contains(.useNativeNumericTypes) ? false : NSNumber(value: false)
return (result, parser)
}
else if let parser = try consumeASCIISequence("null", input: input) {
Expand Down