Skip to content

Added JSON serialization mechanism #137

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

manavgabhawala
Copy link

Adds initial code for (almost) RFC compliant JSON serialization mechanism to convert JSON (in the form of AnyObject) to NSData. Works with PrettyPrinted option too.

return str.dataUsingEncoding(NSUTF8StringEncoding)!
}

private class func JSONSerialize(object: AnyObject, newIndent: String, newLine: String, space: String, indentation: String = "") throws -> String
Copy link
Contributor

Choose a reason for hiding this comment

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

object needs to be of type Any not AnyObject since this will not work for value types.

@parkera
Copy link
Contributor

parkera commented Dec 13, 2015

I think we'll also need some unit tests added for this before we accept it.

@manavgabhawala
Copy link
Author

@phausler fixed the AnyObject -> Any but I also added in code to handle NSDictionary and NSArray because otherwise these objects weren't being handled. Bridging between [AnyObject] and NSArray doesn't work in the current version and that needs to be fixed.
@parkera Added test cases to test serialization.
I ran the tests and it succeeded separately on Darwin but I wasn't able to build or run them on Linux.

@phausler
Copy link
Contributor

There seem to be a few compile failures with this still

oundation/NSJSONSerialization.swift:210:45: warning: cast from 'NSArray' to unrelated type '[AnyObject]' always fails
            return try serializeArray(array as! [AnyObject], newIndent: newIndent, newLine: newLine, space: space, indentation: indentation)
                                      ~~~~~ ^   ~~~~~~~~~~~
Foundation/NSJSONSerialization.swift:215:45: warning: cast from 'NSDictionary' to unrelated type '[String : AnyObject]' always fails
            return try serializeObject(dict as! [String: AnyObject], newIndent: newIndent, newLine: newLine, space: space, indentation: indentation)
                                       ~~~~ ^   ~~~~~~~~~~~~~~~~~~~

and the tests also fail to compile

TestFoundation/TestNSJSONSerialization.swift:425:16: error: value of type 'NSString' has no member '_swiftObject'
        return string._swiftObject
               ^~~~~~ ~~~~~~~~~~~~
TestFoundation/TestNSJSONSerialization.swift:461:24: error: type of expression is ambiguous without more context
            let json = ["a": 4.0, "b": NSNull(), "c": "string", "d": false]
                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TestFoundation/TestNSJSONSerialization.swift:487:45: error: contextual type 'Any' (aka 'protocol<>') cannot be used with array literal
            XCTAssertEqual(try trySerialize([NSNull(), "hello", 1.0]), "[null,\"hello\",1.0]")
                                            ^~~~~~~~~~~~~~~~~~~~~~~~
TestFoundation/TestNSJSONSerialization.swift:497:25: error: type of expression is ambiguous without more context
            let first = ["a": 4.0, "b": NSNull(), "c": "string", "d": false]
                        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TestFoundation/TestNSJSONSerialization.swift:498:26: error: type of expression is ambiguous without more context
            let second = [NSNull(), "hello", 1.0]
                         ^~~~~~~~~~~~~~~~~~~~~~~~
TestFoundation/TestNSJSONSerialization.swift:432:32: error: call can throw, but it is executed in a non-throwing autoclosure
            XCTAssertEqual(try trySerialize([String: Any]()), "{}")
                               ^
TestFoundation/TestNSJSONSerialization.swift:433:32: error: call can throw, but it is executed in a non-throwing autoclosure
            XCTAssertEqual(try trySerialize([String: NSNumber]()), "{}")
                               ^
TestFoundation/TestNSJSONSerialization.swift:434:32: error: call can throw, but it is executed in a non-throwing autoclosure
            XCTAssertEqual(try trySerialize([String: String]()), "{}")
                               ^
TestFoundation/TestNSJSONSerialization.swift:438:32: error: call can throw, but it is executed in a non-throwing autoclosure
            XCTAssertEqual(try trySerialize(json), "{}")
                               ^
TestFoundation/TestNSJSONSerialization.swift:440:9: warning: 'catch' block is unreachable because no errors are thrown in 'do' block
        catch {
        ^
TestFoundation/TestNSJSONSerialization.swift:449:32: error: call can throw, but it is executed in a non-throwing autoclosure
            XCTAssertEqual(try trySerialize(json), "{\"hello\":\"world\"}")
                               ^
TestFoundation/TestNSJSONSerialization.swift:452:32: error: call can throw, but it is executed in a non-throwing autoclosure
            XCTAssertEqual(try trySerialize(json), "{\"hello\":\"world\",\"swift\":\"is \\\\ \\\"awesome\\\"\"}")
                               ^
TestFoundation/TestNSJSONSerialization.swift:454:9: warning: 'catch' block is unreachable because no errors are thrown in 'do' block
        catch {
        ^
TestFoundation/TestNSJSONSerialization.swift:472:32: error: call can throw, but it is executed in a non-throwing autoclosure
            XCTAssertEqual(try trySerialize([String]()), "[]")
                               ^
TestFoundation/TestNSJSONSerialization.swift:473:32: error: call can throw, but it is executed in a non-throwing autoclosure
            XCTAssertEqual(try trySerialize([NSNumber]()), "[]")
                               ^
TestFoundation/TestNSJSONSerialization.swift:474:32: error: call can throw, but it is executed in a non-throwing autoclosure
            XCTAssertEqual(try trySerialize([Any]()), "[]")
                               ^
TestFoundation/TestNSJSONSerialization.swift:475:32: error: call can throw, but it is executed in a non-throwing autoclosure
            XCTAssertEqual(try trySerialize([AnyObject]()), "[]")
                               ^
TestFoundation/TestNSJSONSerialization.swift:477:9: warning: 'catch' block is unreachable because no errors are thrown in 'do' block
        catch {
        ^
TestFoundation/TestNSJSONSerialization.swift:485:32: error: call can throw, but it is executed in a non-throwing autoclosure
            XCTAssertEqual(try trySerialize(["hello", "swift⚡️"]), "[\"hello\",\"swift⚡️\"]")
TestFoundation/TestNSJSONSerialization.swift:486:32: error: call can throw, but it is executed in a non-throwing autoclosure
            XCTAssertEqual(try trySerialize([1.0, 3.3, 2.3]), "[1.0,3.3,2.3]")
                               ^

@copumpkin
Copy link
Contributor

plutil could also use this, right?

@phausler
Copy link
Contributor

phausler commented Jan 7, 2016

plutil should have json support but it does not yet have that part of the implementation

@copumpkin
Copy link
Contributor

@phausler yeah I understand. I just meant that when plutil grows support for JSON, it can use this code.

@parkera
Copy link
Contributor

parkera commented Jan 7, 2016

Yup, that's exactly what plutil on Darwin does.

@czechboy0
Copy link
Member

@manavgabhawala What's the status of this PR? I'm in need of this and would be happy to help out, however there haven't been any commits in two months.
Should I start anew or is this likely to get accepted?

@manavgabhawala
Copy link
Author

@czechboy0 Pushed changes to make it work with the latest swift build snapshot.
@phausler Please review. Thanks

@phausler
Copy link
Contributor

@swift-ci Please test

@manavgabhawala
Copy link
Author

@phausler one test case was failing because the ordering of the keys of the dictionary was different on Linux builds. But I updated the test cases so that the ordering of keys doesn't impact the result of the test cases. Please test again.

@phausler
Copy link
Contributor

@swift-ci Please test

atrick pushed a commit to atrick/swift-corelibs-foundation that referenced this pull request Jan 12, 2021
…orts

Remove `@testable import` and `-enable-testing`
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.

5 participants