ZevEisenberg / UsefulDecode

Better error messing for JSONDecoder

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UsefulDecode

A quick sketch of a more useful error reporting experience for JSONDecoder.

Before:

valueNotFound(Swift.String, Swift.DecodingError.Context(codingPath: [_JSONKey(stringValue: "Index 0", intValue: 0), CodingKeys(stringValue: "address", intValue: nil), CodingKeys(stringValue: "city", intValue: nil), CodingKeys(stringValue: "birds", intValue: nil), _JSONKey(stringValue: "Index 1", intValue: 1), CodingKeys(stringValue: "name", intValue: nil)], debugDescription: "Expected String value but found null instead.", underlyingError: nil))

After:

Value not found: expected 'name' (String) at [0]/address/city/birds/[1]/name, got:
{
  "feathers" : "some",
  "name" : null
}

Usage

let decoder = JSONDecoder()
let value = try decoder.decodeWithBetterErrors(MyCoolType.self, from: someData)
// there's no step 3

About

Better error messing for JSONDecoder

License:MIT License


Languages

Language:Swift 100.0%