Coder Social home page Coder Social logo

ditto-serializer's Introduction

My motivation to build this library is because I didn't find a great library for JSON Serialization in Scala as ActiveModel::Serializer or fast_jsonapi so I thought, why not to build a pretty DSL for JSON Serialization in this great language?

Built with โค๏ธŽ by jdavierb

Build Status

๐Ÿ”ฅ Features

๐Ÿ“œ Table of Contents

Installation

If you use SBT you can add ditto-serializer in your project with

externalResolvers += "ditto-serializer" at "https://maven.pkg.github.com/jdaviderb/ditto-serializer"

libraryDependencies += "jdaviderb" %% "ditto-serializer" % "0.5"

DSL

Ditto serializer is very simple we only have 3 helpers in our API for build JSON

  1. attribute[T]: this helper append to field to the JSON Object
  2. list: this helper returns a new instance of DittoList (Array) you can add new a element using the method add example: list.add("new item")
  3. map: this helper returns a new instance of DittoMap [JsonObject] you can add a new (key, value) using the method set example: map.set("key", "value")

Model Definition

case class User(firstName: String, lastName: String)

Serializer Definitation

import org.jdaviderb.dittoSerializer.core.Serializer

class UserSerializer(val user: User) extends Serializer {
  attribute("name") { user.firstName }
  attribute("last_name") { user.lastName }
  attribute("full_name") { s"${user.firstName} ${user.lastName}" }
}

JSON Serialization

Using Circle

if you want to use Circle for JSON Serialization, you have to add this package in your project

externalResolvers += "ditto-circle" at "https://maven.pkg.github.com/jdaviderb/ditto-circle"

libraryDependencies += "jdaviderb" %% "ditto-circle" % "0.5"

Example:

import org.jdaviderb.DittoCircle.Enconder
import org.jdaviderb.dittoSerializer.core.Serializer

// Model definition
case class User(firstName: String, lastName: String)

// Serializer
class UserSerializer(val user: User) extends Serializer {
  attribute("name") { user.firstName }
  attribute("last_name") { user.lastName }
  attribute("full_name") { s"${user.firstName} ${user.lastName}" }
}

// JSON Serialization
val jorge = User("Jorge", "Hernandez")
val josue = User("Josue", "Hernandez")
val users = List(jorge, josue)

Enconder.from(new UserSerializer(jorge))
/*
  RESULT:
  {
    "full_name" : "Jorge Hernandez",
    "last_name" : "Hernandez",
    "name" : "Jorge"
  }
*/

Enconder.fromList(users.map(new UserSerializer(_)))
/*
  RESULT:
  [
    {
      "full_name" : "Jorge Hernandez",
      "last_name" : "Hernandez",
      "name" : "Jorge"
    },
    {
      "full_name" : "Josue Hernandez",
      "last_name" : "Hernandez",
      "name" : "Josue"
    }
  ]
*/

Using Spray

Pending

Using Json4s

Pending

Pending

  • add more documentation
  • add examples for tests
  • support Json4s
  • support Spray Json

ditto-serializer's People

Contributors

jdaviderb avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.