swift-extras / swift-extras-json Goto Github PK
View Code? Open in Web Editor NEWJSON encoding and decoding without the use of Foundation in pure Swift.
License: Apache License 2.0
JSON encoding and decoding without the use of Foundation in pure Swift.
License: Apache License 2.0
This is more of a question than a bug. I am trying to find a way to encode a decimal value with the proper precision. For example, with a precision of 1/1000, I want to encode 2.000 and not 2 or 2.0. The easy way would be to encode it as a string, but the FHIR spec requires that it be encoded as a number.
In Java I had access to a minimal JSON library where I had the option to encode the string sequence that would appear in the value even though the value itself was representing an integer.
In any case, is there a way I could encode a float. double, or Decimal value with the correct precision? What would it take to do it?
There is a typo decodeFixedWithInteger()
instead of Width
in: Sources/PureSwiftJSONCoding/Decoding/JSONSingleValueDecodingContainer.swift
This seems like it would want to be public:
public struct PSJSONDecoder {
@usableFromInline var userInfo: [CodingUserInfoKey: Any] = [:]
since I need to set some context into user info before i kick off decoding
https://github.com/apple/swift-nio/blob/master/docs/debugging-allocations.md
Thanks for this great project!
In the README, you state "It offers a significant performance improvement compared to the Foundation implementation on Linux.". While that's great as is, I wonder whether it could be beneficial to use this library also on Apple platforms.
Did you – by any chance – compare performance with their builtin JSON coding yet?
I thought the whole point of this library was that one DIDN'T need to use Encodable and Decodable? I am trying desperately to solve the problem of precision in floating point numbers and its the Encodable/Decodable requirement is the cause of the problem.
How would I encode a struct like this (with some values of course)
struct TestIt
{
var title: String
var value: Float
}
to a JSON String that can be sent on the wire without specifying conformance to Encodable?
The following code returns null
when it should return {"firstName":"Adam","surname":"Fowler"}
. This is caused by JSONSingleValueEncodingContainer.encode not encoding anything.
struct Name: Encodable {
var firstName: String
var surname: String
}
struct Object: Encodable {
var name: Name
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(name)
}
}
let object = Object(name: Name(firstName: "Adam", surname: "Fowler"))
let json = try JSONEncoder().encode(object)
In the following situation it causes the encoder to crash in JSONKeyedEncodingContainer.encode as it is expecting a value back but nothing is returned.
@propertyWrapper
struct Coding<Value: Encodable>: Encodable {
var wrappedValue: Value
init(wrappedValue: Value) {
self.wrappedValue = wrappedValue
}
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(wrappedValue)
}
}
struct Object: Encodable {
@Coding var name: String
}
let object = Object(name: "Adam Fowler")
let json = try PureSwiftJSONCoding.JSONEncoder().encode(object)
The following code produces a Fatal error: unimplemented
error when container.nestedContainer
is called. Looking at the code I can see also nestedUnkeyedContainer isn't implemented.
import PureSwiftJSONCoding // fabianfett/pure-swift-json
struct Object: Encodable {
let firstName: String
let surname: String
init(firstName: String, surname: String) {
self.firstName = firstName
self.surname = surname
}
func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
var nameContainer = container.nestedContainer(keyedBy: NameCodingKeys.self, forKey: .name)
try nameContainer.encode(firstName, forKey: .firstName)
try nameContainer.encode(surname, forKey: .surname)
}
private enum CodingKeys: String, CodingKey {
case name = "name"
}
private enum NameCodingKeys: String, CodingKey {
case firstName = "firstName"
case surname = "surname"
}
}
let object = Object(firstName: "Adam", surname: "Fowler")
let json = try JSONEncoder().encode(object)
It would be great, if this library could eventually support JSON5, which comes with a bundle of amazing features.
Foundation on macOS supports it by now (through .json5Allowed
), but it looks like swift-corelibs-foundation doesn't (yet?).
Decimals are originally meant to be encoded/decoded as a dictionary: https://github.com/apple/swift/blob/3978d81c840c899bd090243d7d344f8dceacd74b/stdlib/public/Darwin/Foundation/Decimal.swift#L118
Apple's JSONDecoder provides a way to decode them from single numbers but sadly numbers are parsed as Double by JSONSerialization, so it loses precision: https://github.com/apple/swift/blob/3978d81c840c899bd090243d7d344f8dceacd74b/stdlib/public/Darwin/Foundation/JSONEncoder.swift#L2464
Since pure-swift-json parses numbers as string, we could provide a similar way of parsing Decimal from regular numbers. As far as I can understand, JSONSingleValueDecodingContainter would need a special decode function for Decimal.Type that would work similarly to the second link.
Decimal's init also takes a Locale, it should be allowed to be customized and the default should probably be "en_US".
Most likely a separate issue but there should also be an option for encoding Decimals the same way instead of using the default method.
The coding path stack is not updated as you create/delete containers.
We need more tests for the encoder. It's test coverage is rather bad right now:
https://codecov.io/gh/fabianfett/pure-swift-json/tree/master/Sources/PureSwiftJSONCoding/Encoding
PRs are very welcome.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.