{"id":27248858,"url":"https://github.com/bakdata/generic-avro-reflect","last_synced_at":"2025-04-10T23:48:18.926Z","repository":{"id":52876576,"uuid":"168677429","full_name":"bakdata/generic-avro-reflect","owner":"bakdata","description":"Allows generic types to be serialized in avro","archived":false,"fork":false,"pushed_at":"2024-02-22T14:26:25.000Z","size":211,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-04-10T23:48:13.598Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bakdata.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2019-02-01T09:57:11.000Z","updated_at":"2023-10-18T08:11:35.000Z","dependencies_parsed_at":"2024-02-08T10:27:02.635Z","dependency_job_id":"6fe06875-9662-456e-8357-908a3158a2f5","html_url":"https://github.com/bakdata/generic-avro-reflect","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bakdata%2Fgeneric-avro-reflect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bakdata%2Fgeneric-avro-reflect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bakdata%2Fgeneric-avro-reflect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bakdata%2Fgeneric-avro-reflect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bakdata","download_url":"https://codeload.github.com/bakdata/generic-avro-reflect/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248317725,"owners_count":21083528,"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":"2025-04-10T23:48:18.486Z","updated_at":"2025-04-10T23:48:18.920Z","avatar_url":"https://github.com/bakdata.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://dev.azure.com/bakdata/generic-avro-reflect/_apis/build/status/bakdata.generic-avro-reflect?branchName=master)](https://dev.azure.com/bakdata/generic-avro-reflect/_build/latest?definitionId=1\u0026branchName=master)\n[![Sonarcloud status](https://sonarcloud.io/api/project_badges/measure?project=com.bakdata.generic-avro-reflect%3Ageneric-avro-reflect\u0026metric=alert_status)](https://sonarcloud.io/dashboard?id=bakdata-generic-avro-reflect)\n[![Code coverage](https://sonarcloud.io/api/project_badges/measure?project=com.bakdata.generic-avro-reflect%3Ageneric-avro-reflect\u0026metric=coverage)](https://sonarcloud.io/dashboard?id=bakdata-generic-avro-reflect)\n[![Maven](https://img.shields.io/maven-central/v/com.bakdata.generic-avro-reflect/generic-avro-reflect.svg)](https://search.maven.org/search?q=g:com.bakdata.generic-avro-reflect%20AND%20a:generic-avro-reflect\u0026core=gav)\n\nGeneric Avro Reflect\n====================\n\n\nGenerate an Avro schema from any Java object at runtime.\n\nYou can find a [blog post on medium](https://medium.com/bakdata/xxx) with some examples and detailed explanations of how Generic Avro Reflect works.\n\n## Getting Started\n\nYou can add Generic Avro Reflect via Maven Central.\n\n#### Gradle\n```gradle\ncompile group: 'com.bakdata', name: 'generic-avro-reflect', version: '1.0.0'\n```\n\n#### Maven\n```xml\n\u003cdependency\u003e\n    \u003cgroupId\u003ecom.bakdata\u003c/groupId\u003e\n    \u003cartifactId\u003egeneric-avro-reflect\u003c/artifactId\u003e\n    \u003cversion\u003e1.0.0\u003c/version\u003e\n\u003c/dependency\u003e\n```\n\n\nFor other build tools or versions, refer to the [latest version in MvnRepository](https://mvnrepository.com/artifact/com.bakdata.generic-avro-reflect/generic-avro-reflect/latest).\n\n## Using it\n\nHere is a quick example that shows you how to use Generic Avro Reflect.\n\nLet's say you have this class and want to generate an Avro schema from an instance. \n\n```java\npublic class MyClass\u003cT\u003e {\n    T myValue;\n    \n    public MyClass(T value) {\n        myValue = value;\n    }\n}\n```\n\nThen to generate the Avro schema, you simply have to call the `getSchema()` method on the `Reflect2Data`. \n\n```java\nMyClass\u003cString\u003e myObject = new MyClass\u003c\u003e(\"foo\");\nSchema mySchema = Reflect2Data.get().getSchema(myObject);\n```\n\nThis will result in the following schema:\n```json\n{\n  \"type\": \"record\",\n  \"name\": \"MyClass\",\n  \"namespace\": \"org.my.namespace\",\n  \"fields\": [\n    {\n      \"name\": \"myValue\",\n      \"type\": \"string\",\n    }\n  ]\n}\n```\n\n#### Reading and Writing Schemas\n\nYou probably want to use this to write and read your data.\nAvro uses `DatumWriter`s and `DatumReader`s for this. \nYou simply specify the schema that should be written or read and the rest will be done for you.\n\n```java\n// Get Schema for myObject.\nReflect2Data reflectData = Reflect2Data.get();\nMyClass\u003cString\u003e myObject = new MyClass\u003c\u003e(\"foo\");\nSchema schema = reflectData.getSchema(myObject);\n\n// Write myObject to a byte array.\nDatumWriter datumWriter = reflectData.createDatumWriter(schema);\nOutputStream out = new ByteArrayOutputStream();\n// We can ignore the `null` for this example.\nBinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null);\ndatumWriter.write(x, encoder);\n\n// Read serialized myObject back to a Java object.\n// We can ignore the `null`s for this example\nBinaryDecoder decoder = DecoderFactory.get().binaryDecoder(out.toByteArray(), null);\nDatumReader datumReader = reflectData.createDatumReader(schema);\nMyClass\u003cString\u003e myNewObject = datumReader.read(null, decoder);\n\n// We can expect the object before and the one after serializing to be equal.\nassertEquals(myObject, myNewObject);\n``` \n\n#### More Examples\n\nYou can find many more tests in [this repository's test code](https://github.com/bakdata/generic-avro-reflect/blob/master/generic-avro-reflect/src/test/java/org/apache/avro/reflect/Reflect2DataTest.java).\n\n#### Known Limitations\n\nAs the type resolution is done at runtime and depends on actual values being present, there is a limitation regarding `null` values.\nIf a value is set to `null`, Reflect2Data cannot resolve its type and will default to `Object`.\n\n```java\nMyClass\u003cString\u003e myObject = new MyClass\u003c\u003e(null);\nSchema mySchema = Reflect2Data.get().getSchema(myObject);\n```\n\nDoing this will result in the following schema:\n```json\n{\n  \"type\": \"record\",\n  \"name\": \"MyClass\",\n  \"namespace\": \"org.apache.avro.reflect.data\",\n  \"fields\": [\n    {\n      \"name\": \"myValue\",\n      \"type\": {\n        \"type\": \"record\",\n        \"name\": \"Object\",\n        \"namespace\": \"java.lang\",\n        \"fields\": [],\n      },\n    }\n  ],\n}\n```\n\nThis also applies to empty `List`s or `Map`s. \nIn general, you can assume if there is a value on which you can call `.getClass()`, the type can be resolved.\n\n## Development\n\nIf you want to contribute to this project, you can simply clone the repository and build it via Gradle.\nAll dependencies should be included in the Gradle files, there are no external prerequisites.\n\n```bash\n\u003e git clone git@github.com:bakdata/generic-avro-reflect.git\n\u003e cd generic-avro-reflect \u0026\u0026 ./gradlew build\n```\n\nPlease note, that we have [code styles](https://github.com/bakdata/bakdata-code-styles) for Java.\nThey are basically the Google style guide, with some small modifications.\n\n## Contributing\n\nWe are happy if you want to contribute to this project.\nIf you find any bugs or have suggestions for improvements, please open an issue.\nWe are also happy to accept your PRs.\nJust open an issue beforehand and let us know what you want to do and why.\n\n## License\nThis project is licensed under the MIT license.\nHave a look at the [LICENSE](https://github.com/bakdata/fluent-kafka-streams-tests/blob/master/LICENSE) for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbakdata%2Fgeneric-avro-reflect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbakdata%2Fgeneric-avro-reflect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbakdata%2Fgeneric-avro-reflect/lists"}