pickling icon indicating copy to clipboard operation
pickling copied to clipboard

Pickling an empty list does not produce an empty JSON array

Open manuelbernhardt opened this issue 9 years ago • 5 comments

scala> import scala.pickling._, Defaults._
import scala.pickling._
import Defaults._

scala>  import json._
import json._

scala> case class Foo(list: List[String])
defined class Foo

scala> Foo(List.empty).pickle
<console>:19: warning: method newTermName in trait Names is deprecated: Use TermName instead
              Foo(List.empty).pickle
                              ^
res0: scala.pickling.json.pickleFormat.PickleType =
JSONPickle({
  "$type": "Foo",
  "list": {
    "$type": "scala.collection.immutable.Nil.type"
  }
})

I would expect list not to be an object but an empty JSON array instead

manuelbernhardt avatar Feb 24 '15 14:02 manuelbernhardt

Nevermind, I think I may have misunderstood the aim of the JSON pickler - am I right that the JSON pickler is just meant to turn a type into a string representation that happens to be JSON rather than a JSON format that is meant for manipulation as-is?

manuelbernhardt avatar Feb 24 '15 16:02 manuelbernhardt

the JSON format that comes with pickling doesn't produce very "natural" JSON. the one that's in sbt serialization generates something that's probably more what you were expecting.

havocp avatar Feb 24 '15 17:02 havocp

Yeah, I think maybe we need to contribute our JSON back, or just make a note that it exists.

We use json4s + jawn, which is not zero-dependency, hence it can't be part of the pickling library directly, but rather an external module.

jsuereth avatar Feb 28 '15 00:02 jsuereth

I think the readme or the talk said pickling's JSON implementation inlines the String operations on compile-time so the runtime performance is really good. sbt/serialization's implementation is less hardcore about runtime perf, but aims for natural, organic feelings of humans. (which is not a bad thing when you're using this for json API) We should perhaps run some benchmark and tables. Also we should make the doc page. @heathermiller was saying we can use docs.scala-lang.org.

eed3si9n avatar Feb 28 '15 00:02 eed3si9n

I'd very much welcome an indication as to the nature of the JSON being produced, perhaps going as far as to renaming it to SyntheticJSON as I believe this is not what Crockford had in mind when he set out to create JSON.

To clarify: the use-case I'm having at the moment is to implement a CQRS system where the UI is in Javascript, all Commands and Events implementing the same traits. Therefore I was looking for a tool to allow de/serialization of the messages using a type hint of sorts. @yanns pointed me to https://github.com/sphereio/sphere-scala-libs/tree/master/json which I am going to give a shot now. Would sbt-serialization support this use-case as well?

manuelbernhardt avatar Mar 02 '15 10:03 manuelbernhardt