Ninja Framework Return JSON in custom format

775 views Asked by At

How do we return JSON of custom format while returning JSON in NINJA FRAMEWORK controller.

    return Results.json().render(MyPOJO);

MyPOJO class

    @Entity
    public class MyPOJO {
       private String Name;
       private String Value;

       public String getName() {
           return Name;
       }
       public void setName(String Name) {
           this.Name = Name;
       }
       public String getValue() {
           return Value;
       }
       public void setValue(String Value) {
           this.Value = Value;
       }
  }

Current JSON Output

  [{"Name":"Person1", "Value":"Value1"}, {"Name":"Person2", "Value":"Value2"}]

Custom JSON Output (Required)

  [{"1":"Person1", "2":"Value1"}, {"1":"Person2", "2":"Value2"}]
1

There are 1 answers

0
Ra_ On BEST ANSWER

Jorge is right - Ninja just uses Jackson to serialize stuff - you can use all goodies of Jackson to customize parsing and rendering of your entities.

In your case the solution is simple. Just use @JsonProperty like that:

@Entity
public class MyPOJO {
   private String Name;
   private String Value;

   @JsonProperty("1")
   public String getName() {
       return Name;
   }
   public void setName(String Name) {
       this.Name = Name;
   }

   @JsonProperty("2")
   public String getValue() {
       return Value;
   }
   public void setValue(String Value) {
       this.Value = Value;
   }

}