On Wednesday, March 29, 2017 at 7:13:28 AM UTC-4, Konrad Malawski wrote:

> In principle sure, though not on anyones roadmap nowadays.
> You could contribute such a thing or find a json parser whihc has such 
> failure mode - I'm unaware of any parser which has such failure mode though.
>
 
Jackson has a failure mode to fail on unknown fields, and actually it's the 
default.
You need to explicitly disable it if you don't want it. Either globally
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, 
false);
or per class:
@JsonIgnoreProperties(ignoreUnknown = true)

It looks like there's some support for using jackson in 
akka-http: http://doc.akka.io/docs/akka/2.4.6/java/http/common/json-support.html
so maybe that's one way to go.


On Wednesday, March 29, 2017 at 7:54:57 AM UTC-4, Alan Burlison wrote:
>
> On 29/03/2017 12:13, Konrad Malawski wrote: 
>
> I can't think of any way of doing it that doesn't require reflection to 
> enumerate the fields in the case class that the JSON is being 
> unmarshalled in to. And although I've used Spray/JSON quite a bit I have 
> no idea how the case class/tuple marshalling/unmarshalling stuff works. 



Would it be possible to override the JsonReader and do something like:

def read(value: JsValue) = {
  val names = Set(fieldName1, fieldName2, fieldName3)
  value.asJsObject.fields.keys.filterNot(names.contains).foreach { field =>
     throw new UnknownFieldException(field)
  }
  val p1V = fromField[P1](value, fieldName1)
  val p2V = fromField[P2](value, fieldName2)
  val p3V = fromField[P3](value, fieldName3)
  construct(p1V, p2V, p3V)
}

It's using reflection but only once per case class.
If you need it for a lot of case classes you can probably generate some 
code by using the template:
https://github.com/spray/spray-json/blob/master/src/main/boilerplate/spray/json/ProductFormatsInstances.scala.template

-
Henrik

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to