How to test an empty value of a json

84 views
Skip to first unread message

Corey Nolet

unread,
Nov 26, 2014, 2:47:18 PM11/26/14
to raptur...@googlegroups.com
So I have the following json:

{ people: [ { id: "1", name: "bill" }, { id: "2" }] }

What I want to do is write a quick inline filter do grab me a list of all the people who have a name. I'm trying to do this:

val json = JsonBuffer.parse(jsonString)
val peopleWithNames = json.people.as[List[Json]].filter(_.name != null)

I'm assuming since it seems you are following Martin Odersky's scala semantics pretty closely that you have opted out of using null. I've tried using None and i still can't seem to get the properly.

I see a normalizeOrEmpty() method. Do I need to call that on anything that I think could be empty?

Jon Pretty

unread,
Nov 26, 2014, 4:49:15 PM11/26/14
to raptur...@googlegroups.com
Hi Corey,

Don't use the `normalize` methods. I think you'd be best off doing the following, in 0.9.0, though it's not ideal. I've highlighted the changes in red.

// We're going to explicitly specify the return types from now on
// using a different "strategy". This was renamed to "modes.explicit"
// in v1.0.0.
import strategy.explicit

// Remember to call `.get` on the parse method now
val json = JsonBuffer.parse(jsonString).get

// and also on the first `as` method
val peopleWithNames = json.people.as[List[Json]].get.filter(_.name.as[String].opt.isDefined)

To do the same thing in v1.0.8, you'd write:

val json = JsonBuffer.parse(jsonString)
val people = json.people.as[List[Json]].filter(_.name.as[Option[String]].isDefined)

Hope that helps!

Jon


--
You received this message because you are subscribed to the Google Groups "Rapture users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rapture-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Jon Pretty | @propensive

Corey Nolet

unread,
Nov 26, 2014, 9:19:34 PM11/26/14
to raptur...@googlegroups.com
I'm using 1.0.8 at home just to test.


    val json  = Json.parse("{ \"people\":[{ \"id\":1, \"name\":\"Corey\"}, { \"id\":2}]}")

    val jsonbuffer = JsonBuffer.empty

    jsonbuffer.people = json.people

    jsonbuffer.people = jsonbuffer.people.as[List[Json]].filter(it => {
      it.name.as[Option[String]].isDefined
    })


This code throws this exception:

Exception in thread "main" rapture.data.MissingValueException: Missing value: <value>.name
at rapture.data.DataType$$anonfun$doNormalize$1$$anonfun$apply$2.liftedTree2$1(data.scala:113)
at rapture.data.DataType$$anonfun$doNormalize$1$$anonfun$apply$2.apply(data.scala:108)
at rapture.data.DataType$$anonfun$doNormalize$1$$anonfun$apply$2.apply(data.scala:103)
at rapture.core.package$$anonfun$yCombinator$1.apply(package.scala:46)
at rapture.data.DataType$class.doNormalize(data.scala:121)
at rapture.json.Json.doNormalize(json.scala:86)
at rapture.data.DataType$class.$normalize(data.scala:92)
at rapture.json.Json.$normalize(json.scala:86)
at rapture.data.DataType$$anonfun$as$1.apply(data.scala:126)
at rapture.core.modes$ThrowExceptions.wrap(modes.scala:105)
at rapture.data.DataType$class.as(data.scala:125)
at rapture.json.Json.as(json.scala:86)
at cjn.RaptureJson$$anonfun$main$1.apply(RaptureJson.scala:18)
at cjn.RaptureJson$$anonfun$main$1.apply(RaptureJson.scala:17)
at scala.collection.TraversableLike$$anonfun$filter$1.apply(TraversableLike.scala:264)
at scala.collection.immutable.List.foreach(List.scala:318)
at scala.collection.TraversableLike$class.filter(TraversableLike.scala:263)
at scala.collection.AbstractTraversable.filter(Traversable.scala:105)
at cjn.RaptureJson$.main(RaptureJson.scala:17)
at cjn.RaptureJson.main(RaptureJson.scala)

Jon Pretty

unread,
Nov 27, 2014, 11:19:57 PM11/27/14
to raptur...@googlegroups.com
Hi Corey,

Sorry for taking a bit longer this time!

That's actually a bug -- it's exactly what getting `.as[Option[T]]` is designed to avoid! Sorry...

It's quite a recent one, and something I'm looking to fix as soon as I can. It's being tracked here:

  https://github.com/propensive/rapture-json/issues/23

If you watch the ticket on Github, you should expect to see it fixed in the next couple of days. Sorry about that...

Cheers,
Jon

Jim Newsham

unread,
Feb 26, 2015, 4:36:06 PM2/26/15
to raptur...@googlegroups.com

Hi guys,

I'm using rapture-json 1.1.0 with json4s 3.2.11, and I am seeing the same behavior that issue #23 claims to have fixed.  Test code:

import rapture.json._
import rapture.json.Json
import rapture.json.jsonBackends.json4s._

object TestRaptureJsonOption extends App {

    val json = json"""{ "foo": "bar" }"""
    println(json.missing.as[Option[String]])
  
}

Exception:

Exception in thread "main" rapture.data.MissingValueException: Missing value: <value>.missing
at rapture.data.DataType$$anonfun$doNormalize$1$$anonfun$apply$2.liftedTree2$1(data.scala:113)
at rapture.data.DataType$$anonfun$doNormalize$1$$anonfun$apply$2.apply(data.scala:108)
at rapture.data.DataType$$anonfun$doNormalize$1$$anonfun$apply$2.apply(data.scala:103)
at rapture.core.package$$anonfun$yCombinator$1.apply(package.scala:41)
at rapture.data.DataType$class.doNormalize(data.scala:121)
at rapture.json.Json.doNormalize(json.scala:131)
at rapture.data.DataType$class.$normalize(data.scala:92)
at rapture.json.Json.$normalize(json.scala:131)
at rapture.data.DataType$$anonfun$as$1.apply(data.scala:126)
at rapture.core.internal.ThrowExceptionsMode.wrap(modes.scala:175)
at rapture.data.DataType$class.as(data.scala:125)
at rapture.json.Json.as(json.scala:131)
at TestRaptureJsonOption$.delayedEndpoint$TestRaptureJsonOption$1(TestRaptureJsonOption.scala:8)
at TestRaptureJsonOption$delayedInit$body.apply(TestRaptureJsonOption.scala:5)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at TestRaptureJsonOption$.main(TestRaptureJsonOption.scala:5)
at TestRaptureJsonOption.main(TestRaptureJsonOption.scala)


Any ideas?  Thanks,
Jim

Jon Pretty

unread,
Mar 1, 2015, 8:10:28 AM3/1/15
to raptur...@googlegroups.com
Hi Jim,

Sorry for the delay in responding - I've been distracted by a conference the last few days, and I'm just catching up...

Yes, I thought this issue was fixed for all backends, but evidently it's not. I've got plans to reimplement the code where this is failing (for different reasons) before my talk at Scala Days in two weeks, but hopefully I'll be able to commit and release a working version in a week or so.

I've just added a test to the test suite, which ought to be identical to the example you sent, it appears to work with the latest source version, so I've possibly fixed it in another patch.

I'll spend more time investigating over the next week, but for now, I've reopened the issue.

Cheers,
Jon



Jim Newsham

unread,
Mar 2, 2015, 5:19:01 PM3/2/15
to raptur...@googlegroups.com

No problem, thanks for the response.  Easy enough to work around in the meantime (by using a simple extractor utility method which catches the exception and returns an option).

Regards,
Jim

Jon Pretty

unread,
Mar 2, 2015, 5:34:07 PM3/2/15
to raptur...@googlegroups.com
Thanks, Jim,

An alternative is to use a different mode for the optional calls. If you include this import:

  import rapture.core.modes.returnOption._

then all calls to the `as` method will be returned as `Option`s of what they would otherwise be. It's cumbersome if you're continually switching between modes just to get `Option`s out, but if you always want `Option`s, then this should be by far the best way.

To get back to the "normal" mode, just

  import rapture.core.modes.throwExceptions._

Any use?
Jon
Reply all
Reply to author
Forward
0 new messages