not sure if this fits into a “pitch” or a “proposal”, or something in-between, but here goes nothing:
motivation
JSON is one of the most common formats to exchange structured data as over a network, but right now we’re limited to using the JSONDecoder vended by Foundation, either directly via a Foundation import, or indirectly through Vapor. JSONDecoder also struggles with decimal values, as it parses them all as floating point, which can lead to data corruption.
proposed library
json
is a git submodule that implements JSON parsing in pure Swift. It’s also integrated with Decodable
!
sample program:
@main enum Main
{
struct Decimal:Codable
{
let units:Int
let places:Int
}
struct Number:Codable
{
let success:Bool
let value:Decimal
}
static
func main() throws
{
let string:String =
"""
{"success":true,"value":0.1}
"""
let decoder:JSON.Decoder =
try Grammar.parse(string, as: JSON.Decoder.self)
let number:Number = try .init(from: decoder)
}
}
json
depends on grammar
, so you need to have both submodules added for the example to compile.
interested in knowing what you all think, and where to go from here!