EzDevInfo.com

jerkson

[ABANDONED] The Scala applewood bacon to Jackson's chicken breast: JSON cordon bleu.

Jerkson (Jackson) issue with scala.runtime.BoxedUnit?

Jerkson started throwing a really strange error that I haven't seen before.

com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class scala.runtime.BoxedUnit and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationConfig.SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: scala.collection.MapWrapper["data"])

I'm parsing some basic data from an API. The class I've defined is:

case class Segmentation(
  @(JsonProperty@field)("legend_size")
  val legend_size: Int,

  @(JsonProperty@field)("data")
  val data: Data

) 

and Data looks like:

case class Data(
  @(JsonProperty@field)("series")
  val series: List[String],

  @(JsonProperty@field)("values")
  val values: Map[String, Map[String, Any]]

)

Any clue why this would be triggering errors? Seems like a simple class that Jerkson can handle.

Edit: sample data:

{"legend_size": 1, "data": {"series": ["2013-04-06", "2013-04-07", "2013-04-08", "2013-04-09", "2013-04-10", "2013-04-11", "2013-04-12", "2013-04-13", "2013-04-14", "2013-04-15"], "values": {"datapoint": {"2013-04-12": 0, "2013-04-15": 4, "2013-04-14": 0, "2013-04-08":
0, "2013-04-09": 0, "2013-04-11": 0, "2013-04-10": 0, "2013-04-13": 0, "2013-04-06": 0, "2013-04-07": 0}}}}

Source: (StackOverflow)

play, scala and jerkson noClassDefFound error

I am trying to work with jerkson in play and with scala 2.10. However, i want to load data fixtures based on a json files. for this prcoedure I'm trying to load the json with the "parse" command from jerkson. That ultimatly fails.

I'm doing this in the "override def onStart(app: Application)" function. The error:

NoClassDefFoundError: Could not initialize class com.codahale.jerkson.Json$

Any guesses why this is happening ? I have the following libs in my deps.:

"com.codahale" % "jerkson_2.9.1" % "0.5.0",
"com.cloudphysics" % "jerkson_2.10" % "0.6.3"

my parsing command is:

com.codahale.jerkson.Json.parse[Map[String,Any]](json)

Thanks in advance


Source: (StackOverflow)

Advertisements

Is there a way to loosely parse JSON with Jackson?

For instance, if

key1:value1,key2:value2

is passed into the parser, I'd like it to be treated the same as:

{"key1":"value1","key2","value2"}

Source: (StackOverflow)

Spring REST Jerkson generates array format

I am using Spring MVC 3.2.2 as my REST framework.

I am using Jerkson (Scala) Jackson wrapper.

I am trying to figure out my when my rest layer returns a single object the json String contains square brackets ([]) as it was an array (although it is not).

Any ideas?


Source: (StackOverflow)

How can I use Scala2.10+Play2.1+Jerkson?

It seems Jerkson is no more available within Play2.1 (Scala 2.10) and I cannot find a solution on the Internet.


Source: (StackOverflow)

Custom json serialization of structured scala case classes

I have some working jackson scala module code for roundtripping scala case classes. Jackson worked great for flat case classes but when I made one which contains a list of other case classes the amount of code I seemed to need was a lot. Consider:

abstract class Message
case class CardDrawn(player: Long, card: Int, mType: String = "CardDrawn") extends Message
case class CardSet(cards: List[CardDrawn], mType: String = "CardSet") extends Message

To get the CardSet to roundtrip to/from json with jackson scala module I used a custom serializer/deserializer written in java:

object ScrumGameMashaller {

  val mapper = new ObjectMapper() 
  val module = new SimpleModule("CustomSerializer")
  module.addSerializer(classOf[CardSet], new CardSetSerializer)
  module.addDeserializer(classOf[CardSet], new CardSetDeserializer)
  val scalaModule = DefaultScalaModule
  mapper.registerModule(scalaModule)
  mapper.registerModule(module)

  def jsonFrom(value: Any): String = {
    import java.io.StringWriter
    val writer = new StringWriter()
    mapper.writeValue(writer, value)
    writer.toString
  }

  private[this] def objectFrom[T: Manifest](value: String): T =
    mapper.readValue(value, typeReference[T])

  private[this] def typeReference[T: Manifest] = new TypeReference[T] {
    override def getType = typeFromManifest(manifest[T])
  }

  private[this] def typeFromManifest(m: Manifest[_]): Type = {
    if (m.typeArguments.isEmpty) { m.runtimeClass }
    else new ParameterizedType {
      def getRawType = m.runtimeClass
      def getActualTypeArguments = m.typeArguments.map(typeFromManifest).toArray
      def getOwnerType = null
    }
  }

with serializer:

public class CardSetSerializer extends JsonSerializer<CardSet> {
@Override
    public void serialize(CardSet cardSet, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
        jgen.writeStartObject();
        jgen.writeArrayFieldStart("cards");
        List<CardDrawn> cardsDrawn = cardSet.cards();
        scala.collection.Iterator<CardDrawn> iter = cardsDrawn.iterator();
        while(iter.hasNext()){
            CardDrawn cd = iter.next();
            cdSerialize(jgen,cd);
        }
        jgen.writeEndArray();
        jgen.writeStringField("mType", "CardSet");
        jgen.writeEndObject();      
    }

    private void cdSerialize(JsonGenerator jgen, CardDrawn cd) throws IOException, JsonProcessingException {
        jgen.writeStartObject();
        jgen.writeNumberField("player", cd.player());
        jgen.writeNumberField("card", cd.card());
        jgen.writeEndObject();
    }
}

and matching deserializer:

public class CardSetDeserializer extends JsonDeserializer<CardSet> {

    private static class CardDrawnTuple {
        Long player;
        Integer card;
    }

    @Override
    public CardSet deserialize(JsonParser jsonParser, DeserializationContext cxt) throws IOException, JsonProcessingException {
        ObjectCodec oc = jsonParser.getCodec();
        JsonNode root = oc.readTree(jsonParser);
        JsonNode cards = root.get("cards");
        Iterator<JsonNode> i = cards.elements();
        List<CardDrawn> cardObjects = new ArrayList<>();
        while( i.hasNext() ){
            CardDrawnTuple t = new CardDrawnTuple();
            ObjectNode c = (ObjectNode) i.next();
            Iterator<Entry<String, JsonNode>> fields = c.fields();
            while( fields.hasNext() ){
                Entry<String,JsonNode> f = fields.next();
                if( f.getKey().equals("player")) {
                    t.player = f.getValue().asLong();
                } else if( f.getKey().equals("card")){
                    t.card = f.getValue().asInt();
                } else { 
                    System.err.println(CardSetDeserializer.class.getCanonicalName()+ " : unknown field " + f.getKey());
                }
            }
            CardDrawn cd = new CardDrawn(t.player, t.card, "CardDrawn");
            cardObjects.add(cd);
        }

        return new CardSet(JavaConversions.asScalaBuffer(cardObjects).toList(), "CardSet");
    }

}

This seems like a lot code to deal with something fairly vanilla in the scala. Can this code be improved (what did I miss that jackson has to make this easy)? Else is there a library which will do structured case classes automatically? The jerkson examples looked easy but that seems to have been abandoned.


Source: (StackOverflow)

using jerkson in scala for list of lists

I have the following, and want to use jerkson in scala, but am having issues with this. I'm sure this is a very amateur error, but was hoping to get some help from here.

  scala> val orig=List("this", List("a", "b", List("c", "d")))
  orig: List[java.lang.Object] = List(this, List(a, b, List(c, d)))

 val json_ver=generate(orig)
 json_ver: String = ["this",["a","b",["c","d"]]]

 //now i want to go from json_ver back to orig list of lists
 //I've tried parse[List[Any]](json_ver)
 //parse[List[List[Any]]](json_ver)

All to no avail. I would really appreciate it if someone could point me in the right direction


Source: (StackOverflow)

Scanning a HUGE JSON file for deserializable data in Scala

I need to be able to process large JSON files, instantiating objects from deserializable sub-strings as we are iterating-over/streaming-in the file.

For example:

Let's say I can only deserialize into instances of the following:

case class Data(val a: Int, val b: Int, val c: Int)

and the expected JSON format is:

{   "foo": [ {"a": 0, "b": 0, "c": 0 }, {"a": 0, "b": 0, "c": 1 } ], 
    "bar": [ {"a": 1, "b": 0, "c": 0 }, {"a": 1, "b": 0, "c": 1 } ], 
     .... MANY ITEMS .... , 
    "qux": [ {"a": 0, "b": 0, "c": 0 }  }

What I would like to do is:

import com.codahale.jerkson.Json
val dataSeq : Seq[Data] = Json.advanceToValue("foo").stream[Data](fileStream)
// NOTE: this will not compile since I pulled the "advanceToValue" out of thin air.

As a final note, I would prefer to find a solution that involves Jerkson or any other libraries that comes with the Play framework, but if another Scala library handles this scenario with greater ease and decent performance: I'm not opposed to trying another library. If there is a clean way of manually seeking through the file and then using a Json library to continue parsing from there: I'm fine with that.

What I do not want to do is ingest the entire file without streaming or using an iterator, as keeping the entire file in memory at a time would be prohibitively expensive.


Source: (StackOverflow)

Deserializing JSON into user-defined case classes with Jerkson

I came across this excellent tutorial on processing JSON in Scala using Jerkson. In particular, I am interested in deserializing JSON into user-defined case classes. The article has a simple example

case class Simple(val foo: String, val bar: List[String], val baz: Map[String,Int])

object SimpleExample {
  def main(args: Array[String]) {
    import com.codahale.jerkson.Json._
    val simpleJson = """{"foo":42, "bar":["a","b","c"], "baz":{"x":1,"y":2}}"""
    val simpleObject = parse[Simple](simpleJson)
    println(simpleObject)
  }
}

I got this error running it, I am on Play 2.0.1, Scala 2.9.1-1, Jerkson 0.5.0.

Execution exception [[ParsingException: Unable to find a case accessor

Also found this on Google Groups but it isn't helpful.

Any ideas?


Source: (StackOverflow)

how to pass generic types to Argonaut

I am trying to wrap Argonaut (http://argonaut.io) in order to serialize/deserialize JSON in a Scala project. We where using Jerkson before but as it has been discontinued we are looking for an alternative.

This is the basic JSON wrapper

import argonaut._, Argonaut._

object Json {
  def Parse[T](input: String): T = {
    input.decodeOption[T].get
  }
}

When I try and compile this I get the following errors.

could not find implicit value for evidence parameter of type argonaut.DecodeJson[T]
    input.decodeOption[T]
                  ^
not enough arguments for method decodeOption: (implicit evidence$6: argonaut.DecodeJson[T]) Option[T].
Unspecified value parameter evidence$6.
    input.decodeOption[T]
                  ^

Any suggestions on how to fix this or pointers on what I am doing wrong would be most appreciated.

Also suggestions on alternative JSON frameworks are very welcome.

I'm kind of new to Scala/Java and how generics work there but I have been writing .NET/C# for many years.


Source: (StackOverflow)

Jerkson Json parser for scala.

I have used Jerkson for scala, to serialize my list of objects to a JSON file. I'm able to decompose the object into JSON format object and written to a file. Now, I when I want to read it into my program for further processing I get this error. FYI, my file size is 500MB and in the future might grow upto 1GB.

I saw few forums which has asked to increase the XX:MaxPermSize=256M. I'm not sure if this is going to solve my problem, even if it does for now, what is the guarantee that this might not surface later when the size of my JSON file grows to 1GB.Is there a better alternative ? Thanks!

Exception in thread "main" java.lang.OutOfMemoryError: PermGen space
    at java.lang.String.intern(Native Method)
    at org.codehaus.jackson.util.InternCache.intern(InternCache.java:41)
    at org.codehaus.jackson.sym.CharsToNameCanonicalizer.findSymbol(CharsToNameCanonicalizer.java:506)
    at org.codehaus.jackson.impl.ReaderBasedParser._parseFieldName(ReaderBasedParser.java:997)
    at org.codehaus.jackson.impl.ReaderBasedParser.nextToken(ReaderBasedParser.java:418)
    at com.codahale.jerkson.deser.ImmutableMapDeserializer.deserialize(ImmutableMapDeserializer.scala:32)
    at com.codahale.jerkson.deser.ImmutableMapDeserializer.deserialize(ImmutableMapDeserializer.scala:11)
    at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2704)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315)
    at com.codahale.jerkson.Parser$class.parse(Parser.scala:83)
    at com.codahale.jerkson.Json$.parse(Json.scala:6)
    at com.codahale.jerkson.Parser$class.parse(Parser.scala:14)
    at com.codahale.jerkson.Json$.parse(Json.scala:6)

Source: (StackOverflow)

Strings maintaining leading and trailing quotes from JSON when using Jerkson

JSON in question:

{
"search_id": "",
"type": "Search.filter",
"query": "bar,club",
"params": {
    "search_id": "",
    "user_id": "",
    "client": "ios",
    "lat": 40.73199375351,
    "lon": -74.00080404533901,
    "radius": 20
}

}

Code to Retrieve the Data:

val json = Json.parse(new String(body))
println((json \ "search_id") + " | " + (json \ "query"))
println(json)

printing just the json JsValue prints out the entire JSON as expected. printing out the first item produces: "" | "bar,club"

Why is it maintaining the quotes from JSON formatting? That's not part of the string, it's basically saying that the content inside the quotes is a string. How do I fix this?


Source: (StackOverflow)

Play! 2.1 / Why isn't my Jerkson dependency resolved?

Attempting to use the forked Jerkson library from https://github.com/randhindi/jerkson. Cloned a source dependency into the folder module and defined the following:

object ApplicationBuild extends Build {

  val appName         = "coolapp"
  val appVersion      = "1.0-SNAPSHOT"

  lazy val jerkson = Project(
    id = "jerkson",
    base = file("module"),
    settings = Seq(
      name               := "jerkson",
      organization       := "com.codahale",
      version            := "0.6.0-SNAPSHOT",
      scalaVersion       := "2.10.0"
    )
  )

  val appDependencies = Seq(
    "com.codahale" % "jerkson" % "0.6.0-SNAPSHOT",
    "jp.t2v" %% "play2.auth"      % "0.9",
    "jp.t2v" %% "play2.auth.test" % "0.9" % "test",
    "org.ocpsoft.prettytime" % "prettytime" % "1.0.8.Final",
    "com.typesafe" %% "play-plugins-redis" % "2.1-1-RC2",
    "net.databinder.dispatch" %% "dispatch-core" % "0.10.0"
  )

// resolvers follow
}

However, when I go to compile it gives me the following error:

sbt.ResolveException: unresolved dependency: com.codahale#jerkson;0.6.0-SNAPSHOT: not found

Any guesses here? Strangely this is the first time I've ever needed to resolve a source dependency so spare the ignorance. Thanks!


Source: (StackOverflow)

scala defining class with additional parameter for type

I'm using a case class which - among other parameters - is instantiated with a BigInt hexadecimal value. I use this case class to deserialize JSON messages via Jerkson/Jackson. The beauty of using Jackson is that the de/serialization works out-of-the-box for case classes based on their signature (I guess).

Now, a BigInt value in hexadecimal encoding would need to be instantiated with an additional radix parameter: BigInt(hexValue, 16). However my JSON messages don't contain such parameter. I'm looking for a solution to define this radix within my case class' definition so that Jackson would continue be able to use the class without configuration. Something like:

case class MyClass(name: String, hexValue: BigInt(hexValue, 16))

I understand that alternative approaches would be to a) define the JSON de/serialization explicitly or to b) define my own wrapper class around BigInt. However I'm looking for a more elegant and "scala-ish" solution - if there is any.

Note: Int is not sufficient, it has to be BigInt.


Source: (StackOverflow)

Could not find implicit Marshaller in Spray.io when using Jackson

I have a Spray.io directive that handles a POST and I want to use Jerkson (scala interface for Jackson) to parse the incoming JSON into the appropriate class.

post {
        path("") {
          entity(as[String]) { stuff =>
              complete {
                parse[User](stuff)
              }
          }
        }
      }

The issue is that when I go to compile, Spray goes looking for a Marshaller:

    [error] C:\project\src\main\scala\com\project\AccountService\controllers\Users.scala:53: 
could not find implicit value for evidence parameter of type
 spray.httpx.marshalling.Marshaller[com.project.AccountService.models.User]
    [error]                     parse[User](stuff)
    [error]                                ^
    [error] one error found

Do I need to write a custom Marhsaller for this? Or is my directive not written properly? And if I do need one, any good examples out there?

Thanks!


Source: (StackOverflow)