-
Notifications
You must be signed in to change notification settings - Fork 1.2k
JSON Decoding based on JSONValue #2985
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
Conversation
@swift-ci test |
@@ -737,3 +701,48 @@ private extension JSONValue { | |||
} | |||
} | |||
} | |||
|
|||
extension NSNumber { | |||
static func fromJSONNumber(_ string: String) -> NSNumber? { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be a constructor (init?(jsonNumberString string: String)
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In some cases this might return a NSDecimalNumber
. I don't think that we can make it work with a convenience
initializer (which is the only option we have in an extension).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah seems to be the case even though NSDecimalNumber
is a subclass of NSNumber
@bendjones Can you take a look? @fabianfett is working on a full reimplementation at the Swift layer of JSONSerialization in SCF, and this patch has performance improvements by accessing the underlying JSONSerialization implementation instead of calling into JSONSerialization and then working on the returned values. |
7ece173
to
a912c67
Compare
@swift-ci test |
swiftlang/sourcekit-lsp#372 |
@swift-ci test |
a912c67
to
0efd82e
Compare
@swift-ci test |
|
||
guard let value = try self.decoder.unbox(entry, as: UInt.self) else { | ||
throw DecodingError.valueNotFound(type, DecodingError.Context(codingPath: self.decoder.codingPath, debugDescription: "Expected \(type) value but found null instead.")) | ||
@inline(__always) private func decodeFixedWidthInteger<T: FixedWidthInteger>() throws -> T { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the trick for really really I mean it inline me @inlinable @inline(__always)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@inlinable
can only be applied to public
functions AFAIK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah true that's the time to use that trick...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This all LGTM it's a large patch but I read through it twice to make sure I didn't miss something major
Merging pending test passes. |
// | ||
// Created by Fabian Fett on 02.03.21. | ||
// Copyright © 2021 Swift. All rights reserved. | ||
// |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a better header? Or maybe file is redundant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
urgh. this file was not intended to be commited.
0efd82e
to
6be5d3f
Compare
@swift-ci test |
|
||
if let double = Double(numberString) { | ||
return Decimal(double) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be best avoid the fallback to Double
parsing. Converting a Double
to Decimal
is lossy and if the string does not parse as a Decimal
anyway it can be considered an invalid value for the type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed.
6be5d3f
to
5ed74d3
Compare
@swift-ci test |
This is a follow up pr to #2966.
It should significantly improve overall JSON performance, since we save on a lot of roundtrips through
NSArray
andNSDictionary
.