diff --git a/README.md b/README.md index 6c4ca86..0695353 100644 --- a/README.md +++ b/README.md @@ -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 ## @@ -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 } ``` diff --git a/Sources/AutoJSONSerialization/Models/DateProperty.swift b/Sources/AutoJSONSerialization/Models/DateProperty.swift index b242036..84d3783 100644 --- a/Sources/AutoJSONSerialization/Models/DateProperty.swift +++ b/Sources/AutoJSONSerialization/Models/DateProperty.swift @@ -2,6 +2,6 @@ import Foundation struct DateProperty: AutoJSONDeserializable, AutoJSONSerializable { let date: Date - // sourcery: keyName = "optional_date" + // sourcery: JSONKey = "optional_date" let optionalDate: Date? } diff --git a/Sources/AutoJSONSerialization/Models/JSONDeserializableProperty.swift b/Sources/AutoJSONSerialization/Models/JSONDeserializableProperty.swift index 89c8437..aeec538 100644 --- a/Sources/AutoJSONSerialization/Models/JSONDeserializableProperty.swift +++ b/Sources/AutoJSONSerialization/Models/JSONDeserializableProperty.swift @@ -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 { diff --git a/Sources/AutoJSONSerialization/Models/SinglePropertyWithKeyPathAnnotation.swift b/Sources/AutoJSONSerialization/Models/SinglePropertyWithKeyPathAnnotation.swift index dd63801..db0c0f9 100644 --- a/Sources/AutoJSONSerialization/Models/SinglePropertyWithKeyPathAnnotation.swift +++ b/Sources/AutoJSONSerialization/Models/SinglePropertyWithKeyPathAnnotation.swift @@ -1,4 +1,4 @@ struct SinglePropertyWithKeyPathAnnotation: AutoJSONDeserializable, AutoJSONSerializable { - // sourcery: keyName = "label" + // sourcery: JSONKey = "label" let name: String } diff --git a/Templates/AutoJSONDeserializable.stencil b/Templates/AutoJSONDeserializable.stencil index c49da96..a4a3c47 100644 --- a/Templates/AutoJSONDeserializable.stencil +++ b/Templates/AutoJSONDeserializable.stencil @@ -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 %} diff --git a/Templates/AutoJSONSerializable.stencil b/Templates/AutoJSONSerializable.stencil index 36e8972..0eb38fe 100644 --- a/Templates/AutoJSONSerializable.stencil +++ b/Templates/AutoJSONSerializable.stencil @@ -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 }