Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

A DSL for constructing json-like data sturctures

Fix typo

latest commit 825a9dfb29
Kota Mizushima authored
README.md

Jsonda - Simple DSL for JSON objects which is independent from specific JSON library Build 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 1.2.0 for Scala 2.9.1, Scala 2.9.2, Scala 2.10.X, Scala.2.11.X.

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

Release Note

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 :- 29
}

The type of person is org.json4s.native.JValue. If you are familiar with json4s, 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
  }
}

Jsonda 0.8.0 supports not only json4s but also lift-json, and scala.util.parsing.json. Note that the return type of % method depends on the underlying JSON library.

  • Jsonda with lift-json:
import com.github.kmizu.jsonda.dsl.LiftJsonDSL._
  • Jsonda with scala.util.parsing.json:
import com.github.kmizu.jsonda.dsl.StdJsonDSL._

Scaladoc

Scaladoc is available via the following links:

License

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

Something went wrong with that request. Please try again.