Skip to content

JSONSerialization: Improve number parsing for JSON #2980

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

Conversation

spevans
Copy link
Contributor

@spevans spevans commented Feb 4, 2021

This is a potential solution for SR-7054 and SR-12244 involving parsing JSON numbers and then later bridging them to Decimal or a floating point type and losing accuray compared to the original input. It will need to be updated after #2966 is merge but that will be minimal changes. The bridging should also be marginally faster as it doesnt need to create a 2nd NSNumber for the equality check.

  • Add _NSJSONNumber, an internal subclass of NSNumber to provide lazy
    parsing of numbers at time of use. This avoids parsing numbers as
    Decimal when later bridged to Double/Float and vice-versa which
    has accuracy issues if converted from one to the other.

  • Add test cases for SR-7054 and SR-12244

@spevans
Copy link
Contributor Author

spevans commented Feb 4, 2021

@swift-ci test

@spevans spevans force-pushed the pr_json_number_parsing branch from 7a310cd to c93c307 Compare March 2, 2021 12:56
@spevans
Copy link
Contributor Author

spevans commented Mar 2, 2021

@swift-ci test

@spevans spevans force-pushed the pr_json_number_parsing branch 2 times, most recently from ab86b7a to 11685c3 Compare March 2, 2021 14:48
@spevans
Copy link
Contributor Author

spevans commented Mar 2, 2021

@swift-ci test

@@ -14,7 +14,7 @@ import SwiftFoundation
import Foundation
#endif
@_implementationOnly import CoreFoundation
@_implementationOnly import CFXMLInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

Unintentionally added during commit?

@@ -13,7 +13,7 @@ import SwiftFoundation
import Foundation
#endif
@_implementationOnly import CoreFoundation
@_implementationOnly import CFXMLInterface
Copy link
Contributor

Choose a reason for hiding this comment

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

Unintentionally added during commit?

@spevans spevans force-pushed the pr_json_number_parsing branch 2 times, most recently from c41803b to 25c8f39 Compare March 2, 2021 19:10
@spevans
Copy link
Contributor Author

spevans commented Mar 2, 2021

@swift-ci test

var fractionExponent: Int?
var exponentValue: Int?
var negativeExponent = false
var jsonString = ""
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks so much for moving your implementation into this parseNumber function! 👍 I guess the only thing left from my side here, is the creation of jsonString. In my point of view, we should only create the jsonString at the very end of the number parsing based on the numbers arraySlice.

self.makeStringFast(self[numberStartIndex ..< self.readerIndex])

Appending to a String especially with Character(UnicodeScalar(ascii)) is, if I recall correctly, very slow.

@spevans spevans force-pushed the pr_json_number_parsing branch from 25c8f39 to c173435 Compare March 3, 2021 11:36
@spevans
Copy link
Contributor Author

spevans commented Mar 3, 2021

@swift-ci test

@spevans spevans force-pushed the pr_json_number_parsing branch from c173435 to 953b800 Compare March 4, 2021 18:35
@spevans
Copy link
Contributor Author

spevans commented Mar 4, 2021

@swift-ci test

@spevans spevans force-pushed the pr_json_number_parsing branch from 953b800 to 7c223ec Compare March 4, 2021 22:29
@spevans
Copy link
Contributor Author

spevans commented Mar 4, 2021

@swift-ci test

- Add _JSONNumber and _NSJSONNumber, an internal subclass of NSNumber, to
  provide lazy parsing of numbers at time of use. This avoids parsing
  numbers as Decimal when later bridged to Double/Float and vice-versa
  which has accuracy issues if converted from one to the other.

- Add test cases for SR-7054 and SR-12244
@spevans spevans force-pushed the pr_json_number_parsing branch from 7c223ec to a5887b7 Compare March 5, 2021 11:17
@spevans
Copy link
Contributor Author

spevans commented Mar 5, 2021

@swift-ci test

@parkera
Copy link
Contributor

parkera commented Jul 19, 2024

Closing older PRs after re-core of swift-corelibs-foundation on swift-foundation (#5001).

@parkera parkera closed this Jul 19, 2024
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.

3 participants