{"id":17065995,"url":"https://github.com/lachlanmckee/gsonpath-extensions","last_synced_at":"2025-04-12T18:46:29.788Z","repository":{"id":57737337,"uuid":"84700968","full_name":"LachlanMcKee/gsonpath-extensions","owner":"LachlanMcKee","description":"An extension library for Gsonpath that adds Android Support Library annotation client-side validation","archived":false,"fork":false,"pushed_at":"2018-11-19T16:03:19.000Z","size":129,"stargazers_count":8,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-26T13:04:45.242Z","etag":null,"topics":["adapter-factory","android","annotation-processor","annotation-validation","annotations","gson","gson-path","java","json","kotlin"],"latest_commit_sha":null,"homepage":null,"language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/LachlanMcKee.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-12T04:55:58.000Z","updated_at":"2019-06-20T04:49:53.000Z","dependencies_parsed_at":"2022-08-24T14:59:41.606Z","dependency_job_id":null,"html_url":"https://github.com/LachlanMcKee/gsonpath-extensions","commit_stats":null,"previous_names":["lachlanmckee/gsonpath-extensions-android"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LachlanMcKee%2Fgsonpath-extensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LachlanMcKee%2Fgsonpath-extensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LachlanMcKee%2Fgsonpath-extensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/LachlanMcKee%2Fgsonpath-extensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/LachlanMcKee","download_url":"https://codeload.github.com/LachlanMcKee/gsonpath-extensions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248617561,"owners_count":21134194,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["adapter-factory","android","annotation-processor","annotation-validation","annotations","gson","gson-path","java","json","kotlin"],"created_at":"2024-10-14T11:05:34.814Z","updated_at":"2025-04-12T18:46:29.768Z","avatar_url":"https://github.com/LachlanMcKee.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Gson Path Extensions\n\nAn extension library for Gson Path that adds validation that does not exist in the core Gson Path library. The library supports [Android Support Library](https://developer.android.com/reference/android/support/annotation/package-summary.html) annotations (without including the dependency), but also includes the `FloatRange`, `IntRange` and `Size` for non-Android projects.\n* This library currently supports validation for `@FloatRange`, `@IntRange`, `@StringDef` and `@IntDef`.\n* The `@EmptyToNull` annotation bundled in the library will convert empty strings, arrays, collections or maps to null.\n   * If the value marked with `@EmptyToNull` is also marked with a `@NonNull` (or similar) annotation, it will throw an exception.\n\n## FloatRange example\nThe following is an example of `@FloatRange` annotation validation.\n\n### Model\n```java\n@AutoGsonAdapter\ninterface FloatModel extends BaseFloatModel {\n    @FloatRange(from = 0, to = 5, fromInclusive = false, toInclusive = false)\n    Float getValue();\n}\n```\n\n### Generated validation\n```java\n// Gsonpath Extensions\nif (value_value != null) {\n\n    // Extension - 'FloatRange' Annotation\n    if (value_value \u003c= 0.0) {\n        throw new com.google.gson.JsonParseException(\"Invalid 'from' range for value. Expected: '\u003e 0.0', Found '\" + value_value + \"'\");\n    }\n    if (value_value \u003e= 5.0) {\n        throw new com.google.gson.JsonParseException(\"Invalid 'to' range for value. Expected: '\u003c 5.0', Found '\" + value_value + \"'\");\n    }\n}\n```\n\n## IntRange example\nThe following is an example of `@IntRange` annotation validation.\n\n### Model\n```java\n@AutoGsonAdapter\ninterface IntModel extends BaseModel\u003cInteger\u003e {\n    @IntRange(from = 0, to = 5)\n    Integer getValue();\n}\n```\n\n### Generated validation\n```java\n// Gsonpath Extensions\nif (value_value != null) {\n    // Extension - 'IntRange' Annotation\n    if (value_value \u003c 0) {\n        throw new com.google.gson.JsonParseException(\"Invalid 'from' range for value. Expected: '\u003e= 0', Found '\" + value_value + \"'\");\n    }\n    if (value_value \u003e 5) {\n        throw new com.google.gson.JsonParseException(\"Invalid 'to' range for value. Expected: '\u003c= 5', Found '\" + value_value + \"'\");\n    }\n}\n```\n\n## Size example\nThe following is an example of `@Size` annotation validation.\n\n### Model\n```java\n@AutoGsonAdapter\ninterface ArrayModel extends BaseArrayModel {\n    @Size(min = 0, max = 6, multiple = 2, value = 2)\n    Integer[] getValue();\n}\n```\n\n### Generated validation\n```java\n// Gsonpath Extensions\nif (value_value != null) {\n    // Extension - 'Size' Annotation\n    if (value_value.length != 2) {\n        throw new com.google.gson.JsonParseException(\"Invalid array length for field 'value'. Expected length: '2', actual length: '\" + value_value.length + \"'\");\n    }\n    if (value_value.length \u003c 0) {\n        throw new com.google.gson.JsonParseException(\"Invalid array length for field 'value'. Expected minimum: '0', actual minimum: '\" + value_value.length + \"'\");\n    }\n    if (value_value.length \u003e 6) {\n        throw new com.google.gson.JsonParseException(\"Invalid array length for field 'value'. Expected maximum: '6', actual maximum: '\" + value_value.length + \"'\");\n    }\n    if (value_value.length % 2 != 0) {\n        throw new com.google.gson.JsonParseException(\"Invalid array length for field 'value'. length of '\" + value_value.length + \"' is not a multiple of 2\");\n    }\n}\n```\n\n## IntDef example\nThe following is an example of `@IntDef` annotation validation.\n\n### Model\n```java\n@AutoGsonAdapter\ninterface IntDefModel {\n    @IntDefExample\n    Integer getValue();\n    \n    @Retention(RetentionPolicy.SOURCE)\n    @IntDef({VALUE_1, VALUE_2})\n    @interface IntDefExample {\n        int VALUE_1 = 1;\n        int VALUE_2 = 2;\n    }\n}\n```\n\n### Generated validation\n```java\n// Gsonpath Extensions\nif (value_value != null) {\n    // Extension - 'Int Def' Annotation\n    switch (value_value) {\n        case 1:\n        case 2:\n            break;\n        \n        default:\n            throw new com.google.gson.JsonParseException(\"Unexpected Int '\" + value_value + \"' for field 'value'\");\n    }\n}\n```\n\n\n## StringDef example\nThe following is an example of `@StringDef` annotation validation. Note that the switch statement reassigns the string value to the actual constant from the `@StringDef` annotation. This is to appease the support library linting rules in Android Studio.\n\n### Model\n```java\n@AutoGsonAdapter\ninterface StringDefModel {\n    @StringDefExample\n    String getValue();\n    \n    @Retention(RetentionPolicy.SOURCE)\n    @StringDef({VALUE_1, VALUE_2})\n    @interface StringDefExample {\n        String VALUE_1 = \"1\";\n        String VALUE_2 = \"2\";\n    }\n}\n```\n\n### Generated validation\n```java\n// Gsonpath Extensions\nif (value_value != null) {\n    // Extension - 'String Def' Annotation\n    switch (value_value) {\n        case StringDefExample.VALUE_1:\n            value_value = StringDefExample.VALUE_1;\n            break;\n        \n        case StringDefExample.VALUE_2:\n            value_value = StringDefExample.VALUE_2;\n            break;\n        \n        default:\n            throw new com.google.gson.JsonParseException(\"Unexpected String '\" + value_value + \"' for field 'value'\");\n    }\n}\n```\n\n\n## EmptyToNull example\nThe following is an example of `@EmptyToNull` annotation validation.\n\n### Model\n```java\n@AutoGsonAdapter\ninterface StringDefModel {\n    @EmptyToNull\n    String getNullableString();\n    \n    @EmptyToNull\n    String[] getNullableArray();\n    \n    @EmptyToNull\n    Collection\u003cString\u003e getNullableCollection();\n    \n    @EmptyToNull\n    Map\u003cString, String\u003e getNullableMap();\n    \n    @NonNull\n    @EmptyToNull\n    String getNonNullString();\n    \n    @NonNull\n    @EmptyToNull\n    String[] getNonNullArray();\n    \n    @NonNull\n    @EmptyToNull\n    Collection\u003cString\u003e getNonNullCollection();\n    \n    @NonNull\n    @EmptyToNull\n    Map\u003cString, String\u003e getNonNullMap();\n}\n```\n\n### Generated validation (paraphrased)\n```java\n// Extension - 'EmptyToNull' Annotation\nif (nullableString.trim().length() == 0) {\n    nullableString = null;\n}\nif (nullableArray.length == 0) {\n    nullableArray = null;\n}\nif (nullableCollection.size() == 0) {\n    nullableCollection = null;\n}\nif (nullableMap.size() == 0) {\n    nullableMap = null;\n}\nif (nonNullString.trim().length() == 0) {\n    throw new com.google.gson.JsonParseException(\"JSON element 'nonNullString' cannot be blank\");\n}\nif (nonNullArray.length == 0) {\n    throw new com.google.gson.JsonParseException(\"JSON element 'nonNullArray' cannot be blank\");\n}\nif (nonNullCollection.size() == 0) {\n    throw new com.google.gson.JsonParseException(\"JSON element 'nonNullCollection' cannot be blank\");\n}\nif (nonNullMap.size() == 0) {\n    throw new com.google.gson.JsonParseException(\"JSON element 'nonNullMap' cannot be blank\");\n}\n```\n\n## Download\nThis library is available on Maven, you can add it to your project using the following gradle dependencies:\n\n```gradle\napt 'net.lachlanmckee:gsonpath-extensions:1.1.0'\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flachlanmckee%2Fgsonpath-extensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flachlanmckee%2Fgsonpath-extensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flachlanmckee%2Fgsonpath-extensions/lists"}