Concatenate this JValue
with another JValue
.
Concatenate this JValue
with another JValue
.
Example:
> JArray(JInt(1) :: JInt(2) :: Nil) ++ JArray(JInt(3) :: Nil) res0: JArray(List(JInt(1), JInt(2), JInt(3)))
Find immediate children of this JValue
that match a specific JValue
subclass.
Find immediate children of this JValue
that match a specific JValue
subclass.
This methid will search a JObject
or JArray
for values of a specific type and
return a List
of those values if they any are found.
So given some JSON like so:
[ { "thinga":1, "thingb":"bacon" },{ "thingc":3, "thingd":"Wakka" },{ "thinge":{ "thingf":4 }, "thingg":true } ]
You would use this method like so:
scala> json \ classOf[JInt] res0: List[net.liftweb.json.JInt#Values] = List(1, 3)
This method does require that whatever type you're searching for is subtype of JValue
.
An XPath-like expression to find a child of a JObject
or a JArray
of JObject
by name.
An XPath-like expression to find a child of a JObject
or a JArray
of JObject
by name. If you call this method on anything other than a JObject
or JArray
of JObject
s
you'll get a JNothing
.
This method is most useful if you have an object that you need to dig into in order to retrieve a specific value. So, let's say that you had a JSON object that looked something like this:
{ "name": "Joe", "profession": "Software Engineer", "catchphrase": { "name": "Alabama Cheer", "value": "Roll tide" } }
If for some reason you're interested in taking a look at Joe's catchphrase, you can
query it using the \
method to find it like so:
Example:
scala> json \ "catchphrase" res0: JValue = JObject(List(JField("name", JString("Alabama Cheer")), JField("value", JString("Roll tide"))))
Likewise, if you wanted to find Joe's name you could do the following:
scala> json \ "name" res0: JValue = JString("Joe")
The result could be any subclass of JValue
.
In the event that the JValue
you're operating on is actually an array of objects, you'll
get back a JArray
of the result of executing \
on each object in the array. In the event
nothing is found, you'll get a JNothing
.
Find all descendants of this JValue
that match a specific JValue
subclass.
Find all descendants of this JValue
that match a specific JValue
subclass.
Unlike its cousin \
, this method will recurse down into all children looking for
type matches searching a JObject
or JArray
for values of a specific type and
return a List
of those values if they are found.
So given some JSON like so:
[ { "thinga":1, "thingb":"bacon" },{ "thingc":3, "thingd":"Wakka" },{ "thinge":{ "thingf":4 }, "thingg":true } ]
You would use this method like so:
scala> json \\ classOf[JInt] res0: List[net.liftweb.json.JInt#Values] = List(1, 3, 4)
Find all children of a JObject
with the matching name, returning an empty JObject
if
no matches are found.
Find all children of a JObject
with the matching name, returning an empty JObject
if
no matches are found.
For example given this example JSON:
{ "name": "Joe", "profession": "Software Engineer", "catchphrase": { "name": "Alabama Cheer", "value": "Roll tide" } }
We might do the following:
scala> json \\ "name" res2: JValue = JObject(List(JField(name,JString(Joe)), JField(name,JString(Alabama Cheer))))
Return the element in the i
-th position from a JArray
.
Return the element in the i
-th position from a JArray
.
Will return JNothing
when invoked on any other kind of JValue
.
For example:
scala> val array = JArray(JInt(1) :: JInt(2) :: Nil) array: net.liftweb.json.JsonAST.JArray = JArray(List(JInt(1), JInt(2))) scala> array(1) res0: net.liftweb.json.JsonAST.JValue = JInt(2)
Return direct child elements of this JValue
, if this JValue
is a JObject
or JArray
.
Return direct child elements of this JValue
, if this JValue
is a JObject
or JArray
.
This method is useful for getting all the values of a JObject
or JArray
and will return them as a
List[JValue]
. If the JValue
you invoke this method on is not a JObject
or JArray
you will instead
get Nil
.
Example:
> JArray(JInt(1) :: JInt(2) :: Nil).children List(JInt(1), JInt(2))
Return a diff.
Return a diff.
net.liftweb.json.Diff#diff
Extract a value into a concrete Scala instance from its JValue
representation.
Extract a value into a concrete Scala instance from its JValue
representation.
Value can be:
Example:
> case class Person(name: String) > JObject(JField("name", JString("joe")) :: Nil).extract[Person] res0: Person("joe")
Optionally extract a value into a concrete Scala instance from its JValue
representation.
Optionally extract a value into a concrete Scala instance from its JValue
representation.
This method will attempt to extract a concrete Scala instance of type A
, but if it fails
it will return a scala.None
instead of throwing an exception as extract
would.
Value can be:
Example:
scala> case class Person(name: String) defined class Person scala> implicit val formats = DefaultFormats formats: net.liftweb.json.DefaultFormats.type = net.liftweb.json.DefaultFormats$@39afbb7c scala> JObject(JField("name", JString("joe")) :: Nil).extractOpt[Person] res1: Option[Person] = Some(Person(joe))
Attempt to extract a concrete Scala instance of type A
from this JValue
and, on failing to do so, return
the default value instead.
Attempt to extract a concrete Scala instance of type A
from this JValue
and, on failing to do so, return
the default value instead.
Value can be:
Example:
> case class Person(name: String) > JNothing.extractOrElse(Person("joe")) res0: Person("joe")
Return a List of all values which matches the given predicate, recursively.
Return a List of all values which matches the given predicate, recursively.
Example:
> JArray(JInt(1) :: JInt(2) :: Nil) filter { case JInt(x) => x > 1 case _ => false } res0: List[net.liftweb.json.JsonAST.JValue] = List(JInt(2))
This operates recursively, so nested objects work too:
> ((("boom" -> ("slam" -> "hello")) ~ ("shap" -> 3)): JObject) filter { case JString("hello") => true case _ => false } res0: List[net.liftweb.json.JsonAST.JValue] = List(JString(hello))
Return a List
of all fields that match the given predicate.
Return a List
of all fields that match the given predicate. Does not
recurse into child elements, so this will only check a JObject
's field
values.
Example:
> JObject(JField("age", JInt(10))) filterField { case JField("age", JInt(x)) if x > 18 => true case _ => false } res0: List[net.liftweb.json.JsonAST.JField] = List() > JObject(JField("age", JInt(10))) filterField { case JField("age", JInt(x)) if x < 18 => true case _ => false } res1: List[net.liftweb.json.JsonAST.JField] = List(JField(age,JInt(10)))
A List
of JField
s that match the given predicate p
, or Nil
if this JValue
is not a JObject
.
Return the first element from a JValue
which matches the given predicate.
Return the first element from a JValue
which matches the given predicate.
Example:
> JArray(JInt(1) :: JInt(2) :: Nil) find { _ == JInt(2) } res0: Option[net.liftweb.json.JsonAST.JValue] = Some(JInt(2))
Return the first field from this JValue
which matches the given predicate.
Return the first field from this JValue
which matches the given predicate.
When invoked on a JObject
it will first attempt to see if the JObject
has the field defined on it.
Not finding the field defined, this method will recurse into the fields of that object and search for the
value there. When invoked on or encountering a JArray
during recursion this method will run its search
on each member of the JArray
.
Example:
> JObject(JField("age", JInt(2))) findField { case JField(n, v) => n == "age" } res0: Option[net.liftweb.json.JsonAST.JField] = Some(JField(age,JInt(2))
Fold over JValue
s by applying a function to each element.
Fold over JValue
s by applying a function to each element.
The initial value for the fold.
The function to apply, which takes an accumulator and the next item as paramaters.
Fold over a series of JField
s applying a function to each one.
Fold over a series of JField
s applying a function to each one.
The initial value for the fold.
The function to apply, which takes an accumulator as its first parameter and the next field as its second.
Return a new JValue
resulting from applying the given function to each value, recursively.
Return a new JValue
resulting from applying the given function to each value, recursively.
If this function is invoked on a JObject
, it will iterate over the field values of that JObject
.
If this function is invoked on a JArray
, it will iterate over the values of that JArray
.
If this function is invoked on any other kind of JValue
it will simply pass that instance into the
function you have provided.
Example:
JArray(JInt(1) :: JInt(2) :: Nil) map { case JInt(x) => JInt(x+1) case x => x }
Return a new JValue
resulting from applying the given function to each JField
in a JObject
or a
JArray
of JObject
, recursively.
Return a JSON where all values matching the given predicate are removed.
Return a JSON where all values matching the given predicate are removed.
Example:
> JArray(JInt(1) :: JInt(2) :: JNull :: Nil).remove(_ == JNull) res0: net.liftweb.json.JsonAST.JValue = JArray(List(JInt(1), JInt(2), JNothing))
Return a JValue
where all fields matching the given predicate are removed.
Return a JValue
where all fields matching the given predicate are removed.
Example:
> JObject(JField("age", JInt(10))) removeField { case JField("age", _) => true case _ => false }
Return a new JValue
resulting from replacing the value at the specified field
path with the replacement value provided.
Return a new JValue
resulting from replacing the value at the specified field
path with the replacement value provided. This has no effect if the path
is empty or if the value is not a JObject
instance.
Example:
> JObject(List(JField("foo", JObject(List(JField("bar", JInt(1))))))).replace("foo" :: "bar" :: Nil, JString("baz")) JObject(List(JField("foo", JObject(List(JField("bar", JString("baz")))))))
Return a new JValue
resulting from applying the given partial function
to each value within this JValue
.
Return a new JValue
resulting from applying the given partial function
to each value within this JValue
.
If this is a JArray
, this means we will transform each value in the
array and return an updated array.
If this is a JObject
, this means we will transform the value of each
field of the object and the object in turn and return an updated object.
If this is another type of JValue
, the value is transformed directly.
Note that this happens recursively, so you will receive both each value in an array and the array itself, or each field value in an object and the object itself. If an array contains arrays, we will recurse into them in turn.
Examples:
> JArray(JInt(1) :: JInt(2) :: Nil) transform { case JInt(x) => JInt(x+1) } res0: net.liftweb.json.JsonAST.JValue = JArray(List(JInt(2), JInt(3)))
Without type matching, notice that we get the result of the transform replacing the array:
> JArray(JInt(1) :: JInt(2) :: Nil) transform { case _ => JString("hello") } res0: net.liftweb.json.JsonAST.JValue = JString("hello")
This JValue
with its child values recursively transformed by
the given PartialFunction
, when defined. If the
PartialFunction
is undefined, leaves the child values
untouched.
Return a new JValue
resulting from applying the given partial function f
to each field in JSON.
Return a new JValue
resulting from applying the given partial function f
to each field in JSON.
Example:
JObject(("age", JInt(10)) :: Nil) transformField { case ("age", JInt(x)) => ("age", JInt(x+1)) }
Return a representation of the values in this JValue
in a native Scala structure.
Create a new instance of WithFilter
for Scala to use when using
this JValue
in a for comprehension.
Create a new instance of WithFilter
for Scala to use when using
this JValue
in a for comprehension.