kmizu / jsonda   2.0.0

BSD 3-clause "New" or "Revised" License Website GitHub

A DSL for constructing json-like data sturctures

Scala versions: 2.13 2.12 2.11 2.10

Jsonda - Simple DSL for JSON objects which is independent from specific JSON library

Build Status

Gitter Build Status Maven Central Scaladoc Reference Status

This project provides a Jsonda DSL, which can be used to construct JSON object easily, in Scala. The code size of Jsonda is small and it is easy to understand what it does. Although Jsonda was formerly Jsonic, I was told that there is already another Jsonic library in Java by @okapies san. Then, I renamed Jsonic to Jsonda.

Using with sbt

If you woule like to use Jsonda with sbt, what you need to do is only adding the following lines to your build.sbt. There exists jsonda 2.0.0 for Scala.2.11.X, Scala 2.12.X, and Scala 2.13.X.

For jsonda-json4s:

libraryDependencies += "com.github.kmizu" %% "jsonda-json4s" % "2.0.0"

For jsonda-play_json:

libraryDependencies += "com.github.kmizu" %% "jsonda-play_json" % "2.0.0"

Release Note

2.0.0

  • Drop symbol literals as keys
    • such as {'key :- "value"}
    • use {"key" :- "value"} form instead
  • Support Scala 2.1.3
  • Follow Semantic Versioning from this version

1.6.0

  • Drop spray-json support
  • Add support for play-json

1.4.0

  • Drop Scala 2.9.X and Scala 2.10.X support
  • Add support for spray-json

1.2.0

  • Drop std-json and lif-json support. The reason why lift-json support is dropped is that lift-json against Scala 2.11 was not published.

Syntax

Jsonda is very simple DSL for creating JSON objects. Notations are followings:

  • object:
    %{ $key1 :- $value1; $key2 :- $value2; ... }

available from 1.0.0

    %{ $key1 := $value1; $key2 := $value2; ... }

or

    %{ $key1 :- $value1
       $key2 :- $value2 }

available from 1.0.0:

    %{ $key1 := $value1
       $key2 := $value2 }

Both have same meanings.

  • array:
    $($value1, $value2, ...)

or Traversable[JsonValueType] as the followings (new in jsonda 0.8.0):

    Seq($value1, $value2, ...)
  • primitive:
    • number(Int):
      100
      200
      300
  • number (Long)
      1000000000000L
      2000000000000L
      3000000000000L
  • number(Float):
      10.5F
      12.0F
  • number(Double):
      10.5
      20.5
  • string (from Scala's BigInt):
      BigInt("100000000000000000000")
      BigInt("2000000000000000000000000")
  • string (from Scala's BigDecimal):
      BigDecimal("1.123456789")
      BigDecimal("2.234568978")
  • string:
      "Hello, World!"
      "Hello, Scala!"
  • boolean:
      true
      false
  • null:
      JsonNull
  • Option[A]: If value of Option[A] is Some(a), a is implicitly converted. None is regarded as null in JSON.

Quick Start

Here are the way to create JSON using Jsonda (with jsonda-json4s):

import com.github.kmizu.jsonda.dsl.Json4sDSL._

val person = %{
  "name" :- "Kota Mizushima"
  "age" :- 33
}

The type of person is org.json4s.native.JValue. If you are familiar with json4s, you can easily manipulate JSON objects.

Or the below (with jsonda-play_json):

import com.github.kmizu.jsonda.dsl.PlayJsonDSL._

val person = %{
  "name" :- "Kota Mizushima"
  "age" :- 33
}

The type of person is play.api.libs.json.JsValue. If you are familiar with play-json you can easily manipulate JSON objects.

Nested JSON can be written easily as the followings:

import com.github.kmizu.jsonda.dsl.Json4sDSL._
    
val config = % {
  'name :- "a config"
  'debug :- false
  'logging :- % {
    'level :- "warn"
    'verbose :- true
  }
  'optimization :- %{
    'agressive :- true
    'inline :- true
  }
}

Scaladoc

Scaladoc is available via the following links:

License

This software is distributed under modified BSD License. See LICENSE.txt