{"id":25824980,"url":"https://github.com/oleg-cherednik/json-gson-impl","last_synced_at":"2026-04-15T23:31:48.491Z","repository":{"id":46290476,"uuid":"327434375","full_name":"oleg-cherednik/json-gson-impl","owner":"oleg-cherednik","description":"An implementation of json-api for gson project.","archived":false,"fork":false,"pushed_at":"2024-02-24T10:17:23.000Z","size":374,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-10-06T10:48:12.995Z","etag":null,"topics":["gson","gsonutil","java","java-8","java-json","json","json-api"],"latest_commit_sha":null,"homepage":"https://github.com/oleg-cherednik/json-gson-impl","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/oleg-cherednik.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-01-06T21:35:13.000Z","updated_at":"2024-01-05T06:59:34.000Z","dependencies_parsed_at":"2023-12-14T05:23:45.143Z","dependency_job_id":"793d0666-5f1e-46e9-b6fb-cbcee3c18980","html_url":"https://github.com/oleg-cherednik/json-gson-impl","commit_stats":null,"previous_names":["oleg-cherednik/gson-utils"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/oleg-cherednik/json-gson-impl","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oleg-cherednik%2Fjson-gson-impl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oleg-cherednik%2Fjson-gson-impl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oleg-cherednik%2Fjson-gson-impl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oleg-cherednik%2Fjson-gson-impl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oleg-cherednik","download_url":"https://codeload.github.com/oleg-cherednik/json-gson-impl/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oleg-cherednik%2Fjson-gson-impl/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31864980,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-15T15:24:51.572Z","status":"ssl_error","status_checked_at":"2026-04-15T15:24:39.138Z","response_time":63,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["gson","gsonutil","java","java-8","java-json","json","json-api"],"created_at":"2025-02-28T13:25:48.528Z","updated_at":"2026-04-15T23:31:48.430Z","avatar_url":"https://github.com/oleg-cherednik.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Maven Central](https://maven-badges.herokuapp.com/maven-central/ru.oleg-cherednik.gson/gson-utils/badge.svg)](https://maven-badges.herokuapp.com/maven-central/ru.oleg-cherednik.gson/gson-utils)\n[![javadoc](https://javadoc.io/badge2/ru.oleg-cherednik.gson/gson-utils/javadoc.svg)](https://javadoc.io/doc/ru.oleg-cherednik.gson/gson-utils)\n[![java8](https://badgen.net/badge/java/8+/blue)](https://badgen.net/)\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](http://www.apache.org/licenses/LICENSE-2.0.txt)\n\n[![circle-ci](https://circleci.com/gh/oleg-cherednik/gson-utils/tree/master.svg?style=shield)](https://app.circleci.com/pipelines/github/oleg-cherednik/gson-utils)\n[![codacy-quality](https://app.codacy.com/project/badge/Grade/ba0faab92b44432491376ee5d331a63e?branch=master)](https://app.codacy.com/gh/oleg-cherednik/gson-utils/dashboard?branch=master)\n\n\u003cdetails\u003e\u003csummary\u003edevelop\u003c/summary\u003e\n\u003cp\u003e\n\n[![circle-ci](https://circleci.com/gh/oleg-cherednik/gson-utils/tree/dev.svg?style=shield)](https://app.circleci.com/pipelines/github/oleg-cherednik/gson-utils)\n[![codecov](https://codecov.io/gh/oleg-cherednik/gson-utils/branch/dev/graph/badge.svg?token=2wNFKJhR70)](https://codecov.io/gh/oleg-cherednik/gson-utils)\n[![vulnerabilities](https://snyk.io/test/github/oleg-cherednik/gson-utils/badge.svg?targetFile=build.gradle)](https://snyk.io/test/github/oleg-cherednik/gson-utils?targetFile=build.gradle)\n[![codacy-quality](https://app.codacy.com/project/badge/Grade/ba0faab92b44432491376ee5d331a63e?branch=dev)](https://app.codacy.com/gh/oleg-cherednik/gson-utils/dashboard?branch=dev)\n\n\u003c/p\u003e\n\u003c/details\u003e  \n\n# gson-utils\n\u003e [Gson](https://github.com/google/gson) usability utilities. It's designed to\n\u003e add additional features like easy and centralized configuration, builder or\n\u003e static method set. Artifact does not include direct `Gson` dependencies. It is\n\u003e up to you to add them into your project. \n\n## Features\n*   Encapsulate all checked exceptions from Gson with custom runtime exception;\n*   A central place for configuration;\n*   A central place for holding `Gson` instances;\n*   Utility class to make most common operations much more comfortable to use;\n*   Ability to change `Zone` to save `ZonedDateTime` independently of original zone;\n*   `Reader`/`Writer` support for objects, lists and maps;\n*   Lazy read support for list and maps from `Writer`;\n*   Read numeric as `Integer`, `Long`, `BigInteger` or `Double` (but not only as `Double`).\n*   Advanced `Reader`/`Writer` support for `enum`. \n\n## Build Tools\n\n### Gradle\n\n```groovy\ncompile 'ru.oleg-cherednik.gson:gson-utils:2.2'\n```\n\n### Maven\n\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003eru.oleg-cherednik.gson\u003c/groupId\u003e\n    \u003cartifactId\u003egson-utils\u003c/artifactId\u003e\n    \u003cversion\u003e2.2\u003c/version\u003e\n\u003c/dependency\u003e\n```                                                    \n\nIn the version, first part is the major version of `Gson` that is used in this utils.\nThe second part is the `gson-utils` version. This number is unique.\n\n**Note:** `gson-utils` does not contain dependency to the specific `gson` version, so you have to\nadd it additionally:\n\n```groovy\ncompile 'com.google.code.gson:gson:2.8.7'\n```\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.google.code.gson\u003c/groupId\u003e\n    \u003cartifactId\u003egson\u003c/artifactId\u003e\n    \u003cversion\u003e2.8.7\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n## Usage \n\n*   [GsonUtils](#gsonutils-class) - utility class with set of methods to use json transformation;\n*   [GsonUtilsBuilder](#gsonutilsbulder-class) - builder for Gson instance contains all configuration properties;\n*   [GsonDecorator](#gsondecorator-class) - decorator class to hold Gson instance with additional methods;\n*   [GsonUtilsHelper](#gsonutilshelper-class) - helper class is used as a holder for default properties;\n*   [EnumId](#work-with-enum) - advanced enum serialization support;\n\n\u003cdetails\u003e\u003csummary\u003eData class for examples\u003c/summary\u003e\n\u003cp\u003e\n\n```java   \npackage ru.olegcherednik.utils.gson.data;\n\npublic class Data {\n\n    private int intVal;\n    private String strVal;\n\n    public Date() {}\n\n    public Data(int intValue, String strValue) {\n        this.intValue = intValue;\n        this.strValue = strValue;\n    }\n\n    public int getIntValue() {\n        return intValue;\n    }\n\n    public String getStrVal() {\n        return strValue;\n    }\n\n}\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n### GsonUtils class\n\n#### Read json\n\n\u003cdetails\u003e\u003csummary\u003edetails\u003c/summary\u003e\n\u003cp\u003e\n\n##### Read json string to a custom object type (but not a collection)\n\n```java\npublic class Snippet {\n\n    public static Data jsonStringToObj() {\n        String json = \"\"\"\n                      {\n                        \"intVal\": 666,\n                        \"strVal\": \"omen\"\n                      }\n                      \"\"\";\n        return GsonUtils.readValue(json, Data.class);\n    }\n}\n```\n\n##### Read json string to a list of custom object type\n\n```java\npublic class Snippet {\n\n    public static List\u003cData\u003e jsonStringToList() {\n        String json = \"\"\"\n                      [\n                          {\n                              \"intVal\" : 555,\n                              \"strVal\" : \"victory\"\n                          },\n                          {\n                              \"intVal\" : 666,\n                              \"strVal\" : \"omen\"\n                          }\n                      ]\n                      \"\"\";\n        return GsonUtils.readList(json, Data.class);\n    }        \n\n}\n```\n\n##### Read json string to a map with string as keys and map or primitive types as values\n\n```java\npublic class Snippet {\n\n    public static Map\u003cString, ?\u003e jsonStringToMap() {\n        String json = \"\"\"\n                      {\n                          \"victory\" : {\n                              \"intVal\" : 555,\n                              \"strVal\" : \"victory\"\n                          },\n                          \"omen\" : {\n                              \"intVal\" : 666,\n                              \"strVal\" : \"omen\"\n                          }\n                      }\n                      \"\"\";\n        return GsonUtils.readMap(json);\n    }\n                 \n}\n```\n**Note:** `Map` values have either primitive type or `Map` or `List`.\n\n##### Read json string to a map with string as keys and given type as value\n\n```java\npublic class Snippet {\n\n    public static Map\u003cString, Data\u003e jsonStringToMap() {\n        String json = \"\"\"\n                      {\n                          \"victory\" : {\n                              \"intVal\" : 555,\n                              \"strVal\" : \"victory\"\n                          },\n                          \"omen\" : {\n                              \"intVal\" : 666,\n                              \"strVal\" : \"omen\"\n                          }\n                      }\n                      \"\"\";\n        return GsonUtils.readMap(json, Data.class);\n    }\n                 \n}\n```\n\n##### Read json string to a map with given type for key and value\n\n```java\npublic class Snippet {\n\n    public static Map\u003cInteger, Data\u003e jsonStringToMap() {\n        String json = \"\"\"\n                      {\n                          \"1\" : {\n                              \"intVal\" : 555,\n                              \"strVal\" : \"victory\"\n                          },\n                          \"2\" : {\n                              \"intVal\" : 666,\n                              \"strVal\" : \"omen\"\n                          }\n                      }\n                      \"\"\";\n        return GsonUtils.readMap(json, Integer.class, Data.class);\n    }\n                 \n}\n```\n\n##### Read json from a reader to a custom object type (but not a collection)\n\n```json                        \n{\n    \"intVal\" : 666,\n    \"strVal\" : \"omen\"\n}\n```\n```java\npublic class Snippet {\n\n    public static Data readJsonFromFileToObj(File file) {\n        try (Reader in = new FileReader(file)) {\n            return GsonUtils.readValue(in, Data.class);\n        }\n    }\n                 \n}\n```\n\n##### Read json from a reader eager to a list of custom object type\n```json                        \n[\n    {\n        \"intVal\" : 555,\n        \"strVal\" : \"victory\"\n    },\n    {\n        \"intVal\" : 666,\n        \"strVal\" : \"omen\"\n    }\n]\n```\n```java\npublic class Snippet {\n\n    public static List\u003cData\u003e readJsonEdgerFromFileToList(File file) {\n        try (Reader in = new FileReader(file)) {\n            return GsonUtils.readList(in, Data.class);\n        }\n    }\n                 \n}\n```\n\n##### Read json from a reader lazy to a list of custom object type\n\n```json                        \n[\n    {\n        \"intVal\" : 555,\n        \"strVal\" : \"victory\"\n    },\n    {\n        \"intVal\" : 666,\n        \"strVal\" : \"omen\"\n    }\n]\n```\n```java\npublic class Snippet {\n\n    public static List\u003cData\u003e readJsonLazyFromFileToList(File file) {\n        try (Reader in = new FileReader(file)) {\n            List\u003cData\u003e res = new ArrayList\u003c\u003e();\n            Iterator\u003cData\u003e it = GsonUtils.readListLazy(in, Data.class);\n            \n            while (it.hasNext())\n                res.add(it.next());\n            \n            return res;\n        }\n    }\n                 \n}\n```\n##### Read json from a reader to a map with string as keys and map or primitive types as a values\n\n```json                        \n{\n    \"victory\" : {\n        \"intVal\" : 555,\n        \"strVal\" : \"victory\"\n    },\n    \"omen\" : {\n        \"intVal\" : 666,\n        \"strVal\" : \"omen\"\n    }\n}\n```\n```java\npublic class Snippet {\n\n    public static Map\u003cString, ?\u003e readJsonFromFileToMap(File file) {\n        try (Reader in = new FileReader(file)) {\n            return GsonUtils.readMap(in);\n        }\n    }\n   \n}\n```\n**Note:** `map` values have either primitive type or `Map` or `List`.\n\n##### Read json from a reader to a map with string as keys and given type as a value\n\n```json                        \n{\n    \"victory\" : {\n        \"intVal\" : 555,\n        \"strVal\" : \"victory\"\n    },\n    \"omen\" : {\n        \"intVal\" : 666,\n        \"strVal\" : \"omen\"\n    }\n}\n```\n```java\npublic class Snippet {\n\n    public static Map\u003cString, Data\u003e readJsonFromFileToMap(File file) {\n        try (Reader in = new FileReader(file)) {\n            return GsonUtils.readMap(in, Data.class);\n        }\n    }\n                 \n}\n```\n\n##### Read json from a reader to a map with Integer as keys and given type as a value\n\n```json                        \n{\n    \"1\" : {\n        \"intVal\" : 555,\n        \"strVal\" : \"victory\"\n    },\n    \"2\" : {\n        \"intVal\" : 666,\n        \"strVal\" : \"omen\"\n    }\n}\n```\n```java\npublic class Snippet {\n\n    public static Map\u003cInteger, Data\u003e readJsonFromFileToMap(File file) {\n        try (Reader in = new FileReader(file)) {\n            return GsonUtils.readMap(in, Integer.class, Data.class);\n        }\n    }\n                 \n}\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n#### Write json\n\n\u003cdetails\u003e\u003csummary\u003edetails\u003c/summary\u003e\n\u003cp\u003e\n\n##### Write a custom object to json string\n\n```java\npublic class Snippet {\n\n    public static String objToJsonString() {\n        Data data = new Data(555, \"victory\");\n        return GsonUtils.writeValue(data);\n    }\n                 \n}\n```\n```json\n{\"intVal\":555,\"strVal\":\"victory\"}\n```\n\n##### Write a list to json string\n\n```java\npublic class Snippet {\n\n    public static String listToJsonString() {\n        List\u003cData\u003e data = List.of(new Data(555, \"victory\"), new Data(666, \"omen\"));\n        return GsonUtils.writeValue(map);\n    }\n                 \n}\n```\n```json\n[{\"intVal\":555,\"strVal\":\"victory\"},{\"intVal\":666,\"strVal\":\"omen\"}]\n```\n\n##### Write a map to json string\n\n```java\npublic class Snippet {\n\n    public static String mapToJsonString() {\n        Map\u003cString, Data\u003e data = Map.of(\n                \"victory\", new Data(555, \"victory\"),\n                \"omen\", new Data(666, \"omen\"));\n        return GsonUtils.writeValue(data);\n    }\n                 \n}\n```\n```json\n{\"victory\":{\"intVal\":555,\"strVal\":\"victory\"},\"omen\":{\"intVal\":666,\"strVal\":\"omen\"}}\n```\n\n##### Write any object to pretty print json string\n\n```java\npublic class Snippet {\n\n    public static String mapToPrettyPrintJsonString() {\n        Map\u003cString, Data\u003e data = Map.of(\n                \"victory\", new Data(555, \"victory\"),\n                \"omen\", new Data(666, \"omen\"));\n        return GsonUtils.prettyPrint().writeValue(data);\n    }\n   \n}\n```\n```json\n{\n  \"victory\": {\n    \"intVal\": 555,\n    \"strVal\": \"victory\"\n  },\n  \"omen\": {\n    \"intVal\": 666,\n    \"strVal\": \"omen\"\n  }\n}\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n#### Work with enum\n\n\u003cdetails\u003e\u003csummary\u003edetails\u003c/summary\u003e\n\u003cp\u003e\n\nBy default, `Gson` serializes or deserializes enums by the case-sensitive `name`.\nIt could give a problem as well as sometime it's better to change name of the\nconstant when serialize it into `json`.\n\nTo solve these issues, `GsonUtils` provides an interface `EnumId` with range of\nmethod to serialize or deserialize enums. To use it, you have to declare your\nenums according to the following snippet:\n\n```java\npublic enum Auto implements EnumId {\n    AUDI(\"audi\"),\n    BMW(\"bmw\"),\n    MERCEDES(\"mercedes\");\n\n    private final String id;\n\n    Auto(String id) {\n        this.id = id;\n    }\n\n    @Override\n    public String getId() {\n        return id;\n    }\n\n    @JsonCreator\n    public static Auto parseId(String id) {\n        return EnumId.parseId(Auto.class, id);\n    }\n}\n```  \n\nWhere `@JsonCreator` is an *optional* annotation to mark a single method that\naccepts exactly one string parameter to deserialize an enum constant.\n\nThat's it! You can use `gson-utils` methods as usual. \n\n\u003c/p\u003e\n\u003c/details\u003e\n\n### Spring bean\n\n\u003cdetails\u003e\u003csummary\u003edetails\u003c/summary\u003e\n\u003cp\u003e\n\n#### Create gson bean based on the default configuration\n\n`GsonUtilsHelper` class contains *default gson configuration*. This configuration\ncan be used to create gson bean:\n\n```java\n@Configuration\npublic class AppConfig {\n\n    @Bean\n    public GsonDecorator gsonDecorator() {\n        return GsonUtilsHelper.createGsonDecorator();\n    }\n\n}\n```\n\n#### Create gson bean based on the custom configuration\n\nTo customize gson configuration, a new instance of `GsonUtilsBuilder` should be\ncreated and configured. Then this instance should be used to create `GsonDecorator`.\n\n```java\n@Configuration\npublic class AppConfig {\n\n    @Bean\n    public GsonUtilsBuilder gsonUtilsBuilder() {\n        // customize Gson here\n        return new GsonUtilsBuilder();\n    }\n\n    @Bean\n    public GsonDecorator gsonDecorator(GsonUtilsBuilder gsonUtilsBuilder) {\n        return GsonUtilsHelper.createGsonDecorator(gsonUtilsBuilder);\n    }\n\n}\n```\n\n### Using of gson bean\n\nA new `GsonDecorator` should be used to work with json instead of using a `Gson` instance. \n\n```java\n@Service\npublic class SpringBootService {\n\n    @Autowired\n    private GsonDecorator gson;\n\n    public String toJson(Data data) {\n        return gson.writeValue(data);\n    }\n\n    public Data fromJson(String json) {\n        return gson.readValue(json, Data.class);\n    }\n\n}\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n### GsonUtilsBuilder class\n\n\u003cdetails\u003e\u003csummary\u003edetails\u003c/summary\u003e\n\u003cp\u003e\n\nThe class provides ability to customize `Gson` instance and create a new `Gson`\ninstances with current settings.\n\n#### Use custom type adapter factory\n\nFollowing snippet shows how to serialize `Date` to `UTC` time zone and format `\"HH:mm:ss yyyy-MM-dd\"`. \nYou have to create a new instance of `GsonUtilsBuilder` and add custom implementation.\n\n```java\npublic class Snippet {\n\n    public static Gson createCustomGson() {\n        UnaryOperator\u003cZoneId\u003e zoneModifier = GsonUtilsBuilder.ZONE_MODIFIER_TO_UTC;\n        DateTimeFormatter df = DateTimeFormatter.ofPattern(\"HH:mm:ss yyyy-MM-dd\");\n        TypeAdapter\u003cDate\u003e typeAdapter = new DateTypeAdapter(zoneModifier, dateTimeFormatter);\n        GsonUtilsBuilder builder = new GsonUtilsBuilder().registerTypeAdapter(Data.class, typeAdapter);\n        return builder.gson();\n    }\n   \n}\n```\n\u003c/p\u003e\n\u003c/details\u003e\n\n### GsonDecorator class\n\n\u003cdetails\u003e\u003csummary\u003edetails\u003c/summary\u003e\n\u003cp\u003e\n\nThis is a decorator over the standard `Gson` class with more additional method. `GsonUtils` chooses required `Gson`\ninstance and delegates all work to the `GsonDecorator`.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n### GsonUtilsHelper class\n\n\u003cdetails\u003e\u003csummary\u003edetails\u003c/summary\u003e\n\u003cp\u003e\n\nThis class provides set of methods to create a `Gson` instances based on given `GsonUtilsBulder`.\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n### Links\n\n*   Home page: https://github.com/oleg-cherednik/gson-utils\n\n*   Maven:\n    *   **central:** https://mvnrepository.com/artifact/ru.oleg-cherednik.gson/gson-utils\n    *   **download:** https://repo1.maven.org/maven2/ru/oleg-cherednik/gson/gson-utils/\n   \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foleg-cherednik%2Fjson-gson-impl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foleg-cherednik%2Fjson-gson-impl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foleg-cherednik%2Fjson-gson-impl/lists"}