user375566
user375566

Reputation:

Ignoring fields with Jerkson Parsing, expected a valid value error

Say I have JSON as such:

{
   "field":{
      "nested":{
         "foo":"foo val",
         "bar":"bar val",
      },
      "toignore1":{

      },
      "toignore2":{

      }
   }
}

I can't seem to parse this correctly, and since it's possible I don't know all the fields to ingore, e.g. toignore3..., I don't want to call them out in the models. I just need a few values from the whole response. If JSON_STRING represents the JSON above, why can't I do this when parsing with Jerkson?

case class JsonModel(val field: FieldModel)
case class FieldModel(val nested: NestedModel) // ignoring other stuff here 
case class NestedModel(val foo: String, bar: String)

val parsed = parse[JsonModel](JSON_STRING)

Upvotes: 2

Views: 216

Answers (1)

chiappone
chiappone

Reputation: 2908

You can do this one of two ways:

case class CaseClassWithIgnoredField(id: Long) {
  @JsonIgnore
  val uncomfortable = "Bad Touch"
}

@JsonIgnoreProperties(Array("uncomfortable", "unpleasant"))
case class CaseClassWithIgnoredFields(id: Long) {
  val uncomfortable = "Bad Touch"
  val unpleasant = "The Creeps"
}

Upvotes: 2

Related Questions