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

Add a convenience function to decode the Message's json data #9

Merged
merged 3 commits into from
Aug 8, 2023

Conversation

svara
Copy link
Collaborator

@svara svara commented Aug 8, 2023

This PR is adds a convenience function to easily decode the Message's json data.
In addition, it also exposes a JsonDataDecoder structure, where you can set a custom json decoder.
This is useful to converts snake-case keys to camel-case keys.

struct OlympicEventResult: Codable {
    var goldWinner: String
    var silverWinner: String
    var bronzeWinner: String
}

let json = """
{
    "silver_winner": "Sound",
    "gold_winner": "Light",
    "bronze_winner": "Unladen Swallow"
}
"""

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
JsonDataDecoder.appDecoder = decoder

@svara svara requested a review from jayohms August 8, 2023 10:48
Copy link
Collaborator

@jayohms jayohms left a comment

Choose a reason for hiding this comment

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

Looks great, nice addition 👍

Tests/MessageTests.swift Show resolved Hide resolved
@svara svara merged commit d239819 into main Aug 8, 2023
1 check passed
@svara svara deleted the json-data-decoding-convenience branch August 8, 2023 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants