Skip to content

Conversation

@lickel
Copy link
Collaborator

@lickel lickel commented Oct 14, 2022

This changes the handling of JSON literals to force dictionary and array values to conform to a new JSONConvertible protocol.
This is also adds support for a non-optional initializer since it's known to safely convert.

The resulting JSON objects will be strongly typed and evaluated by the compiler.
They will have more guarantees than something like JSON([String: Any]) which lazily evaluates its children.

Performance

It will have a minor impact on performance in so far as the compiler can choose to do one of several things:

let n: JSON = [
    "simple": [1, 2, 3],
    "complex": [1, "a"],
]

The simple array could be compiled as:

  • [Int]
  • [JSON]
  • JSON

The complex array could be

  • [any JSONConvertible]
  • [JSON]
  • JSON

Depending on what the compiler decides to create, it may cost need it iterate and convert an array of primitives into a usable actual object.
This would cost extra allocations and runtime performance.

As long as we aren't creating massive JSON payloads, this shouldn't really be a problem.
Further, you could help the compiler by doing [1, "a"] as JSON

The alternative would be for Dictionary and Array to only conform to JSONConvertible if their values are JSON (as opposed to JSONConvertible.
That would mean that literal JSON types (at the second level) would only accept JSON instead of JSONConvertible.
That would force each tier of the structure to proactively convert to JSON; however, that means you can't add non-literal values to your array / dictionary.

You would be forced to do something like:

let myValue: UInt64 = 1

let n: JSON = [
    "simple": [1, 2, 3],
    "complex": [1, "a"],
    "extra": [myValue] as JSON,
]

@codecov-commenter
Copy link

codecov-commenter commented Oct 14, 2022

Codecov Report

Base: 94.13% // Head: 93.90% // Decreases project coverage by -0.22% ⚠️

Coverage data is based on head (923c8b5) compared to base (4b275ac).
Patch coverage: 73.78% of modified lines in pull request are covered.

Additional details and impacted files
@@               Coverage Diff               @@
##           feature/5.0      #43      +/-   ##
===============================================
- Coverage        94.13%   93.90%   -0.23%     
===============================================
  Files               30       30              
  Lines             4924     4958      +34     
===============================================
+ Hits              4635     4656      +21     
- Misses             289      302      +13     
Impacted Files Coverage Δ
FetchRequests/Sources/JSON.swift 84.16% <70.00%> (-2.91%) ⬇️
FetchRequests/Tests/Models/JSONTestCase.swift 97.81% <100.00%> (+0.08%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@lickel lickel merged commit 9cb0fb0 into feature/5.0 Oct 14, 2022
@lickel lickel deleted the json-conversion branch October 14, 2022 19:18
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.

4 participants