Reading and Writing Dynamic fields

5 views
Skip to first unread message

Kevin Nguyen

unread,
Jun 23, 2022, 7:03:41 PM6/23/22
to jackson-user
Hello, currently I have a byte[] coming in that looks like
{
       field1: value1 // could be any type
       field2: "value2"
       field3: smt_else
}
where both the field names and the value types are dynamic (they are response from a database).

I have a set of all the field names (called names). Currently I'm using both JsonGenerator and JsonParser like so

while (jParser.nextToken() != JsonToken.END_OBJECT) {
          String dataFieldName = jParser.getCurrentName();
          if (names.contains(dataFieldName)) {
               jParser.nextToken();
               jsonGenerator.writeStringField(dataFieldName, jParser.getText());
          }
}

This works fine but my generated json object's field values are all Strings. I was wondering if there's a jsonGenerator.writeObject or a jParser.getObject that will automatically pick up on what types it should be reading or writing? I know there's already a writeObject and readObject but those are more for POJO classes which won't work for due to the dynamic nature of my response.
Reply all
Reply to author
Forward
0 new messages