{"id":19260674,"url":"https://github.com/evant/gsonvalue","last_synced_at":"2025-06-30T03:33:32.298Z","repository":{"id":57736736,"uuid":"52184201","full_name":"evant/gsonvalue","owner":"evant","description":"Compile-time generation of gson TypeAdapters to preserve class encapsulation","archived":false,"fork":false,"pushed_at":"2020-06-14T22:48:25.000Z","size":222,"stargazers_count":42,"open_issues_count":4,"forks_count":5,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-21T16:43:03.536Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/evant.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2016-02-21T00:58:09.000Z","updated_at":"2023-07-16T18:09:48.000Z","dependencies_parsed_at":"2022-08-24T14:57:22.353Z","dependency_job_id":null,"html_url":"https://github.com/evant/gsonvalue","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/evant/gsonvalue","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evant%2Fgsonvalue","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evant%2Fgsonvalue/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evant%2Fgsonvalue/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evant%2Fgsonvalue/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/evant","download_url":"https://codeload.github.com/evant/gsonvalue/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evant%2Fgsonvalue/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262704356,"owners_count":23351050,"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":[],"created_at":"2024-11-09T19:22:26.775Z","updated_at":"2025-06-30T03:33:32.272Z","avatar_url":"https://github.com/evant.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GsonValue\n\nCompile-time generation of gson TypeAdapters to preserve class encapsulation. \n\nBy default, gson uses reflection to read and write fields of you class from json. The problem with\nthis is that it breaks encapsulation. This prevents you from enforcing invariants when you class is\nconstructed and properly hide implementation details from serialization. This library will generate\nTypeAdapters the call your constructor, factory method, or builder to construct you an instance of\nyour class and will only use accessible fields and methods to write it out.\n\nThis library is a nice companion with google's\n[AutoValue](https://github.com/google/auto/tree/master/value), but you may use it with any class you\nchoose. It's nice with kotlin's [data](https://kotlinlang.org/docs/reference/data-classes.html) \nclasses too!\n\n## Download\n\n### Gradle\n\n#### Java\nYou may want to use a gradle plugin for easier management of apt dependencies. For example,\n[gradle-apt-plugin](https://github.com/tbroyer/gradle-apt-plugin).\n\n```groovy\napt 'me.tatarka.gsonvalue:gsonvalue-processor:0.9'\ncompile 'me.tatarka.gsonvalue:gsonvalue:0.9'\n```\n\n#### Android\n```groovy\nannotationProcessor 'me.tatarka.gsonvalue:gsonvalue-processor:0.9'\ncompile 'me.tatarka.gsonvalue:gsonvalue:0.9'\n```\n\n### Maven\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003eme.tatarka.gsonvalue\u003c/groupId\u003e\n  \u003cartifactId\u003egsonvalue\u003c/artifactId\u003e\n  \u003cversion\u003e0.9\u003c/version\u003e\n\u003c/dependency\u003e\n\u003cdependency\u003e\n  \u003cgroupId\u003eme.tatarka.gsonvalue\u003c/groupId\u003e\n  \u003cartifactId\u003egsonvalue-processor\u003c/artifactId\u003e\n  \u003cversion\u003e0.9\u003c/version\u003e\n  \u003cscope\u003eprovided\u003c/scope\u003e\n\u003c/dependency\u003e\n```\n\n\n## Usage \n\nAnnotate your constructor or factory method with `@GsonConstructor`. Json will be deserialized using\nthe parameter names and serialized using public fields or getters. The following classes will map to\nthe json\n```json\n{\"arg\":1}\n```\n\n#### POJO\n```java\nimport me.tatarka.gsonvalue.annotations.GsonConstructor;\n\npublic class Foo {\n    @GsonConstructor\n    public Foo(int arg) {\n        ...\n    }\n    \n    public int arg() {\n        ...\n    }\n}\n\npublic class Bar {\n    @GsonConstructor\n    public static Bar newInstance(int arg) {\n        ...\n    }\n    \n    public int getArg() {\n        ...\n    }\n}\n```\n\n#### AutoValue\n```java\n@AutoValue\npublic abstract class Foo {\n    @GsonConstructor\n    public static Foo newInstance(int arg) {\n        return new AutoValue_Foo(arg);\n    }\n    \n    public abstract int arg();\n}\n```\n\n#### Kotlin Data Class\n```kotlin\n@GsonConstructor data class Foo(val arg: Int)\n```\n\nNote that both bare getters and bean-style prefixes are supported. Like AutoValue, prefixes only\napply if all getters follow that style.\n\n### Builders\n\nAlternatively, you can create a builder class. Annotate either the builder's constructor or\n the factory method that returns the builder with `@GsonBuilder`.\n\n#### POJO\n```java\nimport me.tatarka.gsonvalue.annotations.GsonConstructor;\n\npublic class Foo {\n    public static class Builder {\n        @GsonBuilder\n        public Builder(int arg1) {\n            ...\n        }\n        \n        public Builder arg2(String arg2) {\n            ...\n        }\n        \n        public Foo build() {\n            ...\n        }\n    }\n}\n\npublic class Bar {\n    @GsonBuilder\n    public static Builder builder() {\n        ...\n    }\n    \n    public static class Builder {\n        ...\n    }\n}\n```\n\n#### AutoValue\n```java\n@AutoValue\npublic abstract class Foo {\n\n    @GsonBuilder\n    public static Builder builder() {\n        return new AutoValue_Foo.Builder();\n    }\n\n    @AutoValue.Builder\n    public interface Builder {        \n        Builder arg(int arg);\n        \n        Foo build();\n    }\n}\n```\n\n### Gson\n\nCreate an abstract `TypeAdapterFactory` class an annotate it.\n```java\nimport com.google.gson.TypeAdapterFactory;\nimport me.tatarka.gsonvalue.annotations.GsonValueTypeAdapterFactory;\n\n@GsonValueTypeAdapterFactory\npublic abstract class MyTypeAdapterFactory implements TypeAdapterFactory {\n    public static MyTypeAdapterFactory create() {\n        return new GsonValue_MyTypeAdapterFactory();\n    }\n}\n```\n\nThen register it to your gson builder.\n```java\ngson = new GsonBuilder()\n        .registerTypeAdapterFactory(MyTypeAdapterFactory.create())\n        .create();\n```\n\n### Supported Gson features.\n\n* `@SerializeName` is supported on fields or getters. It will map to both the constructor\nparameter on deserialization and the field or getter on serialization.\n* Transient fields are ignored.\n* `@JsonAdapter` is supported with either a `TypeAdapter` or `TypeAdapterFactory` as long as it\nhas a public no-args constructor. Since gson's version cannot be placed on a getter method, you may\nuse `@me.tatarka.gsonvalue.annotations.JsonAdapter` instead if necessary.\n\n### Unsupported features.\n\nThe following features are not supported. They may be added if there is enough demand.\n\n* `@Since` and `@Until` versioning support.\n* `@Expose` and exclusion strategies.\n* Field naming policies.\n\n## License\n\n    Copyright 2016 Evan Tatarka\n    \n    Licensed under the Apache License, Version 2.0 (the \"License\");\n    you may not use this file except in compliance with the License.\n    You may obtain a copy of the License at\n    \n       http://www.apache.org/licenses/LICENSE-2.0\n    \n    Unless required by applicable law or agreed to in writing, software\n    distributed under the License is distributed on an \"AS IS\" BASIS,\n    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n    See the License for the specific language governing permissions and\n    limitations under the License.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevant%2Fgsonvalue","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fevant%2Fgsonvalue","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevant%2Fgsonvalue/lists"}