implicit final class JsonHelper extends AnyVal
String interpolator for JSON.
json
for JSON Object and jarr
for JSON Array.
Linear Supertypes
Ordering
- Alphabetic
- By Inheritance
Inherited
- JsonHelper
- AnyVal
- Any
Implicitly
- by any2stringadd
- by StringFormat
- by Ensuring
- by ArrowAssoc
- Hide All
- Show All
Visibility
- Public
- Protected
Instance Constructors
- new JsonHelper(sc: StringContext)
Value Members
- final def !=(arg0: Any): Boolean
- Definition Classes
- Any
- final def ##: Int
- Definition Classes
- Any
- def +(other: String): String
- Implicit
- This member is added by an implicit conversion from JsonHelper toany2stringadd[JsonHelper] performed by method any2stringadd in scala.Predef.
- Definition Classes
- any2stringadd
- def ->[B](y: B): (JsonHelper, B)
- Implicit
- This member is added by an implicit conversion from JsonHelper toArrowAssoc[JsonHelper] performed by method ArrowAssoc in scala.Predef.
- Definition Classes
- ArrowAssoc
- Annotations
- @inline()
- final def ==(arg0: Any): Boolean
- Definition Classes
- Any
- final def asInstanceOf[T0]: T0
- Definition Classes
- Any
- def ensuring(cond: (JsonHelper) => Boolean, msg: => Any): JsonHelper
- Implicit
- This member is added by an implicit conversion from JsonHelper toEnsuring[JsonHelper] performed by method Ensuring in scala.Predef.
- Definition Classes
- Ensuring
- def ensuring(cond: (JsonHelper) => Boolean): JsonHelper
- Implicit
- This member is added by an implicit conversion from JsonHelper toEnsuring[JsonHelper] performed by method Ensuring in scala.Predef.
- Definition Classes
- Ensuring
- def ensuring(cond: Boolean, msg: => Any): JsonHelper
- Implicit
- This member is added by an implicit conversion from JsonHelper toEnsuring[JsonHelper] performed by method Ensuring in scala.Predef.
- Definition Classes
- Ensuring
- def ensuring(cond: Boolean): JsonHelper
- Implicit
- This member is added by an implicit conversion from JsonHelper toEnsuring[JsonHelper] performed by method Ensuring in scala.Predef.
- Definition Classes
- Ensuring
- def getClass(): Class[_ <: AnyVal]
- Definition Classes
- AnyVal → Any
- final def isInstanceOf[T0]: Boolean
- Definition Classes
- Any
- def jsan(args: Any*): JsArray
JSON array string interpolation.
- def json(args: Any*): JsObject
JSON object string interpolation.
- def toString(): String
- Definition Classes
- Any
Deprecated Value Members
- def formatted(fmtstr: String): String
- Implicit
- This member is added by an implicit conversion from JsonHelper toStringFormat[JsonHelper] performed by method StringFormat in scala.Predef.
- Definition Classes
- StringFormat
- Annotations
- @deprecated @inline()
- Deprecated
(Since version 2.12.16) Use
formatString.format(value)
instead ofvalue.formatted(formatString)
, or use thef""
string interpolator. In Java 15 and later,formatted
resolves to the new method in String which has reversed parameters.
- def →[B](y: B): (JsonHelper, B)
- Implicit
- This member is added by an implicit conversion from JsonHelper toArrowAssoc[JsonHelper] 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.
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:
The interpolator
json
parse a string toJsObject
. To parse an array, use the interpolatorjsan
toJsArray
. It is also okay to embed variable references directly in processed string literals.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 methodparseJsObject
.To serialize a JSON value (of type JsValue) in compact mode, you can just use
toString
. To pretty print, use the methodprettyPrint
.With a
JsObject
orJsArray
, you can refer to the individual elements with a variation of array syntax, like this: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.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
andJsArray
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: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.It is also possible to append an element or another array to
JsArray
:Common iterative operations such as
foreach
,map
,reduce
can be applied toJsArray
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 genericJsValue
, which is the parent type of specific JSON data types. Therefore, we useasInstanceOf[JsArray]
to convert it toJsArray
in order to useforeach
.