{"id":13594011,"url":"https://github.com/kittinunf/Forge","last_synced_at":"2025-04-09T05:32:43.177Z","repository":{"id":45016474,"uuid":"43631014","full_name":"kittinunf/Forge","owner":"kittinunf","description":"Functional style JSON parsing in Kotlin","archived":false,"fork":false,"pushed_at":"2022-01-14T00:57:16.000Z","size":288,"stargazers_count":127,"open_issues_count":1,"forks_count":9,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-05T17:01:41.155Z","etag":null,"topics":["functional","functional-programming","json","kotlin","parser"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kittinunf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["kittinunf"]}},"created_at":"2015-10-04T09:18:14.000Z","updated_at":"2025-04-05T09:46:51.000Z","dependencies_parsed_at":"2022-09-26T21:41:00.245Z","dependency_job_id":null,"html_url":"https://github.com/kittinunf/Forge","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kittinunf%2FForge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kittinunf%2FForge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kittinunf%2FForge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kittinunf%2FForge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kittinunf","download_url":"https://codeload.github.com/kittinunf/Forge/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247987056,"owners_count":21028891,"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":["functional","functional-programming","json","kotlin","parser"],"created_at":"2024-08-01T16:01:27.565Z","updated_at":"2025-04-09T05:32:40.423Z","avatar_url":"https://github.com/kittinunf.png","language":"Kotlin","readme":"# Forge\n\n[![Kotlin](https://img.shields.io/badge/Kotlin-1.4.0-blue.svg)](http://kotlinlang.org)\n[![jcenter](https://api.bintray.com/packages/kittinunf/maven/Forge/images/download.svg)](https://bintray.com/kittinunf/maven/Forge/_latestVersion)\n[![MavenCentral](https://maven-badges.herokuapp.com/maven-central/com.github.kittinunf.forge/forge/badge.svg)](https://search.maven.org/search?q=g:com.github.kittinunf.forge)\n[![Build Status](https://travis-ci.org/kittinunf/Forge.svg?branch=master)](https://travis-ci.org/kittinunf/Forge)\n[![](https://jitpack.io/v/kittinunf/forge.svg)](https://jitpack.io/#kittinunf/forge/) \n[![Codecov](https://codecov.io/github/kittinunf/Forge/coverage.svg?branch=master)](https://codecov.io/gh/kittinunf/Forge)\n\nForge is a JSON library that helps you map your Kotlin class from a JSON and vice versa in a functional way. Forge is highly inspired by [Aeson](https://hackage.haskell.org/package/aeson), JSON parsing library in Haskell.\n\n## Ideology\n\nHave you ever wonder that how other JSON libraries out there work? Magic under the hood? or a complex annnotation processing? If that is something that you don't want, with Forge, we don't do any of those. \n\nForge aims to provide a full control over how to parse JSON into a Kotlin class, no more magic, no more annotation.\n\n## Installation\n\n### Gradle\n\n```\nrepositories {\n    jcenter() //or mavenCentral()\n}\n\ndependencies {\n    compile 'com.github.kittinunf.forge:forge:\u003clatest-version\u003e'\n}\n```\n\n### Usage (tl;dr:)\n\nGiven you have JSON as such\n\n``` Json\n{\n  \"id\": 1,\n  \"name\": \"Clementina DuBuque\",\n  \"age\": 46,\n  \"email\": \"Rey.Padberg@karina.biz\",\n  \"phone\": {\n    \"name\": \"My Phone\",\n    \"model\": \"Pixel 3XL\"\n  },\n  \"friends\": [\n    {\n        \"id\": 11,\n        \"name\": \"Ervin Howell\",\n        \"age\": 32,\n        \"email\": \"Shanna@melissa.tv\",\n        \"phone\": {\n            \"name\": \"My iPhone\",\n            \"model\": \"iPhone X\"\n        },\n        \"friends\": []\n    }\n  ],\n  \"dogs\": [\n    {\n      \"name\": \"Lucy\",\n      \"breed\": \"Dachshund\",\n      \"is_male\": false\n    }\n  ]\n}\n```\n\nYou can write your Kotlin class definition as such\n\n``` Kotlin\ndata class User(val id: Int,\n                val name: String,\n                val age: Int,\n                val email: String?,\n                val phone: Phone,\n                val friends: List\u003cUser\u003e,\n                val dogs: List\u003cDog\u003e?)\n\ndata class Phone(val name: String, val model: String)\ndata class Dog(val name: String, val breed: String, val male: Boolean)\n\nfun userDeserializer(json: JSON) =\n    ::User.create.\n        map(json at \"id\").\n        apply(json at \"name\").\n        apply(json at \"age\").\n        apply(json maybeAt \"email\").\n        apply(json.at(\"phone\", phoneDeserializer)),  // phoneDeserializer is a lambda, use it directly\n        apply(json.list(\"friends\", ::userDeserializer)).  //userDeserializer is a function, use :: as a function reference\n        apply(json.maybeList(\"dogs\", dogDeserializer))\n\nval phoneDeserializer = { json: JSON -\u003e\n    ::Phone.create.\n        map(json at \"name\").\n        apply(json at \"model\")\n}\n\nval dogDeserializer = { json: JSON -\u003e\n    ::Dog.create.\n        map(json at \"name\").\n        apply(json at \"breed\").\n        apply(json at \"is_male\")\n}\n\n```\n\nViola!, then, you can deserialize your JSON like\n\n``` Kotlin\n\n//jsonContent is when you receive data as a JSON\nval result = Forge.modelFromJson(jsonContent, ::userDeserializer)\n\nwhen (result) {\n    DeserializedResult.Success -\u003e {\n        val user = result.value\n        //success, do something with user\n    }\n\n    DeserializedResult.Failure -\u003e {\n        val error = result.error\n        //failure, do something with error\n    }\n}\n\n```\n\nYou can also serialize you Kotlin class definition as JSON such:\n\n```kotlin\nfun userSerializer(user: User) = JSON.Object(mapOf(\n    \"id\" to user.id.toJson(),\n    \"name\" to user.name.toJson(),\n    \"age\" to user.age.toJson(),\n    \"email\" to user.email.maybeJson(),\n    \"friends\" to user.friends.toJson(::userSerializer),\n    \"dogs\" to user.dogs.maybeJson(::dogSerializer)\n))\n\nfun dogSerializer(dog: Dog) = JSON.Object(mapOf(\n    \"name\" to dog.name.toJson(),\n    \"breed\" to dog.breed.toJson(),\n    \"male\" to dog.male.toJson()\n))\n\nval json = Forge.jsonFromModel(model, ::userSerializer)\n\njson.asString() \n/* ==\u003e\n{\n  \"id\": 1,\n  \"name\": \"Clementina DuBuque\",\n  \"age\": 46,\n  \"email\": \"Rey.Padberg@karina.biz\",\n  \"friends\": [\n    {\n        \"id\": 11,\n        \"name\": \"Ervin Howell\",\n        \"age\": 32,\n        \"email\": \"Shanna@melissa.tv\",\n        \"friends\": []\n    }\n  ],\n  \"dogs\": [\n    {\n      \"name\": \"Lucy\",\n      \"breed\": \"Dachshund\",\n      \"is_male\": false\n    }\n  ]\n}\n*/\n``` \n\n## Credits\n\nForge is brought to you by [contributors](https://github.com/kittinunf/Fuel/graphs/contributors).\n\n## Licenses\n\nForge is released under the [MIT](https://opensource.org/licenses/MIT) license.\n","funding_links":["https://github.com/sponsors/kittinunf"],"categories":["Kotlin","Libraries"],"sub_categories":["[Kotlin](https://github.com/JetBrains/kotlin)"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkittinunf%2FForge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkittinunf%2FForge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkittinunf%2FForge/lists"}