Giter Site home page Giter Site logo

swift-extras / swift-extras-json Goto Github PK

View Code? Open in Web Editor NEW
350.0 350.0 15.0 223 KB

JSON encoding and decoding without the use of Foundation in pure Swift.

License: Apache License 2.0

Swift 86.38% Shell 9.04% C 4.03% Objective-C 0.55%
json swift swift-json swift-server

swift-extras-json's People

Contributors

fabianfett avatar gwynne avatar helje5 avatar ktoso avatar pokryfka avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

swift-extras-json's Issues

Maintaining precision with numbers

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?

Typo `with` instead of `width`

There is a typo decodeFixedWithInteger() instead of Width in: Sources/PureSwiftJSONCoding/Decoding/JSONSingleValueDecodingContainer.swift

Shouldn't PSJSONDecoder.userInfo be public?

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

Performance Question

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?

Why does use of this API require conformance to Encodable?

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?

JSONSingleValueEncodingContainer.encode<T> isn't implemented

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)

JSONEncoder hasn't implemented nestedContainer/nestedUnkeyedContainer

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)

JSON5

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?).

Allow decoding Decimal from regular numbers

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.