Skip to content

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

Merged
merged 1 commit into from
Mar 4, 2021

Conversation

fabianfett
Copy link
Contributor

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 and NSDictionary.

@spevans
Copy link
Contributor

spevans commented Mar 2, 2021

@swift-ci test

@@ -737,3 +701,48 @@ private extension JSONValue {
}
}
}

extension NSNumber {
static func fromJSONNumber(_ string: String) -> NSNumber? {
Copy link
Contributor

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)).

Copy link
Contributor Author

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).

Copy link
Contributor

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

@millenomi
Copy link
Contributor

@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.

@drexin
Copy link
Contributor

drexin commented Mar 2, 2021

@swift-ci test

@benlangmuir
Copy link
Contributor

swiftlang/sourcekit-lsp#372
@swift-ci please test

@spevans
Copy link
Contributor

spevans commented Mar 3, 2021

@swift-ci test

@drexin
Copy link
Contributor

drexin commented Mar 3, 2021

@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 {
Copy link
Contributor

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)?

Copy link
Contributor Author

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.

Copy link
Contributor

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...

Copy link
Contributor

@bendjones bendjones left a 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

@millenomi
Copy link
Contributor

Merging pending test passes.

//
// Created by Fabian Fett on 02.03.21.
// Copyright © 2021 Swift. All rights reserved.
//
Copy link

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?

Copy link
Contributor Author

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.

@drexin
Copy link
Contributor

drexin commented Mar 3, 2021

@swift-ci test


if let double = Double(numberString) {
return Decimal(double)
}
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

@spevans
Copy link
Contributor

spevans commented Mar 4, 2021

@swift-ci test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants