{"id":29376484,"url":"https://github.com/twitter/bijection","last_synced_at":"2025-07-09T22:43:09.133Z","repository":{"id":6151715,"uuid":"7380994","full_name":"twitter/bijection","owner":"twitter","description":"Reversible conversions between types","archived":false,"fork":false,"pushed_at":"2024-11-22T02:00:47.000Z","size":2484,"stargazers_count":657,"open_issues_count":53,"forks_count":123,"subscribers_count":137,"default_branch":"develop","last_synced_at":"2025-07-08T03:39:37.999Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Scala","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/twitter.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":"CONTRIBUTING.md","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":"2012-12-30T22:22:25.000Z","updated_at":"2025-07-03T01:57:51.000Z","dependencies_parsed_at":"2023-02-11T22:46:42.205Z","dependency_job_id":"bd5391e9-7b60-467d-b85e-01426ec84d75","html_url":"https://github.com/twitter/bijection","commit_stats":{"total_commits":712,"total_committers":62,"mean_commits":"11.483870967741936","dds":0.7317415730337078,"last_synced_commit":"f4563bfd2f9e026f46645144c7246c94da14f8e5"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"purl":"pkg:github/twitter/bijection","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twitter%2Fbijection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twitter%2Fbijection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twitter%2Fbijection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twitter%2Fbijection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/twitter","download_url":"https://codeload.github.com/twitter/bijection/tar.gz/refs/heads/develop","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/twitter%2Fbijection/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264504616,"owners_count":23618831,"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-07-09T22:43:08.526Z","updated_at":"2025-07-09T22:43:09.125Z","avatar_url":"https://github.com/twitter.png","language":"Scala","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Bijection\n\n[![Build Status](https://secure.travis-ci.org/twitter/bijection.png)](http://travis-ci.org/twitter/bijection) [![Codecov](https://img.shields.io/codecov/c/github/twitter/bijection.svg?maxAge=2592000)](https://codecov.io/github/twitter/bijection)\n[![Latest version](https://index.scala-lang.org/twitter/bijection/bijection-core/latest.svg?color=orange)](https://index.scala-lang.org/twitter/bijection/bijection-core)\n[![Chat](https://badges.gitter.im/twitter/bijection.svg)](https://gitter.im/twitter/bijection?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge\u0026utm_content=badge)\n\nA Bijection is an invertible function that converts back and forth between two types, with\nthe contract that a round-trip through the Bijection will bring back the original object. Moreover,\nthe inverse has the same property.\n\nSee the [current API documentation](http://twitter.github.io/bijection) for more information.\n\n## Examples:\n\n```scala\n\u003e ./sbt bijection-core/console\nscala\u003e import com.twitter.bijection._\nscala\u003e Bijection[Int, java.lang.Integer](42)\nres0: java.lang.Integer = 42\n```\n\nIn addition to Bijection, we have Injection. An Injection embeds a type A in a larger space of type\nB. Every item from A can be round-tripped through B, but not every B can be mapped to A. So\nInjection is like a pair of function: `A =\u003e B, B =\u003e Try[A]`.\n\n```scala\nimport com.twitter.bijection._\n\nscala\u003e Injection[Int, String](100)\nres0: String = 100\n\nscala\u003e Injection.invert[Int, String](res0)\nres1: Try[Int] = Success(100)\n```\nIf we want to treat an Injection like a Bijection (over a restricted subspace of the larger set),\nwe use the `B @@ Rep[A]` syntax, for instance: `String @@ Rep[Int]`\n\n```scala\nBijection[Int, String @@ Rep[Int]](100)\nres2: com.twitter.bijection.package.@@[String,com.twitter.bijection.Rep[Int]] = 100\n```\n\nUse `invert` to reverse the transformation:\n\n```scala\nscala\u003e Bijection.invert[Int, String @@ Rep[Int]](res2)\nres3: Int = 100\n```\n\nIf you `import Conversion.asMethod` you can use `.as[T]` to use an available Bijection/Injection to `T`:\n\n```scala\nscala\u003e import com.twitter.bijection.Conversion.asMethod\nimport com.twitter.bijection.Conversion.asMethod\n\nscala\u003e 1.as[java.lang.Integer]\nres6: java.lang.Integer = 1\n```\n\nBijections and Injections can also be composed. As with functions, `andThen` composes forward, `compose` composes backward.\n\nThis example round-trips a long into a GZipped base64-encoded string:\n\n```scala\nscala\u003e val injection = Injection.long2BigEndian andThen Bijection.bytes2GZippedBase64\ninjection: com.twitter.bijection.Injection[Long,Array[Byte]] = \u003cfunction1\u003e\n\nscala\u003e injection(123456789L)\nres1: com.twitter.bijection.GZippedBase64String = GZippedBase64String(H4sIAAAAAAAAAGNgYGBgjz4rCgBpa5WLCAAAAA==)\n\nscala\u003e injection.invert(res1)\nres2: Try[Long] = Success(123456789)\n```\n\nWhen you have bijections between a path of items you can `Bijection.connect` or `Injection.connect` them:\n\n```scala\nscala\u003e import com.twitter.bijection.Injection.connect\nimport com.twitter.bijection.Injection.connect\n\nscala\u003e import com.twitter.bijection.Base64String\nimport com.twitter.bijection.Base64String\n\nscala\u003e import Conversion.asMethod\nimport Conversion.asMethod\n\nscala\u003e implicit val long2String2Bytes2B64 = connect[Long,String,Array[Byte],Base64String]\nstring2Long2Bytes2B64: com.twitter.bijection.Bijection[String,com.twitter.bijection.Base64String] = \u003cfunction1\u003e\n\nscala\u003e 243L.as[Base64String]\nres0: com.twitter.bijection.Base64String = Base64String(MjQz)\n\nscala\u003e long2String2Bytes2B64.invert(res0)\nres1: Try[Long] = Success(243)\n```\n\n## Supported Bijections/Injections\n\nBijection implicitly supplies Bijections between:\n\n* all numeric types \u003c-\u003e their boxed java counterparts\n* containers/primitives \u003c-\u003e Json (Injections via bijection-json)\n* thrift/protobuf/avro \u003c-\u003e `Array[Byte]` (Injections via bijection-protobuf/bijection-thrift/bijection-avro)\n* all numeric types \u003c-\u003e big-endian `Array[Byte]` encodings (Injections)\n* all numeric types \u003c-\u003e String (Injections)\n* Bijections for all `asScala`, `asJava` pairs provided by [scala.collection.JavaConverters](http://www.scala-lang.org/api/current/scala/collection/JavaConverters$.html)\n* String \u003c-\u003e utf8 encoded bytes\n* `Array[Byte]` \u003c-\u003e `GZippedBytes`\n* `Array[Byte]` \u003c-\u003e `Base64String`\n* `Array[Byte]` \u003c-\u003e `GZippedBase64String`\n* `Array[Byte]` \u003c-\u003e `java.nio.ByteBuffer`\n* `Class[T]` \u003c-\u003e String (Injection)\n* `A =\u003e B` \u003c-\u003e `C =\u003e D` (function conversion)\n* Bijection/Injection builders for all tuples. (`(String,Int)` \u003c-\u003e `(Array[Byte], java.lang.Integer)` is built automatically, for example.)\n\nAdditionally there is a method to generate Bijections between most of Scala's built in types:\n```Bijection.toContainer[Int,String,List[Int],Vector[String]``` returns\n```Bijection[List[Int], Vector[String]```\n\nIf you see a reversible conversion that is not here and related to types in the standard library\nof Java or Scala, please contribute!\n\n## Serialization via Bufferable\n\n`Bufferable[T]` handles putting and getting a type `T` into a ByteBuffer in a composable way.\n`Bufferable[T]` instances for all primitives/tuples/containers are provided. Bijections and\nInjections to any of these types give you binary serialization via Bufferable.\n\n## Documentation\n\nTo learn more and find links to tutorials and information around the web, check out the [Bijection Wiki](https://github.com/twitter/bijection/wiki).\n\nThe latest ScalaDocs are hosted on Bijection's [Github Project Page](http://twitter.github.io/bijection).\n\n## Get Involved + Code of Conduct\nPull requests and bug reports are always welcome!\n\nDiscussion occurs primarily on the [Bijection mailing list](https://groups.google.com/forum/#!forum/bijection).\nIssues should be reported on the [GitHub issue tracker](https://github.com/twitter/bijection/issues).\n\nWe use a lightweight form of project governence inspired by the one used by Apache projects.\nPlease see [Contributing and Committership](https://github.com/twitter/analytics-infra-governance#contributing-and-committership) for our code of conduct and our pull request review process.\nThe TL;DR is send us a pull request, iterate on the feedback + discussion, and get a +1 from a [Committer](COMMITTERS.md) in order to get your PR accepted.\n\nThe current list of active committers (who can +1 a pull request) can be found here: [Committers](COMMITTERS.md)\n\nA list of contributors to the project can be found here: [Contributors](https://github.com/twitter/bijection/graphs/contributors)\n\n## Maven\n\nBijection modules are available on maven central. The current groupid and version for all modules is, respectively, `\"com.twitter\"` and  `0.9.7`.\n\nCurrent published artifacts are\n\n* `bijection-core`\n* `bijection-protobuf`\n* `bijection-thrift`\n* `bijection-guava`\n* `bijection-scrooge`\n* `bijection-json`\n* `bijection-util`\n* `bijection-clojure`\n* `bijection-netty`\n* `bijection-avro`\n* `bijection-hbase`\n\nEvery artifact is published against Scala `\"2.11\"`,  `\"2.12\"` and `\"2.13\"`. To pull in the jars, make sure to add your desired scala version as a suffix, ie:\n\n`bijection-core_2.11` or `bijection-core_2.12` or `bijection-core_2.13`.\n\n## Chat\n[![Gitter](https://badges.gitter.im/twitter/bijection.svg)](https://gitter.im/twitter/bijection?utm_source=badge\u0026utm_medium=badge\u0026utm_campaign=pr-badge)\n\n## Authors\n\n* Oscar Boykin \u003chttp://twitter.com/posco\u003e\n* Marius Eriksen \u003chttp://twitter.com/marius\u003e\n* Sam Ritchie \u003chttp://twitter.com/sritchie\u003e\n\n## License\n\nCopyright 2012 Twitter, Inc.\n\nLicensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwitter%2Fbijection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftwitter%2Fbijection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftwitter%2Fbijection/lists"}