Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implicit conversion can easily lead to lost data #512

Open
szeiger opened this issue Aug 11, 2023 · 0 comments
Open

Implicit conversion can easily lead to lost data #512

szeiger opened this issue Aug 11, 2023 · 0 comments

Comments

@szeiger
Copy link

szeiger commented Aug 11, 2023

After refactoring some code from putting data into an ujson.Obj to directly updating the underlying LinkedHashMap we were left with code like this:

    val j = mutable.LinkedHashMap[String, ujson.Value]()
    j.value.put("n", ujson.Str(name))
    ujson.Obj(j)

It compiles without warnings but the n field never makes it into the object. The unnecessary .value call which got the underlying map from the previously used Obj now triggers an implicit conversion that copies the map:

  implicit def JsonableDict[T](items: TraversableOnce[(String, T)])
                              (implicit f: T => Value): Obj = Obj.from(items.map(x => (x._1, f(x._2))))

The new entry is only written into the copy and immediately discarded.

I'm not sure what the right solution is. Clearly .value is necessary. The implicit conversion could be removed but it may make the API harder to use in some cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant