com-lihaoyi / upickle

uPickle: a simple, fast, dependency-free JSON & Binary (MessagePack) serialization library for Scala
https://com-lihaoyi.github.io/upickle
MIT License
707 stars 158 forks source link

UPickle changes the meaning of an empty string encoded as a union #478

Closed Quafadas closed 1 year ago

Quafadas commented 1 year ago

I am ashamed to report this, and I'm not 100% confident it's truly bug. It cost me some pain to figure out what was happening, so it's at least recorded here for posterity;

type SC = "" | "moo" 

@main def testEmptyUnion = 
  val start = """""""" 
  val pt1_read : SC = upickle.default.read[SC](start)
  val pt1_write = upickle.default.write[SC](pt1_read)
  println("read")
  println(pt1_read)
  println("Write")
  println(pt1_write)
  println("End")

This script prints the following;

read
""
Write
"\"\""
End

I think there are too many quotes when the empty string is written as a union. It is different to the encoding of just the empty string, so that behaviour at least is not intuitive for me. Arguably, this "read / write" operation is changing it's data and my calling program got grumpy about that, and I struggled a little to figure out why.

The empty string as a String type, seems to yield differnt.

@main def testEmpty =
  val start = """"""""
  val pt1_read = upickle.default.read[String](start)
  println("read")
  println(pt1_read)
  val pt1_write = upickle.default.write[String](pt1_read)
  println("Write")
  println(pt1_write)

Produces

read

Write
""