Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Help with Scala parsing classes and JSON
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Sean Rhea  
View profile  
 More options Nov 4 2012, 5:05 pm
From: Sean Rhea <sean.c.r...@gmail.com>
Date: Sun, 4 Nov 2012 14:05:27 -0800
Local: Sun, Nov 4 2012 5:05 pm
Subject: Help with Scala parsing classes and JSON

scala.util.parsing.json.JSON isn't thread safe, according to the following
link:

https://issues.scala-lang.org/browse/SI-4929

In a comment in that same thread, Jason Zaugg suggests instead creating a
separate scala.util.parsing.json.Parser for each thread and using that. Can
anyone reply with a code example that implements that suggestion? I can't
figure out how to knit all of the required pieces together. (Perhaps I'm
missing some subtle difference between StdTokenParsers and
StandardTokenParsers?)

Thanks,
Sean
--
I lived for a time in New York City. Just a little bit north of that urban
prison, I knew of a beautiful flat spot among the trees, just big enough
for me to lie down. It was easy to get to from the confines of my
apartment, and I slept there often, alone under the stars. It can be done.
--Mike Clelland


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Matthew Neeley  
View profile  
 More options Nov 4 2012, 5:43 pm
From: Matthew Neeley <mnee...@gmail.com>
Date: Sun, 4 Nov 2012 14:43:36 -0800
Local: Sun, Nov 4 2012 5:43 pm
Subject: Re: Help with Scala parsing classes and JSON
You could do something like this:

import scala.util.parsing.json._

// copied implementation of JSON object from standard lib, but make it a class
class SafeJSON extends Parser {

  private def unRaw (in : Any) : Any = in match {
    case JSONObject(obj) => obj.map({ case (k,v) => (k,unRaw(v))}).toList
    case JSONArray(list) => list.map(unRaw)
    case x => x
  }

  def parseRaw(input : String) : Option[JSONType] =
    phrase(root)(new lexical.Scanner(input)) match {
      case Success(result, _) => Some(result)
      case _ => None
    }

  def parseFull(input: String): Option[Any] =
    parseRaw(input) match {
      case Some(data) => Some(resolveType(data))
      case None => None
    }

  def resolveType(input: Any): Any = input match {
    case JSONObject(data) => data.transform {
      case (k,v) => resolveType(v)
    }
    case JSONArray(data) => data.map(resolveType)
    case x => x
  }

  def perThreadNumberParser_=(f : NumericParser) { numberParser.set(f) }
  def perThreadNumberParser : NumericParser = numberParser.get()

}

// SafeJSON behaves like JSON from standard lib, but stores per-thread
parser instances
// in a thread-local variable and then delegates method calls to the
appropriate parser instance.
object SafeJSON {
  private parser = new ThreadLocal[SafeJSON] {
    override def initialValue = new SafeJSON
  }

  def parseRaw(input: String): Option[JSONType] = parser.get.parseRaw(input)
  def parseFull(input: String): Option[Any] = parser.get.parseFull(input)

  def resolveType(input: Any): Any = parser.get.resolveType(input)

  // additional methods can be similarly proxied here...

}

--Matthew


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Sean Rhea  
View profile  
 More options Nov 4 2012, 7:58 pm
From: Sean Rhea <sean.c.r...@gmail.com>
Date: Sun, 4 Nov 2012 16:58:55 -0800
Local: Sun, Nov 4 2012 7:58 pm
Subject: Re: Help with Scala parsing classes and JSON

That's perfect. Thanks, Matthew!

Sean

--
I lived for a time in New York City. Just a little bit north of that urban
prison, I knew of a beautiful flat spot among the trees, just big enough
for me to lie down. It was easy to get to from the confines of my
apartment, and I slept there often, alone under the stars. It can be done.
--Mike Clelland

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »