Skip to content
This repository was archived by the owner on Feb 20, 2019. It is now read-only.
This repository was archived by the owner on Feb 20, 2019. It is now read-only.

Excessive tpe entries on json array elements #210

@julianrz

Description

@julianrz

When serializing an array (likely, any collection) to JSON, each element of resulting JSON array contains the (potentially lengthy - long package name!) "tpe" entry. This is redundant, since the array itself already has a tpe entry with full information on element type. If a long array is serialized, current behavior would result in a file which is a lot larger than it has to be

val um = CommitUrlEmail("a","b")
val um2 = CommitUrlEmail("a2","b2")
val umList= Array(um,um2)
val pklumList = umList.pickle
println(pklumList.value)

[  "tpe": "scala.Array[long.loog.long.long.pkg.name.CommitUrlEmail]",
  "elems": [
    {
    "tpe": "long.loog.long.long.pkg.name.CommitUrlEmail",
    "commitUrl": "a",
    "email": "b"
  },
    {
    "tpe": "long.loog.long.long.pkg.name.CommitUrlEmail",
    "commitUrl": "a2",
    "email": "b2"
  }
  ]

Activity

adamdecaf

adamdecaf commented on Feb 7, 2015

@adamdecaf

I would think that the specific type information would only be needed on collections whose types have variance. Otherwise it seems that on invariant types the extra tpe's aren't needed. Just brainstorming though.

havocp

havocp commented on Feb 7, 2015

@havocp
Contributor

over in https://github.com/sbt/serialization we have a pickle format for JSON that's a little more economical, though it does still write out the type for this case I believe. Conceptually if you never pickle/unpickle Any (meaning you only use StaticOnly mode) no type tags are needed at all for leaf types in the type hierarchy.

eed3si9n

eed3si9n commented on Feb 8, 2015

@eed3si9n
Member

Here's the case where having type info would help.

scala> :paste
// Entering paste mode (ctrl-D to finish)

sealed trait Fruit
case class Apple(kind: String) extends Fruit
case class Orange(kind: String) extends Fruit

// Exiting paste mode, now interpreting.

defined trait Fruit
defined class Apple
defined class Orange

scala> val fs: Array[Fruit] = Array(Apple("x"), Orange("y"))
fs: Array[Fruit] = Array(Apple(x), Orange(y))

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

scala> fs.pickle.value
res0: String =
{
  "$type": "scala.Array[Fruit]",
  "elems": [
    {
    "$type": "Apple",
    "kind": "x"
  },
    {
    "$type": "Orange",
    "kind": "y"
  }
  ]
}

Unfortunately, Pickling 0.10.0 won't be able to unpickle that to Array[Fruit] but that's another story.

julianrz

julianrz commented on Feb 8, 2015

@julianrz
Author

Good point, but 99% of the time a data structure will be homogeneous, or will contain 2-3 subclasses, so maybe specifying tpe1 tpe2 and tpe3 on array level, and referring to them on each element will work? Especially if it is a sealed hierarchy and all subclasses are known upfront...   

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @adamdecaf@eed3si9n@havocp@julianrz

        Issue actions

          Excessive tpe entries on json array elements · Issue #210 · scala/pickling