json4s icon indicating copy to clipboard operation
json4s copied to clipboard

strange behavior of filterField

Open divijan opened this issue 9 years ago • 8 comments

Contents of the nested JObject spill over into top level as a result of filtering. An illustration: https://gist.github.com/divijan/79d96b8fdc1848a66a36

divijan avatar Aug 01 '15 01:08 divijan

I just tried to reproduce your issue here:

https://github.com/json4s/json4s/blob/bfaffc23ff2019463101ec41edbcc1f900bc78e7/tests/src/test/scala/org/json4s/Examples.scala#L160-L183

But the example works on my machine and on Travis.

https://travis-ci.org/json4s/json4s/builds/77872948

seratch avatar Aug 30 '15 00:08 seratch

You must have missed the "strange" part. Try adding

filtered1.exists(_._1 == "description") must_== false
filtered1.exists(_._1 == "code") must_== false

and

filtered2.exists(_._1 == "description") must_== false
filtered2.exists(_._1 == "code") must_== false

into your test. These should remain nested and shouldn't show up on the top level. Note that I am using json4s-jackson 3.2.11

divijan avatar Aug 31 '15 15:08 divijan

Thanks. Acutally I agree this behavior doesn't look intuitive.

Return a List of all fields which matches the given predicate. https://github.com/json4s/json4s/blob/3.3/core/src/main/scala/org/json4s/MonadicJValue.scala

Basically methods defined on the MonadicJValue tends to scan all the fields recursively. I'm not sure we should change this behavior now.

seratch avatar Aug 31 '15 16:08 seratch

similar topic: #262

seratch avatar Sep 01 '15 14:09 seratch

@seratch any update on this? I got bitten by this too 😢

olkinn avatar Nov 06 '17 15:11 olkinn

@olkinn It is better to avoid changing existing behavior from the viewpoint of backward compatibility, but if there are many requests, I think that it is possible to add a filter method that targets only the top level.

But if you are only targeting the top level, isn't the code so difficult and you don't have to provide it as a library? Also think.

In any case, I think scaladoc needs to be added recursively.

magnolia-k avatar Jan 31 '20 14:01 magnolia-k

I got bitten by this. I see using a widely conventional name like map to do something unconventional like recursively mapping as a bug. I would not be surprised if other users of json4s have silent bugs as a result of expecting conventions to be followed.

d10n avatar May 17 '20 19:05 d10n