How do I create a new, empty "typeless" JSON object?

1,919 views
Skip to first unread message

Tim Holt

unread,
Apr 27, 2012, 5:32:16 PM4/27/12
to lif...@googlegroups.com
I'm struggling with this a bit in my n00bishness with Scala and weakness with Java.

I'd like to declare a var that will hold json data, and then subsequently put data into it depending on code logic.  Here's an example...
  
var my_json = <<empty json>>

try {
    var foo_json = Xml.toJson(scala.xml.XML.logFile("./data/somefile.xml"))
    my_json = ("from_xml" -> foo_json) ~ ("status" -> "ok")
} catch {
    case e : Exception => {
       my_json = ("status" -> "omfg")
    }
}

Irrespective if my code example is the most efficient way to do this, how do I declare "my_json" so it has no data, but I can then put JSON into it later?  It's this specific desire to "declare as empty, then later put data in it based on conditions/logic" that I want.

Diego Medina

unread,
Apr 27, 2012, 8:01:23 PM4/27/12
to lif...@googlegroups.com
This seems to work:


scala> var my_json : net.liftweb.json.JsonAST.JObject = _
my_json: net.liftweb.json.JsonAST.JObject = null


scala> my_json = ("from_xml" -> "foo_json") ~ ("status" -> "ok")
my_json: net.liftweb.json.JsonAST.JObject =
JObject(List(JField(from_xml,JString(foo_json)),
JField(status,JString(ok))))

scala>


but in general I try to avoid having a var and then assign it based on
logic, this is most of the time a java pattern carried to Scala. I
have found that with some effort, you can always find a way to just
use a val.
If you post your actual code, we may be able to help you use a val,
instead of a var.

Regards,

Diego
> --
> Lift, the simply functional web framework: http://liftweb.net
> Code: http://github.com/lift
> Discussion: http://groups.google.com/group/liftweb
> Stuck? Help us help you:
> https://www.assembla.com/wiki/show/liftweb/Posting_example_code



--
Diego Medina
Lift/Scala Developer
di...@fmpwizard.com
http://www.fmpwizard.com
Message has been deleted

Tim Holt

unread,
Apr 30, 2012, 1:11:04 PM4/30/12
to lif...@googlegroups.com
(maybe double post)...

Here's an example of what I'm trying to do.  I'm building two json objects from XML files, wrapping both to catch exceptions.  If successful I want to build a "meta" json object containing the two I read.  I suppose I could nest the second try inside the first, and the last "if success" test inside the second, but that seems rather convoluted, especially if I were to take it to even greater depth or were to read in N XML files from a variable list.

            try {
                var publication_json = Xml.toJson(scala.xml.XML.loadFile("./data/publication_get.xml"))
            } catch {
                case e: Exception => {
                    log.error("Unable to read publication XML", e )
                    response.setStatus( HttpServletResponse.SC_BAD_GATEWAY)
                    success = false
                }
            }

            try {
                var subscription_json = Xml.toJson(scala.xml.XML.loadFile("./data/subscription_get.xml"))
            } catch {
                case e: Exception => {
                    log.error("Unable to read subsription XML", e )
                    response.setStatus( HttpServletResponse.SC_BAD_GATEWAY)
                    success = false
                }
            }

            if (success) {
                // --------------------------------------------------------------
                // --- Build hashmap to store context data for template render
                // --------------------------------------------------------------
                var context_json = ("publication" -> publication_json) ~ ("subscription" -> subscription_json)

David Pollak

unread,
Apr 30, 2012, 4:54:24 PM4/30/12
to lif...@googlegroups.com
Your approach to the problem is antithetical to the Lift and Scala approaches.  Using mutable variables and try/catch and setting flags and setting errors just doesn't work.

Please take a look at Helpers.tryo().

If you:

import net.liftweb.util._
import Helpers._

val response: Box[something] = 
  for {
    i18n_jason <- parse(...) ?~ "i18 parse failure" ~> 501 // error message -> HTTP response code
   pub_json <- Xml.toJson(...) ?~ "Couldn't read pub xml" ~> 501
   sub_json <- Xml.toJson(...) ?~ "Couldn't read sub xml" ~> 501 
  } yield {compute something}

Then you've got a line-by-line "perform action and continue if the action yields a valid response" way of writing your program.

Hope this helps.

Thanks,

David

On Mon, Apr 30, 2012 at 10:05 AM, Tim Holt <tim.m...@gmail.com> wrote:
Here's an example of what I had been trying.  I'm ingesting 3 different XML files into json, and if I have succeeded with all three, then I pack them into one larger json structure that's used as a context object for a template render.  The problem is that the json is created inside the "try" scope, so I can't then access it to create my "meta" json object containing the three I read in...

            try {
                var i18n_json = parse(scala.io.Source.fromFile("./locale/" + locale_code + ".json").mkString)

            } catch {
                case e: Exception => {
                    log.error("Unable to read locale file", e )

                    response.setStatus( HttpServletResponse.SC_BAD_GATEWAY)
                    success = false
                }
            }

            try {
                var publication_json = Xml.toJson(scala.xml.XML.loadFile("./data/publication_get.xml"))
            } catch {
                case e: Exception => {
                    log.error("Unable to read publication XML", e )
                    response.setStatus( HttpServletResponse.SC_BAD_GATEWAY)
                    success = false
                }
            }

            try {
                var subscription_json = Xml.toJson(scala.xml.XML.loadFile("./data/subscription_get.xml"))
            } catch {
                case e: Exception => {
                    log.error("Unable to read subsription XML", e )
                    response.setStatus( HttpServletResponse.SC_BAD_GATEWAY)
                    success = false
                }
            }

            if (success) {
                // --------------------------------------------------------------
                // --- Build hashmap to store context data for template render
                // --------------------------------------------------------------
                var context_json = ("publication" -> publication_json) ~ ("subscription" -> subscription_json) ~ ("request" -> request_json) ~ ("i18n" -> i18n_json)
            } else {
                // --- Meh
            }




On Friday, April 27, 2012 2:32:16 PM UTC-7, Tim Holt wrote:

--



--
Visi.Pro, Cloud Computing for the Rest of Us http://visi.pro
Lift, the simply functional web framework http://liftweb.net

David Pollak

unread,
Apr 30, 2012, 5:00:10 PM4/30/12
to lif...@googlegroups.com
Sorry... the code should have read:

import net.liftweb.util._
import Helpers._

val response: Box[something] = 
  for {
    i18n_jason <- tryo(parse(...)) ?~ "i18 parse failure" ~> 501 // error message -> HTTP response code
   pub_json <- tryo(Xml.toJson(...)) ?~ "Couldn't read pub xml" ~> 501
   sub_json <- tryo(Xml.toJson(...)) ?~ "Couldn't read sub xml" ~> 501 
  } yield {compute something}

Tim Holt

unread,
Apr 30, 2012, 5:06:04 PM4/30/12
to lif...@googlegroups.com
Thanks David, I'll look into this.  Mainly I was trying to leverage the lift json tools, not necessarily restructure my app's approach.  But when (coding) in Rome...
Reply all
Reply to author
Forward
0 new messages