plokhotnyuk / jsoniter-scala

Scala macros for compile-time generation of safe and ultra-fast JSON codecs + circe booster
MIT License
722 stars 94 forks source link

Path of the field with error #1129

Closed kachaps4u closed 4 months ago

kachaps4u commented 4 months ago

Hello! Let's say we have nested class:

eg: Address(streetName: String, city: String, state: String) Person(name: String, address: Option[Address])

If state is missing, then I want to get the path as $.address.state, but I am getting the message as: missing required field "state"

How do we resolve this?

In Jackson, we have Reference inside JsonMappingException to iterate and find the correct path

plokhotnyuk commented 4 months ago

Capturing JSON object keys and JSON array indexes for errors during parsing adds a lot of overhead that is paid in the happy path.

You can estimate that overhead by comparing with benchmark parsing results for real-world API message samples (Google Maps / GitHub Actions / OpenRTB / Twitter) between jsoniter-scala and smithy4s (that uses own custom jsoniter-scala codecs for that under the hood) here.

By default jsoniter-scala adds an exact position offset and optionally a hex dump of the affected place of the input that should be enough for debugging.

kachaps4u commented 4 months ago

Got it and makes sense! thank you somuch!

eg: to throw error for the state: case class Address(streetName: String, city: String, state: String) case class Person(name: String, address: Option[Address]) implicit val newCodec: JsonValueCodec[Person] = JsonCodecMaker.make val json = """{"name":"Apple","address":{"streetName":"street","city":"city","state":null}}""" val obj = readFromString(json)(newCodec)

throws error: Exception in thread "main" com.github.plokhotnyuk.jsoniter_scala.core.JsonReaderException: expected '"', offset: 0x00000047

So convert 0x00000047 hexa to decimal which is 71 (try https://www.rapidtables.com/convert/number/hex-to-decimal.html) and fetch the character. I am just adding this info here so that it helps some other folks who have similar question. Most of the time we do no want to log the hex dump because of PII