All Projects → kittinunf → Forge

kittinunf / Forge

Functional style JSON parsing in Kotlin

Programming Languages

kotlin
9241 projects

Projects that are alternatives of or similar to Forge

Helios
A purely functional JSON library for Kotlin built on Λrrow
Stars: ✭ 157 (+48.11%)
Mutual labels:  json, functional-programming, functional
Alembic
⚗️ Functional JSON Parser - Linux Ready 🐧
Stars: ✭ 115 (+8.49%)
Mutual labels:  json, parser, functional
Cssparser.js
cssparser.js is a parser that generate json from css with matched orders & structures.
Stars: ✭ 61 (-42.45%)
Mutual labels:  json, parser
Lambda
Fun with λ calculus!
Stars: ✭ 65 (-38.68%)
Mutual labels:  functional-programming, functional
Ramtuary
Ramda + Ramda Fantasy + Sanctuary REPL 🌿
Stars: ✭ 72 (-32.08%)
Mutual labels:  functional-programming, functional
Barely json
A Python parser for data that only looks like JSON
Stars: ✭ 56 (-47.17%)
Mutual labels:  json, parser
Kari.hpp
Experimental library for currying in C++17
Stars: ✭ 58 (-45.28%)
Mutual labels:  functional-programming, functional
Parsing With Haskell Parser Combinators
🔍 A step-by-step guide to parsing using Haskell parser combinators.
Stars: ✭ 72 (-32.08%)
Mutual labels:  parser, functional-programming
Parson
Lightweight JSON library written in C.
Stars: ✭ 965 (+810.38%)
Mutual labels:  json, parser
Bulb
A reactive programming library for JavaScript.
Stars: ✭ 84 (-20.75%)
Mutual labels:  functional-programming, functional
Internettools
XPath/XQuery 3.1 interpreter for Pascal with compatibility modes for XPath 2.0/XQuery 1.0/3.0, custom and JSONiq extensions, XML/HTML parsers and classes for HTTP/S requests
Stars: ✭ 82 (-22.64%)
Mutual labels:  json, parser
Fuego
Functional Experiment in Golang
Stars: ✭ 87 (-17.92%)
Mutual labels:  functional-programming, functional
Inferno Most Fp Demo
A demo for the ReactJS Tampa Bay meetup showing how to build a React+Redux-like architecture from scratch using Inferno, Most.js, reactive programmning, and various functional programming tools & techniques
Stars: ✭ 45 (-57.55%)
Mutual labels:  functional-programming, functional
Fast Xml Parser
Validate XML, Parse XML to JS/JSON and vise versa, or parse XML to Nimn rapidly without C/C++ based libraries and no callback
Stars: ✭ 1,021 (+863.21%)
Mutual labels:  json, parser
Funcj
Assorted functional-oriented data structures and algorithms for Java.
Stars: ✭ 60 (-43.4%)
Mutual labels:  json, functional-programming
Rexrex
🦖 Composable JavaScript regular expressions
Stars: ✭ 34 (-67.92%)
Mutual labels:  functional-programming, functional
Json Decoder
Type safe JSON decoder for TypeScript
Stars: ✭ 67 (-36.79%)
Mutual labels:  json, functional-programming
Java
jsoniter (json-iterator) is fast and flexible JSON parser available in Java and Go
Stars: ✭ 1,308 (+1133.96%)
Mutual labels:  json, parser
Immutable Tuple
Immutable finite list objects with constant-time equality testing (===) and no memory leaks.
Stars: ✭ 29 (-72.64%)
Mutual labels:  functional-programming, functional
Swiftlyext
SwiftlyExt is a collection of useful extensions for Swift 3 standard classes and types 🚀
Stars: ✭ 31 (-70.75%)
Mutual labels:  functional-programming, functional

Forge

Kotlin jcenter MavenCentral Build Status Codecov

Forge is a JSON parsing library that helps you map your Kotlin class from a JSON in a functional way. Forge is highly inspired by Aeson, JSON parsing library in Haskell.

Ideology

Have 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.

Forge aims to provide a full control over how to parse JSON into a Kotlin class, no more magic, no more annotation.

Installation

Gradle

repositories {
    jcenter() //or mavenCentral()
}

dependencies {
    compile 'com.github.kittinunf.forge:forge:<latest-version>'
}

Usage (tl;dr:)

Given you have JSON as such

{
  "id": 1,
  "name": "Clementina DuBuque",
  "age": 46,
  "email": "[email protected]",
  "phone": {
    "name": "My Phone",
    "model": "Pixel 3XL"
  },
  "friends": [
    {
        "id": 11,
        "name": "Ervin Howell",
        "age": 32,
        "email": "[email protected]",
        "phone": {
            "name": "My iPhone",
            "model": "iPhone X"
        },
        "friends": []
    }
  ],
  "dogs": [
    {
      "name": "Lucy",
      "breed": "Dachshund",
      "is_male": false
    }
  ]
}

You can write your Kotlin class definition as such

data class User(val id: Int,
                val name: String,
                val age: Int,
                val email: String?,
                val phone: Phone,
                val friends: List<User>,
                val dogs: List<Dog>?)

data class Phone(val name: String, val model: String)
data class Dog(val name: String, val breed: String, val male: Boolean)

fun userDeserializer(json: JSON) =
    ::User.create.
        map(json at "id").
        apply(json at "name").
        apply(json at "age").
        apply(json maybeAt "email").
        apply(json.at("phone", phoneDeserializer)),  // phoneDeserializer is a lambda, use it directly
        apply(json.list("friends", ::userDeserializer)).  //userDeserializer is a function, use :: as a function reference
        apply(json.maybeList("dogs", dogDeserializer))

val phoneDeserializer = { json: JSON ->
    ::Dog.create.
        map(json at "name").
        apply(json at "model")
}

val dogDeserializer = { json: JSON ->
    ::Dog.create.
        map(json at "name").
        apply(json at "breed").
        apply(json at "is_male")
}

Viola!, then, you can deserialize your JSON like


//jsonContent is when you receive data as a JSON
val result = Forge.modelFromJson(jsonContent, ::userDeserializer)

when (result) {
    DeserializedResult.Success -> {
        val user = result.value
        //success, do something with user
    }

    DeserializedResult.Failure -> {
        val error = result.error
        //failure, do something with error
    }
}

Credits

Forge is brought to you by contributors.

Licenses

Forge is released under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].