Skip to content
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

[SR-5923] JSONSerialization cannot cast positive number to UInt #4064

Closed
swift-ci opened this issue Sep 18, 2017 · 4 comments
Closed

[SR-5923] JSONSerialization cannot cast positive number to UInt #4064

swift-ci opened this issue Sep 18, 2017 · 4 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-5923
Radar None
Original Reporter ylin (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Ubuntu 14.04, Swift 4 09-17 snapshot

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug
Assignee None
Priority Medium

md5: f28ac0e9fa50444fffe98dcea94a5965

Issue Description:

Ubuntu 14.04:

ylin@ylin-VirtualBox:~$ swift -I/home/ylin/.swiftenv/versions/4.0-DEVELOPMENT-SNAPSHOT-2017-09-17-a/usr/lib/swift/clang/include
Welcome to Swift version 4.0-dev (LLVM 2dedb62a0b, Clang b9d76a314c, Swift 56b2dc8eab). Type :help for assistance.
  1> import Foundation 
  2. let str = """ 
  3. { 
  4.     "id": 1111 
  5. } 
  6. """ 
  7. let obj = JSONSerialization.jsonObject(with: str.data(using: .utf8)!) 
  8.  
  9. (obj as! [String: Any])["id"] as! UInt64
Could not cast value of type 'Swift.Int' (0x7ffff7d81170) to 'Swift.UInt64' (0x7ffff7d80cc0).
Process 3169 stopped
* thread #​1, name = 'repl_swift', stop reason = signal SIGABRT
    frame #​0: 0x00007ffff6c8dc37 libc.so.6`__GI_raise(sig=6) at raise.c:56
Target 0: (repl_swift) stopped.
str: String = {
  _core = {
    _baseAddress = <extracting data from value failed>

    _countAndFlags = <extracting data from value failed>

    _owner = <extracting data from value failed>

  }
}
obj: Any = <extracting data from value failed>

Execution interrupted. Enter code to recover and continue.
Enter LLDB commands to investigate (type :help for assistance.)

macOS:

ylin@youming-mbpr:~/Swift/swift-html-entities$ swift
Welcome to Apple Swift version 4.0-dev (LLVM 2dedb62a0b, Clang b9d76a314c, Swift 56b2dc8eab). Type :help for assistance.
  1> import Foundation 
  2. let str = """ 
  3. { 
  4.     "id": 1111 
  5. } 
  6. """ 
  7. let obj = JSONSerialization.jsonObject(with: str.data(using: .utf8)!) 
  8.  
  9. (obj as! [String: Any])["id"] as! UInt64
$R0: UInt64 = 1111
str: String = "{\n    \"id\": 1111\n}"
obj: Any = {
  payload_data_0 = 0x00000001002086c0
  payload_data_1 = 0x0000000000000000
  payload_data_2 = 0x0000000000000000
  instance_type = 0x00000001014272c0 libswiftCore.dylib`InitialAllocationPool + 3072
}

First reported here: Kitura/Kitura-CredentialsGitHub#27

@belkadan
Copy link

cc @phausler

@belkadan
Copy link

My understanding is that this is just how JSONSerialization works on Linux. Rather than creating NSNumber intermediates, it creates Ints directly, and you can't use as! to go from one kind of integer to another in Swift. (Apple platforms add magic around NSNumber, but they still won't go from Int to UInt.)

@itaiferber
Copy link
Contributor

That's correct. You'll need to get the value out as either an `Int` or a `Double`, and convert it from there

@spevans
Copy link
Collaborator

spevans commented Jan 17, 2019

The as casting was fixed in 4.2 on Linux so this now works:

$ ~/swift-4.2.1-RELEASE-ubuntu18.04/usr/bin/swift
Welcome to Swift version 4.2.1 (swift-4.2.1-RELEASE). Type :help for assistance.
  1> import Foundation 
  2> let str = " { \"id\": 1111 } "
str: String = " { \"id\": 1111 } "
  3> let obj = JSONSerialization.jsonObject(with: str.data(using: .utf8)!) 
obj: [String : Any] = 1 key/value pair {
  [0] = {
    key = "id"
    value = {
      Foundation.NSValue = {
        Foundation.NSObject = {}
      }
      _base = {
        info = 265422848
        pad = 8388596
      }
      _pad = 36028745744779264
    }
  }
}
  4> (obj as! [String: Any])["id"] as! UInt64
$R0: UInt64 = 1111

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from swiftlang/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants