All Projects → tsers → Zeison

tsers / Zeison

Licence: mit
Small, fast and easy-to-use JSON library for Scala.

Programming Languages

scala
5932 projects

Labels

Projects that are alternatives of or similar to Zeison

Squirrel Json
A vectorized JSON parser for pre-validated, minified documents
Stars: ✭ 43 (-4.44%)
Mutual labels:  json
Oakdex Pokedex
Ruby Gem and Node Package for comprehensive Generation 1-7 Pokedex data, including 809 Pokémon, uses JSON schemas to verify the data
Stars: ✭ 44 (-2.22%)
Mutual labels:  json
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 (+2168.89%)
Mutual labels:  json
Wc3maptranslator
Translate war3map ⇄ json formats for WarCraft III .w3x maps
Stars: ✭ 43 (-4.44%)
Mutual labels:  json
Dito
Dito.js is a declarative and modern web framework with a focus on API driven development, based on Objection.js, Koa.js and Vue.js – Released in 2018 under the MIT license, with support by Lineto.com
Stars: ✭ 44 (-2.22%)
Mutual labels:  json
Ndjson
♨️ Wicked-Fast Streaming 'JSON' ('ndjson') Reader in R
Stars: ✭ 44 (-2.22%)
Mutual labels:  json
Mkvtoolnix Batch
Windows Batch script to automate batch processing using mkvtoolnix.
Stars: ✭ 42 (-6.67%)
Mutual labels:  json
Microblob
Serve millions of JSON documents via HTTP.
Stars: ✭ 45 (+0%)
Mutual labels:  json
Snmpbot
Golang SNMP library + SNMP REST API
Stars: ✭ 44 (-2.22%)
Mutual labels:  json
Excel2json
把Excel表转换成json对象,并保存到一个文本文件中。
Stars: ✭ 1,023 (+2173.33%)
Mutual labels:  json
Jsoncsv
a command tool easily convert json file to csv or xlsx
Stars: ✭ 43 (-4.44%)
Mutual labels:  json
Jsonview
A web extension that helps you view JSON documents in the browser.
Stars: ✭ 1,021 (+2168.89%)
Mutual labels:  json
Graphql Factory
A toolkit for building GraphQL
Stars: ✭ 44 (-2.22%)
Mutual labels:  json
I18nplugin
Intellij idea i18next support plugin
Stars: ✭ 43 (-4.44%)
Mutual labels:  json
Wheel
关于net nio os cache db rpc json web http udp tcp mq 等多个小工具的自定义实现
Stars: ✭ 45 (+0%)
Mutual labels:  json
Jsonj
A fluent Java API for manipulating json data structures
Stars: ✭ 42 (-6.67%)
Mutual labels:  json
Jl Sql
SQL for JSON and CSV streams
Stars: ✭ 44 (-2.22%)
Mutual labels:  json
Fiscalberry
[JSON ↔ HW] Connect things using JSON API with the fiscalberry websocket server interact easily with any kind of Hardware. Another IoT solution...
Stars: ✭ 44 (-2.22%)
Mutual labels:  json
Json Normalizer
📃 Provides generic and vendor-specific normalizers for normalizing JSON documents.
Stars: ✭ 45 (+0%)
Mutual labels:  json
Pg variables
Session wide variables for PostgreSQL
Stars: ✭ 44 (-2.22%)
Mutual labels:  json

Zeison

Build Status Coverage

Small, fast and easy-to-use JSON library for Scala.

Motivation

Oh why? Why must JSON parsing be so challenging in Scala? First you must download tons of dependencies, then remember to use right package imports (for implicit conversions) and/or implicit formats. C'mon! JSON has only six valid data types (+ null). It's not rocket science.

Zeison tries to simplify the JSON parsing, management and rendering so that you don't need to know any implicit values or conversions. Under the hood, it uses jawn for parsing so it is fast too.

Zeison is extremely lightweight - binaries (including jawn-parser) require under 150KB of space.

Usage

To use Zeison in you project, add the following line to your build.sbt

libraryDependencies += "org.tsers.zeison" %% "zeison" % "0.9.0"

All methods and types are inside object org.tsers.zeison.Zeison so in order to use them in your code, you must add the following import

import org.tsers.zeison.Zeison
// or if you want to use methods without "Zeison." prefix
import org.tsers.zeison.Zeison._

API

Zeison API is designed to be extremely simple so that all its features can be demonstrated with one hundred LOC.

Parsing

// parse: (String) => JValue
// parse: (InputStream) => JValue
val data = Zeison.parse("""
                        | {
                        |   "messages": ["tsers", "TSERS"],
                        |   "meta": {
                        |     "numKeys": 2,
                        |     "active": true,
                        |     "score": 0.6
                        |   },
                        |   "response": null
                        | }""".stripMargin)

JSON operations

// type checking
// .isInt .isStr .isBool .isDouble .isArray .isObject .isNull .isDefined
assert(data.meta.numKeys.isInt == true)

// value extraction
assert(data.meta.numKeys.toInt == 2)
assert(data.meta.active.toBool == true)
assert(data.meta.score.toDouble == 0.6)
assert(data.meta.toMap.get("numKeys").map(_.toInt) == Some(2)) // .toMap => Map[String, JValue]
assert(data.messages.toSeq.head.toStr == "tsers")              // .toSeq => Seq[JValue]

// object/array traversal
assert(data("meta")("numKeys") == data.meta.numKeys)
assert(data.messages(0).toStr == "tsers")

// iterable behaviour (=> Iterable[JValue])
assert(data.messages.map(_.toStr).toSet == Set("tsers", "TSERS"))
assert(data.meta.toSet == Set(data.meta)) // all objects and primitives are handled as single value iterable
assert(data.response.toSet == Set.empty)  // all null values and undefined values are handled as empty iterable

// undefined values and optional extraction
assert(data.meta.numKeys.isDefined == true)
assert(data.non_existing.isDefined == false)
assert(data.messages(-1).isDefined == false)
assert(data.messages(10).isDefined == false)
assert(data.meta.numKeys.toOption.map(_.toInt) == Some(2))  // .toOption => Option[JValue]
assert(data.non_existing.toOption == None)
assert(data.response.toOption == None)

// runtime errors
assert(Try(data.messages.toInt).isSuccess == false)         // bad type cast
assert(Try(data.non_existing.toInt).isSuccess == false)     // undefined has no value
assert(Try(data.non_existing.sub_field).isSuccess == false) // undefined has no member x

// JSON creation
// ATTENTION: Zeison is NOT an object mapper!
// Only JSON primitive values, Scala Iterables/Maps and Zeison's JValue types are
// accepted - other types cause runtime exception
val obj = Zeison.toJson(Map(
  "msg"    -> "tsers!",
  "meta"   -> data.meta,
  "primes" -> Seq(1,2,3,5)
))
// or shortcuts for objects and arrays
val justObj = Zeison.toJObject("foo" -> "bar", "tsers" -> 1)
val justArr = Zeison.toJArray(1, 2, 3, "tsers", obj)

// immutable field adding
// .copy(..) works for JSON objects - other types cause runtime exception
val metaWithNewFields = obj.meta.copy("foo" -> "bar", "lol" -> "bal")
assert(metaWithNewFields != obj.meta)
assert(metaWithNewFields.foo.toStr == "bar")
assert(metaWithNewFields.numKeys.toInt == 2)

// immutable field modification
val modifiedScore = obj.meta.copy("score" -> "tsers")
assert(modifiedScore.score.toStr == "tsers")
assert(modifiedScore.numKeys.toInt == 2)

// immutable field removal
val removedScore = obj.meta.copy("score" -> Zeison.JUndefined)
assert(removedScore.score.isDefined == false)
assert(removedScore.numKeys.isDefined == true)

// object merging (shallow!)
val merged = Zeison.toJson(obj.toMap ++ data.toMap)
assert(merged.primes.isDefined)
assert(merged.messages.isDefined)

Rendering

Zeison.render(obj)
Zeison.renderPretty(obj)

Custom types

Some libraries (for example Casbah) enable non-standard JSON types. To support these libraries, Zeison provides a way to define simple custom data types that can be built, extracted and rendered from JSON objects.

def toISO8601(date: Date) = {
  val sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssz") { setTimeZone(TimeZone.getTimeZone("UTC")) }
  sdf.format(date).replaceAll("UTC$", "+00:00")
}
case class JDate(value: Date) extends Zeison.JCustom {
  override def valueAsJson: String = "\"" + toISO8601(value) + "\""
}

val now    = new Date()
val custom = Zeison.toJson(Map("createdAt" -> JDate(now)))

assert(Zeison.render(custom) == s"""{"createdAt":"${toISO8601(now)}"}""")
assert(custom.createdAt.is[Date])
assert(custom.createdAt.to[Date] == now)

License

MIT

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