Packages

  • package root

    JSON (JavaScript Object Notation) is a lightweight data-interchange format.

    JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages,

    JSON has only types of string, number, boolean, object, array, and null. This library includes additional types such as date, int, long, double, counter, binary, UUID, ObjectId (as in BSON), etc.

    It is very easy to parse a JSON object:

    val doc =
      json"""
      {
        "store": {
          "book": [
            {
              "category": "reference",
              "author": "Nigel Rees",
              "title": "Sayings of the Century",
              "price": 8.95
            },
            {
              "category": "fiction",
              "author": "Evelyn Waugh",
              "title": "Sword of Honour",
              "price": 12.99
            },
            {
              "category": "fiction",
              "author": "Herman Melville",
              "title": "Moby Dick",
              "isbn": "0-553-21311-3",
              "price": 8.99
            },
            {
              "category": "fiction",
              "author": "J. R. R. Tolkien",
              "title": "The Lord of the Rings",
              "isbn": "0-395-19395-8",
              "price": 22.99
            }
          ],
          "bicycle": {
            "color": "red",
            "price": 19.95
          }
        }
      }
      """

    The interpolator json parse a string to JsObject. To parse an array, use the interpolator jsan to JsArray. It is also okay to embed variable references directly in processed string literals.

    val x = 1
    json"""
      {
        "x": $x
      }
    """

    If the string is not a JSON object but any other valid JSON expression, one may use parseJson method to convert the string to a JsValue.

    "1".parseJson

    The json interpolator can only be applied to string literals. If you want to parse a string variable, the parseJson method can always be employed. If you know the string contains a JSON object, you may also use the method parseJsObject.

    val s = """{"x":1}"""
    s.parseJsObject

    To serialize a JSON value (of type JsValue) in compact mode, you can just use toString. To pretty print, use the method prettyPrint.

    doc.toString
    doc.prettyPrint

    With a JsObject or JsArray, you can refer to the individual elements with a variation of array syntax, like this:

    doc("store")("bicycle")("color")
    doc("store")("book")(0)("author")

    Note that we follow Scala's array access convention by () rather than [] in JavaScript.

    Besides, you can use the dot notation to access its fields/elements just like in JavaScript:

    doc.store.bicycle.color
    doc.store.book(0).author

    It is worth noting that we didn't define the type/schema of the document while Scala is a strong type language. In other words, we have both the type safe features of strong type language and the flexibility of dynamic language in this JSON library.

    If you try to access a non-exist field, JsUndefined is returned.

    scala> doc.book
    res11: unicorn.json.JsValue = undefined

    Although there are already several nice JSON libraries for Scala, the JSON objects are immutable by design, which is a natural choice for a functional language. For database, however, data mutation is necessary. Therefore, JsObject and JsArray are mutable data structures. You can set/add a field just like in JavaScript:

    json.store.bicycle.color = "green"

    To delete a field from JsObject, use remove method:

    doc.store.book(0) remove "price"

    It is same as setting it JsUndefined:

    doc.store.book(0).price = `JsUndefined`

    To delete an element from JsArray, the remove method will effectively remove it from the array. However, setting an element to undefined doesn't reduce the array size.

    // delete the first element and array size is smaller
    doc.store.book.remove(0)
    // set the first element to undefined but array size keeps same
    doc.store.book(0) = JsUndefined

    It is also possible to append an element or another array to JsArray:

    val a = JsArray(1, 2, 3, 4)
    a += 5
    
    a ++= JsArray(5, 6)

    Common iterative operations such as foreach, map, reduce can be applied to JsArray too.

    doc.store.book.asInstanceOf[JsArray].foreach { book =>
     println(book.price)
    }

    Because Scala is a static language, it is impossible to know doc.store.book is an array at compile time. So it is typed as generic JsValue, which is the parent type of specific JSON data types. Therefore, we use asInstanceOf[JsArray] to convert it to JsArray in order to use foreach.

    Definition Classes
    root
  • package smile
    Definition Classes
    root
  • package json

    Definition Classes
    smile
  • CompactPrinter
  • JsArray
  • JsBinary
  • JsBoolean
  • JsCounter
  • JsDate
  • JsDateTime
  • JsDecimal
  • JsDouble
  • JsInt
  • JsLong
  • JsNull
  • JsObject
  • JsObjectId
  • JsString
  • JsTime
  • JsTimestamp
  • JsUUID
  • JsUndefined
  • JsValue
  • JsValueOrdering
  • JsonHelper
  • JsonParser
  • JsonPrinter
  • JsonSerializer
  • ObjectId
  • ParserInput
  • PrettyPrinter

package json

Linear Supertypes
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. json
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Type Members

  1. trait CompactPrinter extends JsonPrinter

    A JsonPrinter that produces compact JSON source without any superfluous whitespace.

    A JsonPrinter that produces compact JSON source without any superfluous whitespace. Adopt from spray-json.

  2. case class JsArray(elements: ArrayBuffer[JsValue]) extends JsValue with Iterable[JsValue] with Product with Serializable
  3. case class JsBinary(value: Array[Byte]) extends JsValue with Product with Serializable
  4. case class JsBoolean(value: Boolean) extends JsValue with Ordered[JsBoolean] with Product with Serializable
  5. case class JsCounter(value: Long) extends JsValue with Ordered[JsCounter] with Product with Serializable

    A counter is a 64 bit integer.

    A counter is a 64 bit integer. The difference from JsLong is mostly for internal representation in database. For encoding reason, the effective number of bits are 56, which should be big enough in practice.

  6. case class JsDate(value: LocalDate) extends JsValue with Ordered[JsDate] with Product with Serializable

    An immutable date without a time-zone in the ISO-8601 calendar system, often viewed as year-month-day such as 2007-12-03.

  7. case class JsDateTime(value: LocalDateTime) extends JsValue with Ordered[JsDateTime] with Product with Serializable

    An immutable date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30.

    An immutable date-time without a time-zone in the ISO-8601 calendar system, such as 2007-12-03T10:15:30. Although LocalTime/JsTime can be represented to nanosecond precision, a JSON serializer may not store the nano-of-second field to save the space. To preserve the high precision of time, JsTimestamp should be employed.

  8. case class JsDecimal(value: BigDecimal) extends JsValue with Ordered[JsDecimal] with Product with Serializable
  9. case class JsDouble(value: Double) extends JsValue with Ordered[JsDouble] with Product with Serializable
  10. case class JsInt(value: Int) extends JsValue with Ordered[JsInt] with Product with Serializable
  11. case class JsLong(value: Long) extends JsValue with Ordered[JsLong] with Product with Serializable
  12. case class JsObject(fields: Map[String, JsValue]) extends JsValue with Product with Serializable
  13. case class JsObjectId(value: ObjectId) extends JsValue with Product with Serializable
  14. case class JsString(value: String) extends JsValue with Ordered[JsString] with Product with Serializable
  15. case class JsTime(value: LocalTime) extends JsValue with Ordered[JsTime] with Product with Serializable

    An immutable time without a time-zone in the ISO-8601 calendar system, often viewed as hour-minute-second such as 10:15:30.

    An immutable time without a time-zone in the ISO-8601 calendar system, often viewed as hour-minute-second such as 10:15:30. Although LocalTime/JsTime can be represented to nanosecond precision, a JSON serializer may not store the nano-of-second field to save the space. To preserve the high precision of time, JsTimestamp should be employed.

  16. case class JsTimestamp(value: Timestamp) extends JsValue with Ordered[JsTimestamp] with Product with Serializable

    An SQL TIMESTAMP value.

    An SQL TIMESTAMP value. It adds the ability to hold the SQL TIMESTAMP fractional seconds value, by allowing the specification of fractional seconds to a precision of nanoseconds. Support the JDBC escape syntax for timestamp values.

    The precision of a Timestamp object is calculated to be either:

    • 19 , which is the number of characters in yyyy-mm-dd hh:mm:ss
    • 20 + s , which is the number of characters in the yyyy-mm-dd hh:mm:ss.[fff...] and s represents the scale of the given Timestamp, its fractional seconds precision.
  17. type JsTopLevel = Either[JsObject, JsArray]
  18. case class JsUUID(value: UUID) extends JsValue with Product with Serializable
  19. sealed trait JsValue extends Dynamic

    JSON value.

  20. implicit final class JsonHelper extends AnyVal

    String interpolator for JSON.

    String interpolator for JSON. json for JSON Object and jarr for JSON Array.

  21. class JsonParser extends AnyRef
  22. trait JsonPrinter extends (JsValue) => String

    A JsonPrinter serializes a JSON AST to a String.

    A JsonPrinter serializes a JSON AST to a String. Adopt from spray-json.

  23. class JsonSerializer extends LazyLogging

    JSON Serializer in BSON format as defined by http://bsonspec.org/spec.html.

    JSON Serializer in BSON format as defined by http://bsonspec.org/spec.html. This is not fully compatible with BSON spec, where the root must be a document/JsObject. In contrast, the root can be any JsValue in our implementation. Correspondingly, the root will always has the type byte as the first byte.

    Not Multi-threading safe. Each thread should have its own BsonSerializer instance. Data size limit to 10MB by default.

    Although JsTime/JsDateTime can be represented to nanosecond precision, we don't store the nano-of-second field to save the space. To preserve the high precision of time, JsTimestamp should be employed and of course consumes more space.

  24. case class ObjectId(id: Array[Byte]) extends Product with Serializable

    BSON's 12-byte ObjectId type, constructed using: a 4-byte value representing the seconds since the Unix epoch, a 3-byte machine identifier, a 2-byte process id, and a 3-byte counter, starting with a random value.

    BSON's 12-byte ObjectId type, constructed using: a 4-byte value representing the seconds since the Unix epoch, a 3-byte machine identifier, a 2-byte process id, and a 3-byte counter, starting with a random value.

    The implementation is adopt from ReactiveMongo.

  25. trait ParserInput extends AnyRef
  26. trait PrettyPrinter extends JsonPrinter

    A JsonPrinter that produces a nicely readable JSON source.

    A JsonPrinter that produces a nicely readable JSON source. Adopt from spray-json.

Value Members

  1. val JsFalse: JsBoolean
  2. val JsTrue: JsBoolean
  3. implicit def array2JsValue[T <: JsValue](x: Array[T]): JsArray
  4. implicit def bigdecimal2JsValue(x: BigDecimal): JsDecimal
  5. implicit def boolean2JsValue(x: Boolean): JsBoolean
  6. implicit def byteArray2JsValue(x: Array[Byte]): JsBinary
  7. implicit def date2JsValue(x: Date): JsTimestamp
  8. implicit def double2JsValue(x: Double): JsDouble
  9. implicit def int2JsValue(x: Int): JsInt
  10. implicit def jsArrayTopLevel(x: JsArray): JsTopLevel
  11. implicit def jsObjectTopLevel(x: JsObject): JsTopLevel
  12. implicit def json2BigDecimal(x: JsDecimal): BigDecimal
  13. implicit def json2Binary(x: JsBinary): Array[Byte]
  14. implicit def json2Boolean(x: JsBoolean): Boolean
  15. implicit def json2Date(x: JsTimestamp): Date
  16. implicit def json2Date(x: JsDate): LocalDate
  17. implicit def json2DateTime(x: JsDateTime): LocalDateTime
  18. implicit def json2Double(x: JsDouble): Double
  19. implicit def json2Int(x: JsInt): Int
  20. implicit def json2Long(x: JsLong): Long
  21. implicit def json2ObjectId(x: JsObjectId): ObjectId
  22. implicit def json2String(x: JsString): String
  23. implicit def json2Time(x: JsTime): LocalTime
  24. implicit def json2Timestamp(x: JsTimestamp): Timestamp
  25. implicit def json2UUID(x: JsUUID): UUID
  26. implicit def localDate2JsValue(x: LocalDate): JsDate
  27. implicit def localDateTime2JsValue(x: LocalDateTime): JsDateTime
  28. implicit def localTime2JsValue(x: LocalTime): JsTime
  29. implicit def long2JsValue(x: Long): JsLong
  30. implicit def map2JsValue[T <: JsValue](x: Map[String, T]): JsObject
  31. implicit def map2JsValue(x: Map[String, JsValue]): JsObject
  32. implicit def map2JsValue[T <: JsValue](x: Seq[(String, T)]): JsObject
  33. implicit def objectId2JsValue(x: ObjectId): JsObjectId
  34. implicit def pimpBigDecimalArray(x: Array[BigDecimal]): PimpedBigDecimalSeq
  35. implicit def pimpBigDecimalMap(x: Map[String, BigDecimal]): PimpedBigDecimalMap
  36. implicit def pimpBigDecimalMutableMap(x: Map[String, BigDecimal]): PimpedBigDecimalMutableMap
  37. implicit def pimpBigDecimalSeq(x: Seq[BigDecimal]): PimpedBigDecimalSeq
  38. implicit def pimpBooleanArray(x: Array[Boolean]): PimpedBooleanSeq
  39. implicit def pimpBooleanMap(x: Map[String, Boolean]): PimpedBooleanMap
  40. implicit def pimpBooleanMutableMap(x: Map[String, Boolean]): PimpedBooleanMutableMap
  41. implicit def pimpBooleanSeq(x: Seq[Boolean]): PimpedBooleanSeq
  42. implicit def pimpDateArray(x: Array[Date]): PimpedDateSeq
  43. implicit def pimpDateArray(x: Seq[Date]): PimpedDateSeq
  44. implicit def pimpDateMap(x: Map[String, Date]): PimpedDateMap
  45. implicit def pimpDateMutableMap(x: Map[String, Date]): PimpedDateMutableMap
  46. implicit def pimpDoubleArray(x: Array[Double]): PimpedDoubleSeq
  47. implicit def pimpDoubleMap(x: Map[String, Double]): PimpedDoubleMap
  48. implicit def pimpDoubleMutableMap(x: Map[String, Double]): PimpedDoubleMutableMap
  49. implicit def pimpDoubleSeq(x: Seq[Double]): PimpedDoubleSeq
  50. implicit def pimpIntArray(x: Array[Int]): PimpedIntSeq
  51. implicit def pimpIntMap(x: Map[String, Int]): PimpedIntMap
  52. implicit def pimpIntMutableMap(x: Map[String, Int]): PimpedIntMutableMap
  53. implicit def pimpIntSeq(x: Seq[Int]): PimpedIntSeq
  54. implicit def pimpLocalDateArray(x: Array[LocalDate]): PimpedLocalDateSeq
  55. implicit def pimpLocalDateArray(x: Seq[LocalDate]): PimpedLocalDateSeq
  56. implicit def pimpLocalDateTimeArray(x: Array[LocalDateTime]): PimpedLocalDateTimeSeq
  57. implicit def pimpLocalDateTimeArray(x: Seq[LocalDateTime]): PimpedLocalDateTimeSeq
  58. implicit def pimpLocalTimeArray(x: Array[LocalTime]): PimpedLocalTimeSeq
  59. implicit def pimpLocalTimeArray(x: Seq[LocalTime]): PimpedLocalTimeSeq
  60. implicit def pimpLongArray(x: Array[Long]): PimpedLongSeq
  61. implicit def pimpLongMap(x: Map[String, Long]): PimpedLongMap
  62. implicit def pimpLongMutableMap(x: Map[String, Long]): PimpedLongMutableMap
  63. implicit def pimpLongSeq(x: Seq[Long]): PimpedLongSeq
  64. implicit def pimpString(string: String): PimpedString
  65. implicit def pimpStringArray(x: Array[String]): PimpedStringSeq
  66. implicit def pimpStringMap(x: Map[String, String]): PimpedStringMap
  67. implicit def pimpStringMutableMap(x: Map[String, String]): PimpedStringMutableMap
  68. implicit def pimpStringSeq(x: Seq[String]): PimpedStringSeq
  69. implicit def pimpTimestampArray(x: Array[Timestamp]): PimpedTimestampSeq
  70. implicit def pimpTimestampArray(x: Seq[Timestamp]): PimpedTimestampSeq
  71. implicit def seq2JsValue[T <: JsValue](x: Seq[T]): JsArray
  72. implicit def string2JsValue(x: String): JsString
  73. implicit def timestamp2JsValue(x: Timestamp): JsTimestamp
  74. implicit def uuid2JsValue(x: UUID): JsUUID
  75. object CompactPrinter extends CompactPrinter
  76. object JsArray extends Serializable
  77. object JsBoolean extends Serializable
  78. object JsCounter extends Serializable
  79. object JsDate extends Serializable
  80. object JsDateTime extends Serializable
  81. object JsDecimal extends Serializable
  82. object JsDouble extends Serializable
  83. object JsInt extends Serializable
  84. object JsLong extends Serializable
  85. case object JsNull extends JsValue with Product with Serializable
  86. object JsObject extends Serializable
  87. object JsObjectId extends Serializable
  88. object JsString extends Serializable
  89. object JsTime extends Serializable
  90. object JsTimestamp extends Serializable
  91. object JsUUID extends Serializable
  92. case object JsUndefined extends JsValue with Product with Serializable
  93. object JsValueOrdering extends Ordering[JsValue]
  94. object JsonParser

    Fast, no-dependency parser for JSON as defined by http://tools.ietf.org/html/rfc4627.

    Fast, no-dependency parser for JSON as defined by http://tools.ietf.org/html/rfc4627. Adopt from spray-json.

  95. object JsonPrinter
  96. object JsonSerializer
  97. object ObjectId extends Serializable
  98. object ParserInput
  99. object PrettyPrinter extends PrettyPrinter

Inherited from AnyRef

Inherited from Any

Ungrouped