Skip to content
This repository has been archived by the owner on Nov 11, 2017. It is now read-only.

Commit

Permalink
Merge pull request #17 from mpurland/master
Browse files Browse the repository at this point in the history
Fixed JSONDecoder for array and object types when using JSONValue encodeEither
  • Loading branch information
mpurland committed Jan 25, 2016
2 parents e1c8fc5 + 00c6fa2 commit b6e63c9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Tyro/JSONValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ extension JSONValue : JSONValueable {

public var anyObject : AnyObject? {
switch self {
case .Array(let values): return values as? AnyObject
case .Object(let value): return value as? AnyObject
case .Array(let values): return values.mapMaybe { $0.anyObject }
case .Object(let value): return value.mapMaybe { $0.anyObject }
case .String(let s): return s
case .Number(let n): return n
case .Null: return NSNull()
Expand Down
28 changes: 28 additions & 0 deletions TyroTests/EncoderSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,19 @@ class EncoderSpec : XCTestCase {
let result = JSONEncoder.encoder.encodeEither(array)
XCTAssertNotNil(result)
XCTAssert(result.right == JSONValue.Array([.String("a"), .String("b"), .String("c")]))
let jsonString = result.right!.toJSONString()!
print("testEncodeArray: \(jsonString)")
XCTAssertEqual("[\"a\",\"b\",\"c\"]", jsonString)
}

func testEncodeObject() {
let object = ["key": "value"]
let result = JSONEncoder.encoder.encodeEither(object)
XCTAssertNotNil(result)
XCTAssert(result.right == JSONValue.Object(["key": .String("value")]))
let jsonString = result.right!.toJSONString()!
print("testEncodeObject: \(jsonString)")
XCTAssertEqual("{\"key\":\"value\"}", jsonString)
}

func testEncodeString() {
Expand All @@ -39,6 +45,28 @@ class EncoderSpec : XCTestCase {
XCTAssert(result.right == .Number(number))
}

func testEncodeEmbeddedObjects() {
let object = ["number": 42, "array": [1, 2, 3], "object": ["strings": ["1", "2", "3"], "pi": 3.14159], "string": "hello"]
let result = JSONEncoder.encoder.encodeEither(object)
XCTAssertNotNil(result)

switch result.right! {
case .Object(let v):
XCTAssert(result.right == .Object(v))
XCTAssert(v["number"] == .Number(42))
XCTAssert(v["string"] == .String("hello"))
XCTAssert(v["array"] == .Array([.Number(1), .Number(2), .Number(3)]))
XCTAssert(v["object"] == .Object(["strings": .Array([.String("1"), .String("2"), .String("3")]), "pi": .Number(3.14159)]))
let jsonString = result.right!.toJSONString()!

// Ensure the decoded json string matches the original encoded value
let decodedValueFromJson = JSONValue.decodeEither(jsonString).right!
XCTAssertEqual(decodedValueFromJson, result.right!)
default:
XCTFail("Could not encode to JSONValue.Object")
}
}

func testEncodeError() {
let result = JSONEncoder.encoder.encodeEither(JSONEncoder.encoder)
XCTAssertNotNil(result)
Expand Down

0 comments on commit b6e63c9

Please sign in to comment.