1

I have a class where employeeDetails is being read from a data store as json string.

public class Employee {
  private String name;

  @JsonProperty("employeeDetails")
  @JsonSerialize(???)
  private String employeeDetailsBlob;

  // getters and setters
}

Now I want to return employeeDetails(dynamic type) as an object in my response. I have changed the name using @JsonProperty and all the examples I have is for using @JsonSerialize(using = Employee.class).

So my response should look like this

{name: "foo", employeeDetails: { age: 21 }}

What I am getting is

{name: "foo", employeeDetails: "{ age: 21 }"}

I can add @JsonSerialize to my class but then I have to handle all the fields myself and do something like this in the overridden method.

jgen.writeFieldName("employeeDetails");
SerializedString serializedString = new SerializedString(empl.getEmployeeDetailsBlob());
jgen.writeRawValue(serializedString);

Is there a way I can do it using annotations and that too only on the field I want to change from json string to json object.

Abhijeet Ahuja
  • 5,596
  • 5
  • 42
  • 50

1 Answers1

1

Adding @JsonRawValue did the trick.

public class Employee {
   private String name;

   @JsonProperty("employeeDetails")
   @JsonRawValue
   private String employeeDetailsBlob;

   // getters and setters
}
Abhijeet Ahuja
  • 5,596
  • 5
  • 42
  • 50