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

Various extensions to JSONValue #68

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

patrickfreed
Copy link

This PR adds the following extensions to the JSONValue enum:

  • Conformances to various ExpressibleByXLiteral protocols (so e.g. let x: JSON = ["y": [ "z": [1, 2.2, false]]] is possible)
  • Conformance to the Codable protocol (if you want a loosely typed value as part of your Codable struct, for example)
  • Computed properties for failably unwrapping Swift types

@codecov
Copy link

codecov bot commented Jan 12, 2021

Codecov Report

Merging #68 (1b1c179) into main (122b945) will increase coverage by 0.28%.
The diff coverage is 84.52%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main      #68      +/-   ##
==========================================
+ Coverage   85.91%   86.19%   +0.28%     
==========================================
  Files          13       13              
  Lines        1271     1355      +84     
==========================================
+ Hits         1092     1168      +76     
- Misses        179      187       +8     
Impacted Files Coverage Δ
Sources/ExtrasJSON/JSONValue.swift 91.41% <84.52%> (-3.89%) ⬇️
...ON/Decoding/JSONSingleValueDecodingContainer.swift 100.00% <0.00%> (+3.17%) ⬆️
...ON/Encoding/JSONSingleValueEncodingContainer.swift 68.51% <0.00%> (+5.55%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 122b945...1b1c179. Read the comment docs.

Copy link

@t089 t089 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like these convenience accessors, but I would be very careful with the implementation of Codable for JSONValue. Especially the JSON number will only be lossy encoded and decoded.

case .number(let n):
if let int = self.intValue {
try container.encode(int)
} else if let double = self.doubleValue {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, I think this might introduce rounding errors. Because JSON represents numbers as String, you might loose precision when converting to Double and then back to String. Unfortunately, I don't think there is an elegant way to avoid floating point arithmetic when going through the Codable interface.

let container = try decoder.singleValueContainer()
if let int = try? container.decode(Int.self) {
self = .number(String(int))
} else if let double = try? container.decode(Double.self) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same for the decoding: going through a floating point number can introduce artifacts from floating point arithmetic.

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

Successfully merging this pull request may close these issues.

2 participants