Skip to content

Commit

Permalink
Rename keyName annotation to JSONKey
Browse files Browse the repository at this point in the history
Fixes issue #1
  • Loading branch information
Liquidsoul committed Apr 25, 2017
1 parent 0eaeea1 commit 624cfad
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 10 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ Making it conform to AutoJSONDeserializable would result make it support this JS
* ISO8601 formatted dates
* optionals
* nested structures
* JSON keyName annotation
* JSONKey annotation

## Annotations ##

Expand All @@ -102,14 +102,14 @@ Let's say you have this JSON:
```

As you can see, the naming convention does not follow camel case. To fix this you can use Sourcery annotations.
On a property, you can define the `keyName` attribute with the JSON name you want to bind the property to:
On a property, you can define the `JSONKey` attribute with the JSON name you want to bind the property to:

``` swift
struct Contact {
let id: String
// sourcery: keyName = "first_name"
// sourcery: JSONKey = "first_name"
let firstName: String
// sourcery: keyName = "last_name"
// sourcery: JSONKey = "last_name"
let lastName: String
}
```
Expand Down
2 changes: 1 addition & 1 deletion Sources/AutoJSONSerialization/Models/DateProperty.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import Foundation

struct DateProperty: AutoJSONDeserializable, AutoJSONSerializable {
let date: Date
// sourcery: keyName = "optional_date"
// sourcery: JSONKey = "optional_date"
let optionalDate: Date?
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ struct JSONDeserializableProperty: AutoJSONDeserializable, AutoJSONSerializable
let entity: Entity
let optionalEntity: Entity?
let nilEntity: Entity?
// sourcery: keyName = "annotated_entity"
// sourcery: JSONKey = "annotated_entity"
let annotatedEntity: Entity
// sourcery: keyName = "optional_annotated_entity"
// sourcery: JSONKey = "optional_annotated_entity"
let optionalAnnotatedEntity: Entity?

struct Entity: AutoJSONDeserializable, AutoJSONSerializable {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
struct SinglePropertyWithKeyPathAnnotation: AutoJSONDeserializable, AutoJSONSerializable {
// sourcery: keyName = "label"
// sourcery: JSONKey = "label"
let name: String
}
2 changes: 1 addition & 1 deletion Templates/AutoJSONDeserializable.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension {{ type.name }}: JSONDeserializable {
{{ type.accessLevel }} init?(JSONObject: [String: Any]) {
{% macro Optional arg %}guard {{ arg }} else { return nil }{% endmacro %}
{% for variable in type.storedVariables %}
{% set Assignment %}{% if variable.type.implements.AutoJSONDeserializable %}let {{ variable.name }} = (JSONObject["{{ variable.annotations.keyName|default:variable.name }}"] as? [String: Any]).flatMap({{ variable.unwrappedTypeName }}.init(JSONObject:)){% else %}{% if variable.unwrappedTypeName == "Date" %}let {{ variable.name }} = (JSONObject["{{ variable.annotations.keyName|default:variable.name }}"] as? String).flatMap(JSONDateFormatter.date(from:)){% else %}let {{ variable.name }} = JSONObject["{{ variable.annotations.keyName|default:variable.name }}"] as? {{ variable.unwrappedTypeName }}{% endif %}{% endif %}{% endset %}
{% set Assignment %}{% if variable.type.implements.AutoJSONDeserializable %}let {{ variable.name }} = (JSONObject["{{ variable.annotations.JSONKey|default:variable.name }}"] as? [String: Any]).flatMap({{ variable.unwrappedTypeName }}.init(JSONObject:)){% else %}{% if variable.unwrappedTypeName == "Date" %}let {{ variable.name }} = (JSONObject["{{ variable.annotations.JSONKey|default:variable.name }}"] as? String).flatMap(JSONDateFormatter.date(from:)){% else %}let {{ variable.name }} = JSONObject["{{ variable.annotations.JSONKey|default:variable.name }}"] as? {{ variable.unwrappedTypeName }}{% endif %}{% endif %}{% endset %}
{% if variable.isOptional %}
{{ Assignment }}
{% else %}
Expand Down
2 changes: 1 addition & 1 deletion Templates/AutoJSONSerializable.stencil
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ extension {{ type.name }}: JSONSerializable {
var jsonObject = [String: Any]()
{% for variable in type.storedVariables %}
let {{ variable.name }} = self.{{ variable.name }}{% if variable.type.implements.AutoJSONSerializable %}{% if variable.isOptional %}?{%endif%}.toJSONObject(){% else %}{%if variable.unwrappedTypeName == "Date" %}{% if variable.isOptional %}?{%endif%}.iso8601String(){%endif%}{% endif %}
jsonObject["{{ variable.annotations.keyName|default:variable.name }}"] = {{ variable.name }}
jsonObject["{{ variable.annotations.JSONKey|default:variable.name }}"] = {{ variable.name }}
{% endfor %}
return jsonObject
}
Expand Down

0 comments on commit 624cfad

Please sign in to comment.