Problem
Consider a JSON:
{"foo": {"bar": {"goo": {"car": { /* some complex structure inside */ }}}}}
We need to parse and decode the car object into some model:
class Car( /* some accordingly complex structure inside */ )
We don't care about writing this model back to JSON so we only have implemented a Decoder for it:
implicit val carDecoder: Decoder[Car] = { cursor => ??? /* some custom decoder */ }
Now we're attempting to create a JSON path based optic to access it:
JsonPath.root.foo.bar.goo.car.as[Car] // does not compile: could not find implicit value for parameter encode: io.circe.Encoder[Car]
This line fails to compile since the as method requires both Decoder and Encoder (which makes it different from Json#as for example).
Having an Encoder instance could come in handy if we wanted to set/modify the source JSON with some new or update Car instance. But if we don't need to do it (I bet it is 90% or even 99% cases of what JSON paths are created for), then the Encoder instance is not used somehow and just does not allow us to read our model in this way if the model does not have an encoder.
The same can be applied to JsonTraversalPath and JsonFoldPath.
But the latter case is a bit ridiculous, because JsonFoldPath creates a Fold instance, but still requires an Encoder.
The Fold optic cannot be used for "set" or "modify" operations so that encoder comes completely redundant.
Problem
Consider a JSON:
We need to parse and decode the
carobject into some model:We don't care about writing this model back to JSON so we only have implemented a
Decoderfor it:Now we're attempting to create a JSON path based optic to access it:
This line fails to compile since the
asmethod requires bothDecoderandEncoder(which makes it different fromJson#asfor example).Having an
Encoderinstance could come in handy if we wanted to set/modify the source JSON with some new or updateCarinstance. But if we don't need to do it (I bet it is 90% or even 99% cases of what JSON paths are created for), then theEncoderinstance is not used somehow and just does not allow us to read our model in this way if the model does not have an encoder.The same can be applied to
JsonTraversalPathandJsonFoldPath.But the latter case is a bit ridiculous, because
JsonFoldPathcreates aFoldinstance, but still requires anEncoder.The
Foldoptic cannot be used for "set" or "modify" operations so thatencodercomes completely redundant.