package json
- Alphabetic
- By Inheritance
- json
- AnyRef
- Any
- Hide All
- Show All
- Public
- Protected
Type Members
- 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.
- case class JsArray(elements: ArrayBuffer[JsValue]) extends JsValue with Iterable[JsValue] with Product with Serializable
- case class JsBinary(value: Array[Byte]) extends JsValue with Product with Serializable
- case class JsBoolean(value: Boolean) extends JsValue with Ordered[JsBoolean] with Product with Serializable
- 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.
- 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.
- 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.
- case class JsDecimal(value: BigDecimal) extends JsValue with Ordered[JsDecimal] with Product with Serializable
- case class JsDouble(value: Double) extends JsValue with Ordered[JsDouble] with Product with Serializable
- case class JsInt(value: Int) extends JsValue with Ordered[JsInt] with Product with Serializable
- case class JsLong(value: Long) extends JsValue with Ordered[JsLong] with Product with Serializable
- case class JsObject(fields: Map[String, JsValue]) extends JsValue with Product with Serializable
- case class JsObjectId(value: ObjectId) extends JsValue with Product with Serializable
- case class JsString(value: String) extends JsValue with Ordered[JsString] with Product with Serializable
- 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.
- 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.
- type JsTopLevel = Either[JsObject, JsArray]
- case class JsUUID(value: UUID) extends JsValue with Product with Serializable
- sealed trait JsValue extends Dynamic
JSON value.
- implicit final class JsonHelper extends AnyVal
Enable json .
- class JsonParser extends AnyRef
- 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.
- 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.
- 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.
- trait ParserInput extends AnyRef
- 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
- val JsFalse: JsBoolean
- val JsTrue: JsBoolean
- implicit def array2JsValue[T <: JsValue](x: Array[T]): JsArray
- implicit def bigdecimal2JsValue(x: BigDecimal): JsDecimal
- implicit def boolean2JsValue(x: Boolean): JsBoolean
- implicit def byteArray2JsValue(x: Array[Byte]): JsBinary
- implicit def date2JsValue(x: Date): JsTimestamp
- implicit def double2JsValue(x: Double): JsDouble
- implicit def int2JsValue(x: Int): JsInt
- implicit def jsArrayTopLevel(x: JsArray): Right[Nothing, JsArray]
- implicit def jsObjectTopLevel(x: JsObject): Left[JsObject, Nothing]
- implicit def json2BigDecimal(json: JsValue): BigDecimal
- implicit def json2BigDecimal(x: JsDecimal): BigDecimal
- implicit def json2Binary(x: JsBinary): Array[Byte]
- implicit def json2Boolean(json: JsValue): Boolean
- implicit def json2Boolean(x: JsBoolean): Boolean
- implicit def json2ByteArray(json: JsValue): Array[Byte]
- implicit def json2Date(json: JsValue): Date
- implicit def json2Date(x: JsTimestamp): Date
- implicit def json2Date(x: JsDate): LocalDate
- implicit def json2DateTime(x: JsDateTime): LocalDateTime
- implicit def json2Double(json: JsValue): Double
- implicit def json2Double(x: JsDouble): Double
- implicit def json2Int(json: JsValue): Int
- implicit def json2Int(x: JsInt): Int
- implicit def json2LocalDate(json: JsValue): LocalDate
- implicit def json2LocalDateTime(json: JsValue): LocalDateTime
- implicit def json2LocalTime(json: JsValue): LocalTime
- implicit def json2Long(json: JsValue): Long
- implicit def json2Long(x: JsLong): Long
- implicit def json2ObjectId(x: JsObjectId): ObjectId
- implicit def json2String(json: JsValue): String
- implicit def json2String(x: JsString): String
- implicit def json2Time(x: JsTime): LocalTime
- implicit def json2Timestamp(json: JsValue): Timestamp
- implicit def json2Timestamp(x: JsTimestamp): Timestamp
- implicit def json2UUID(x: JsUUID): UUID
- implicit def localDate2JsValue(x: LocalDate): JsDate
- implicit def localDateTime2JsValue(x: LocalDateTime): JsDateTime
- implicit def localTime2JsValue(x: LocalTime): JsTime
- implicit def long2JsValue(x: Long): JsLong
- implicit def map2JsValue[T <: JsValue](x: Map[String, T]): JsObject
- implicit def map2JsValue(x: Map[String, JsValue]): JsObject
- implicit def map2JsValue[T <: JsValue](x: Seq[(String, T)]): JsObject
- implicit def objectId2JsValue(x: ObjectId): JsObjectId
- implicit def pimpBigDecimalArray(x: Array[BigDecimal]): PimpedBigDecimalSeq
- implicit def pimpBigDecimalMap(x: Map[String, BigDecimal]): PimpedBigDecimalMap
- implicit def pimpBigDecimalMutableMap(x: Map[String, BigDecimal]): PimpedBigDecimalMutableMap
- implicit def pimpBigDecimalSeq(x: Seq[BigDecimal]): PimpedBigDecimalSeq
- implicit def pimpBooleanArray(x: Array[Boolean]): PimpedBooleanSeq
- implicit def pimpBooleanMap(x: Map[String, Boolean]): PimpedBooleanMap
- implicit def pimpBooleanMutableMap(x: Map[String, Boolean]): PimpedBooleanMutableMap
- implicit def pimpBooleanSeq(x: Seq[Boolean]): PimpedBooleanSeq
- implicit def pimpDateArray(x: Array[Date]): PimpedDateSeq
- implicit def pimpDateArray(x: Seq[Date]): PimpedDateSeq
- implicit def pimpDateMap(x: Map[String, Date]): PimpedDateMap
- implicit def pimpDateMutableMap(x: Map[String, Date]): PimpedDateMutableMap
- implicit def pimpDoubleArray(x: Array[Double]): PimpedDoubleSeq
- implicit def pimpDoubleMap(x: Map[String, Double]): PimpedDoubleMap
- implicit def pimpDoubleMutableMap(x: Map[String, Double]): PimpedDoubleMutableMap
- implicit def pimpDoubleSeq(x: Seq[Double]): PimpedDoubleSeq
- implicit def pimpIntArray(x: Array[Int]): PimpedIntSeq
- implicit def pimpIntMap(x: Map[String, Int]): PimpedIntMap
- implicit def pimpIntMutableMap(x: Map[String, Int]): PimpedIntMutableMap
- implicit def pimpIntSeq(x: Seq[Int]): PimpedIntSeq
- implicit def pimpLocalDateArray(x: Array[LocalDate]): PimpedLocalDateSeq
- implicit def pimpLocalDateArray(x: Seq[LocalDate]): PimpedLocalDateSeq
- implicit def pimpLocalDateTimeArray(x: Array[LocalDateTime]): PimpedLocalDateTimeSeq
- implicit def pimpLocalDateTimeArray(x: Seq[LocalDateTime]): PimpedLocalDateTimeSeq
- implicit def pimpLocalTimeArray(x: Array[LocalTime]): PimpedLocalTimeSeq
- implicit def pimpLocalTimeArray(x: Seq[LocalTime]): PimpedLocalTimeSeq
- implicit def pimpLongArray(x: Array[Long]): PimpedLongSeq
- implicit def pimpLongMap(x: Map[String, Long]): PimpedLongMap
- implicit def pimpLongMutableMap(x: Map[String, Long]): PimpedLongMutableMap
- implicit def pimpLongSeq(x: Seq[Long]): PimpedLongSeq
- implicit def pimpString(string: String): PimpedString
- implicit def pimpStringArray(x: Array[String]): PimpedStringSeq
- implicit def pimpStringMap(x: Map[String, String]): PimpedStringMap
- implicit def pimpStringMutableMap(x: Map[String, String]): PimpedStringMutableMap
- implicit def pimpStringSeq(x: Seq[String]): PimpedStringSeq
- implicit def pimpTimestampArray(x: Array[Timestamp]): PimpedTimestampSeq
- implicit def pimpTimestampArray(x: Seq[Timestamp]): PimpedTimestampSeq
- implicit def seq2JsValue[T <: JsValue](x: Seq[T]): JsArray
- implicit def string2JsValue(x: String): JsString
- implicit def timestamp2JsValue(x: Timestamp): JsTimestamp
- implicit def uuid2JsValue(x: UUID): JsUUID
- object CompactPrinter extends CompactPrinter
- object JsArray extends Serializable
- object JsBoolean extends Serializable
- object JsCounter extends Serializable
- object JsDate extends Serializable
- object JsDateTime extends Serializable
- object JsDecimal extends Serializable
- object JsDouble extends Serializable
- object JsInt extends Serializable
- object JsLong extends Serializable
- object JsNull extends JsValue with Product with Serializable
- object JsObject extends Serializable
- object JsObjectId extends Serializable
- object JsString extends Serializable
- object JsTime extends Serializable
- object JsTimestamp extends Serializable
- object JsUUID extends Serializable
- object JsUndefined extends JsValue with Product with Serializable
- object JsValueOrdering extends Ordering[JsValue]
- 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.
- object JsonPrinter
- object JsonSerializer
- object ObjectId extends Serializable
- object ParserInput
- object PrettyPrinter extends PrettyPrinter
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
. 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, 1JsUndefined
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
.