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

case class JsObject(fields: Map[String, JsValue]) extends JsValue with Product with Serializable

Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. JsObject
  2. Serializable
  3. Product
  4. Equals
  5. JsValue
  6. Dynamic
  7. AnyRef
  8. Any
Implicitly
  1. by jsObjectTopLevel
  2. by any2stringadd
  3. by StringFormat
  4. by Ensuring
  5. by ArrowAssoc
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. Protected

Instance Constructors

  1. new JsObject(fields: Map[String, JsValue])

Value Members

  1. final def !=(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  2. final def ##: Int
    Definition Classes
    AnyRef → Any
  3. def +(other: String): String
    Implicit
    This member is added by an implicit conversion from JsObject toany2stringadd[JsObject] performed by method any2stringadd in scala.Predef.
    Definition Classes
    any2stringadd
  4. def ++=(that: JsObject): JsObject

    Deep merge another object into this object.

    Deep merge another object into this object.

    that

    the object to merge into this object.

    returns

    the merged object

  5. def ->[B](y: B): (JsObject, B)
    Implicit
    This member is added by an implicit conversion from JsObject toArrowAssoc[JsObject] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @inline()
  6. final def ==(arg0: Any): Boolean
    Definition Classes
    AnyRef → Any
  7. def apply(key: String): JsValue
    Definition Classes
    JsObjectJsValue
  8. def apply(range: Range): JsArray
    Definition Classes
    JsValue
  9. def apply(start: Int, end: Int, step: Int): JsArray
    Definition Classes
    JsValue
  10. def apply(start: Int, end: Int): JsArray
    Definition Classes
    JsValue
  11. def apply(index: Int): JsValue
    Definition Classes
    JsValue
  12. def applyDynamic(key: String)(index: Int): JsValue
    Definition Classes
    JsValue
  13. def asBoolean: Boolean
    Definition Classes
    JsValue
  14. def asDate: LocalDate
    Definition Classes
    JsValue
  15. def asDateTime: LocalDateTime
    Definition Classes
    JsValue
  16. def asDecimal: BigDecimal
    Definition Classes
    JsValue
  17. def asDouble: Double
    Definition Classes
    JsValue
  18. final def asInstanceOf[T0]: T0
    Definition Classes
    Any
  19. def asInt: Int
    Definition Classes
    JsValue
  20. def asLong: Long
    Definition Classes
    JsValue
  21. def asTime: LocalTime
    Definition Classes
    JsValue
  22. def asTimestamp: Timestamp
    Definition Classes
    JsValue
  23. def canEqual(that: Any): Boolean
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Equals
  24. def clone(): AnyRef
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.CloneNotSupportedException]) @native()
  25. def compactPrint: String
    Definition Classes
    JsValue
  26. def contains(key: String): Boolean

    Tests whether this object contains a field.

  27. def ensuring(cond: (JsObject) => Boolean, msg: => Any): JsObject
    Implicit
    This member is added by an implicit conversion from JsObject toEnsuring[JsObject] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  28. def ensuring(cond: (JsObject) => Boolean): JsObject
    Implicit
    This member is added by an implicit conversion from JsObject toEnsuring[JsObject] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  29. def ensuring(cond: Boolean, msg: => Any): JsObject
    Implicit
    This member is added by an implicit conversion from JsObject toEnsuring[JsObject] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  30. def ensuring(cond: Boolean): JsObject
    Implicit
    This member is added by an implicit conversion from JsObject toEnsuring[JsObject] performed by method Ensuring in scala.Predef.
    Definition Classes
    Ensuring
  31. final def eq(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  32. def exists(p: (JsArray) => Boolean): Boolean
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  33. val fields: Map[String, JsValue]
  34. def filterOrElse[A1 >: A](p: (JsArray) => Boolean, zero: => A1): Either[A1, JsArray]
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  35. def finalize(): Unit
    Attributes
    protected[lang]
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.Throwable])
  36. def flatMap[A1 >: A, B1](f: (JsArray) => Either[A1, B1]): Either[A1, B1]
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  37. def flatten[A1 >: A, B1](implicit ev: <:<[JsArray, Either[A1, B1]]): Either[A1, B1]
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  38. def fold[C](fa: (JsObject) => C, fb: (JsArray) => C): C
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  39. def forall(f: (JsArray) => Boolean): Boolean
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  40. def foreach[U](f: (JsArray) => U): Unit
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  41. def get(key: String): Option[JsValue]
    Definition Classes
    JsObjectJsValue
  42. final def getClass(): Class[_ <: AnyRef]
    Definition Classes
    AnyRef → Any
    Annotations
    @native()
  43. def getOrElse[B1 >: B](or: => B1): B1
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  44. final def isInstanceOf[T0]: Boolean
    Definition Classes
    Any
  45. def isLeft: Boolean
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  46. def isRight: Boolean
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  47. def joinLeft[A1 >: A, B1 >: B, C](implicit ev: <:<[A1, Either[C, B1]]): Either[C, B1]
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  48. def joinRight[A1 >: A, B1 >: B, C](implicit ev: <:<[B1, Either[A1, C]]): Either[A1, C]
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  49. def left: LeftProjection[JsObject, JsArray]
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  50. def map[B1](f: (JsArray) => B1): Either[JsObject, B1]
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  51. final def ne(arg0: AnyRef): Boolean
    Definition Classes
    AnyRef
  52. final def notify(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  53. final def notifyAll(): Unit
    Definition Classes
    AnyRef
    Annotations
    @native()
  54. def orElse[A1 >: A, B1 >: B](or: => Either[A1, B1]): Either[A1, B1]
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  55. def prettyPrint: String
    Definition Classes
    JsValue
  56. def productArity: Int
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Product
  57. def productElement(n: Int): Any
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Product
  58. def productElementName(n: Int): String
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Product
  59. def productElementNames: Iterator[String]
    Definition Classes
    Product
  60. def productIterator: Iterator[Any]
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Product
  61. def productPrefix: String
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Product
  62. def remove(key: String): Option[JsValue]
    Definition Classes
    JsObjectJsValue
  63. def remove(index: Int): JsValue
    Definition Classes
    JsValue
  64. def selectDynamic(key: String): JsValue
    Definition Classes
    JsObjectJsValue
  65. def swap: Either[JsArray, JsObject]
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  66. final def synchronized[T0](arg0: => T0): T0
    Definition Classes
    AnyRef
  67. def toOption: Option[JsArray]
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  68. def toSeq: Seq[JsArray]
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  69. def toString(): String
    Definition Classes
    JsValue → AnyRef → Any
  70. def toTry(implicit ev: <:<[JsObject, Throwable]): Try[JsArray]
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
  71. def update(key: String, value: JsValue): JsValue
    Definition Classes
    JsObjectJsValue
  72. def update(index: Int, value: JsValue): JsValue
    Definition Classes
    JsValue
  73. def updateDynamic(key: String)(value: JsValue): JsValue
    Definition Classes
    JsObjectJsValue
  74. def updateDynamic(index: Int)(value: JsValue): JsValue
    Definition Classes
    JsValue
  75. final def wait(): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  76. final def wait(arg0: Long, arg1: Int): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException])
  77. final def wait(arg0: Long): Unit
    Definition Classes
    AnyRef
    Annotations
    @throws(classOf[java.lang.InterruptedException]) @native()

Shadowed Implicit Value Members

  1. final def contains[B1 >: B](elem: B1): Boolean
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Shadowing
    This implicitly inherited member is shadowed by one or more members in this class.
    To access this member you can use a type ascription:
    (jsObject: JsTopLevel).contains(elem)
    Definition Classes
    Either
  2. def productElementNames: Iterator[String]
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Shadowing
    This implicitly inherited member is shadowed by one or more members in this class.
    To access this member you can use a type ascription:
    (jsObject: JsTopLevel).productElementNames
    Definition Classes
    Product

Deprecated Value Members

  1. def formatted(fmtstr: String): String
    Implicit
    This member is added by an implicit conversion from JsObject toStringFormat[JsObject] performed by method StringFormat in scala.Predef.
    Definition Classes
    StringFormat
    Annotations
    @deprecated @inline()
    Deprecated

    (Since version 2.12.16) Use formatString.format(value) instead of value.formatted(formatString), or use the f"" string interpolator. In Java 15 and later, formatted resolves to the new method in String which has reversed parameters.

  2. def right: RightProjection[JsObject, JsArray]
    Implicit
    This member is added by an implicit conversion from JsObject toJsTopLevel performed by method jsObjectTopLevel in smile.json.
    Definition Classes
    Either
    Annotations
    @deprecated
    Deprecated

    (Since version 2.13.0) Either is now right-biased, use methods directly on Either

  3. def [B](y: B): (JsObject, B)
    Implicit
    This member is added by an implicit conversion from JsObject toArrowAssoc[JsObject] performed by method ArrowAssoc in scala.Predef.
    Definition Classes
    ArrowAssoc
    Annotations
    @deprecated
    Deprecated

    (Since version 2.13.0) Use -> instead. If you still wish to display it as one character, consider using a font with programming ligatures such as Fira Code.

Inherited from Serializable

Inherited from Product

Inherited from Equals

Inherited from JsValue

Inherited from Dynamic

Inherited from AnyRef

Inherited from Any

Inherited by implicit conversion jsObjectTopLevel fromJsObject to JsTopLevel

Inherited by implicit conversion any2stringadd fromJsObject to any2stringadd[JsObject]

Inherited by implicit conversion StringFormat fromJsObject to StringFormat[JsObject]

Inherited by implicit conversion Ensuring fromJsObject to Ensuring[JsObject]

Inherited by implicit conversion ArrowAssoc fromJsObject to ArrowAssoc[JsObject]

Ungrouped