Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does use of this API require conformance to Encodable? #64

Open
brianreinhold opened this issue Sep 11, 2020 · 8 comments
Open

Why does use of this API require conformance to Encodable? #64

brianreinhold opened this issue Sep 11, 2020 · 8 comments

Comments

@brianreinhold
Copy link

brianreinhold commented Sep 11, 2020

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?

@fabianfett
Copy link
Collaborator

Hi @brianreinhold, thanks for bringing this up:

I thought the whole point of this library was that one DIDN'T need to use Encodable and Decodable?

The point of the library is to create an alternative to JSONEncoder and JSONDecoder from Foundation. Since the encoder and decoder work with the Codable protocol, my implementation does the same. (Though faster!)

How would I encode a struct like this (with some values of course)

You could manually create a JSONValue from your structure.

See https://github.com/fabianfett/pure-swift-json/blob/main/Sources/PureSwiftJSON/JSONValue.swift

If you want to encode this structure you can:

let value: JSONValue = .object([
  "title": .string("foo bar"),
  "value": .number("1.000")
])

var result = [UInt8]()
value. appendBytes(to: result)

Since the numbers are encoded internally as a string you will continue to have the decimal precision. Does this fix your problem?

@brianreinhold
Copy link
Author

brianreinhold commented Sep 13, 2020 via email

@fabianfett
Copy link
Collaborator

Great, can this issue be closed?

@brianreinhold
Copy link
Author

brianreinhold commented Sep 14, 2020 via email

@fabianfett
Copy link
Collaborator

@brianreinhold sadly there is none at the moment, but I'd be happy to review a pr that addresses this shortcoming.

@brianreinhold
Copy link
Author

brianreinhold commented Sep 14, 2020 via email

@fabianfett
Copy link
Collaborator

What you're trying to achieve is possible, but it won't work with Apples encoder anymore and you'll need to copy and paste my encoder into your code base to make those adjustments. Would you mind sharing your Float type here?

@brianreinhold
Copy link
Author

brianreinhold commented Sep 14, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants