sjson icon indicating copy to clipboard operation
sjson copied to clipboard

ignore missing fields while converting json to case class object

Open lasagvar opened this issue 11 years ago • 1 comments

Hi I have a case class .

case class User( name: Name, gender: String ,age:string}

This is a sample case class in some cases my json is incomplete like

{"name":"sagar","gendar":"male"}

Now when I am trying to convert this json to case class using following code

import sjson.json._ val js = JsValue.fromString(json); val userObj = fromjsonUser

It's giving me error . Is there any way we can create case class objects from incomplete JSON.

lasagvar avatar May 30 '13 05:05 lasagvar

Looks like you are using the reflection based API. In that case u can use ignore = true in @JsonProperty annotation. See https://github.com/debasishg/sjson/blob/master/src/test/scala/sjson/json/TestBeans.scala#L113. And in case u are using typeclass based API, you can always define your own custom typeclass instance.

debasishg avatar May 30 '13 05:05 debasishg