Skip to content

Individually selectable standard transformers#214

Merged
pcantrell merged 4 commits intoswift-4from
standard-transformers
Aug 7, 2017
Merged

Individually selectable standard transformers#214
pcantrell merged 4 commits intoswift-4from
standard-transformers

Conversation

@pcantrell
Copy link
Copy Markdown
Member

@pcantrell pcantrell commented Aug 7, 2017

Problem

Siesta parses JSON by default, using Foundation’s JSONSerialization to output dictionaries / arrays. Swift 4’s straight-to-model JSON coding is the happier, better future — but it expects raw Data, not dictionaries. Apps wanting to use the new JSON coding will thus want Siesta’s default JSON parsing disabled. However, they still likely want its default text and image handling.

Until now, Siesta’s built-in JSON/image/text parsing has been controlled by Service.init’s useDefaultTransformers:, which is an all-or-nothing proposition. It’s not easy to opt in to only image and text but not JSON. (It’s possible, but it takes quite a hairball of config code.)

Solution

This PR adds support for individually selecting default transformers when creating a service:

let service = Service(
    baseURL: "https://example.com",
    standardTransformers: [.text, .image])  // no JSON

Services can then easily use Swift 4 JSON coding:

let jsonDecoder = JSONDecoder()

service.configureTransformer("/users/*") {
    try jsonDecoder.decode(User.self, from: $0.content)
}

The older useDefaultTransformers: is now deprecated in favor of standardTransformers:.

The GithubBrowser example project is updated in this PR to use JSON coding. Its example support for SwiftyJSON has moved to Extensions/.

@pcantrell pcantrell merged commit ee53b6f into swift-4 Aug 7, 2017
@pcantrell pcantrell deleted the standard-transformers branch August 7, 2017 04:36
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.

1 participant