REST Jersey + jackson servlet parsea part of the json as a JsonObject

3

I am receiving a JSON that jersey + jackson parse it directly on the object that will contain the information. The thing is, I want part of that JSON to be saved as a JSONObject or JSONArray .

This would be the JSON that is received,

 {  
  "fecha":"2018-10-03",
  "fechaCoste":"2018-10-30",
  "idTipoParte":1,
  "idEstado":1,
  "actividades":[  
     {  
        "idParteActividad":282,
        "idTipoActividad":3,
           "idPadre":5,
           "type":"farm",
           "key":"11265,14",
           "idModulo":9,
           "modulo":"ACTIVITIES",
        "data":[  
           {  
              "idParteActividadData":131,
              "idField":3,
              "value":123456
           },
           {  
              "idParteActividadData":132,
              "idField":4,
              "value":2.0
           }
        ],
        "descripcion":"test",
        "recursos":[  
           {  
              "idPerfilTrab":17,
              "cantidadObj":0.0,
              "idParteRecurso":264,
              "idTipoRecurso":1,
              "idRecurso":25,
              "cantidad":1.0,
              "idTipoUd":1,
              "importeUd":0.0,
              "costeUd":0.0
           },
           {  
              "idParteRecurso":140,
              "idTipoRecurso":2,
              "idRecurso":2,
              "cantidad":1.0,
              "idTipoUd":1,
              "importeUd":0.0,
              "costeUd":0.0
           },
           {  
              "tipoMvto":"I",
              "idAlmacen":3,
              "idPersona":25,
              "idTipoStock":1,
              "lote":"1",
              "albaran":"1",
              "idParteRecurso":215,
              "idTipoRecurso":3,
              "idRecurso":20,
              "cantidad":1.0,
              "idTipoUd":1,
              "importeUd":1.0,
              "costeUd":0.0
           }
        ],
        "cost":0.0
     }
  ]

}

This would be the object in which the JSON would be saved.

public class Workpart extends Identifiable {

    private Integer idParte;
    private String fecha;
    private String fechaCoste;
    private String idTipoParte;
    private String idEstado;

    private JsonArray actividades;

    public Workpart() {
        super();
    }

    @Override
    public Integer id() {
        return getIdParte();
    }

    public Integer getIdParte() {
        return idParte;
    }

    public void setIdParte(final Integer idParte) {
        this.idParte = idParte;
    }

    public String getFecha() {
        return fecha;
    }

    public void setFecha(final String fecha) {
        this.fecha = fecha;
    }

    public String getFechaCoste() {
        return fechaCoste;
    }

    public void setFechaCoste(final String fechaCoste) {
        this.fechaCoste = fechaCoste;
    }

    public String getIdTipoParte() {
        return idTipoParte;
    }

    public void setIdTipoParte(final String idTipoParte) {
        this.idTipoParte = idTipoParte;
    }

    public String getIdEstado() {
        return idEstado;
    }

    public void setIdEstado(final String idEstado) {
        this.idEstado = idEstado;
    }

    public JsonArray getActividades() {
        return actividades;
    }

    public void setActividades(final JsonArray actividades) {
        this.actividades = actividades;
    }
}

From the JSON the first fields are saved in their corresponding fields but I want what is part of the "activities" node to be saved as a JSON in the "workpart" object

I know that it would be normal to create objects for activities and resources with their corresponding fields, but it is not feasible since there are a lot of different activities with different fields that makes it easier to save it as JSON.

The service responsible for handling the REST call is as follows.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response insert(@PathParam(USER_ID) final Integer userId, final Workpart record) {
    return insertInner(userId, record);
}

With what there is currently a pete occurs in which he says he can not parse activities.

    
asked by Cadeq 16.10.2018 в 11:49
source

1 answer

2

I can not see the advantage of working with JSONArray , but what you need is to create your own de-serializer to do what you want. If you're working with Jackson, you have to create a class that extends JsonDeserializer :

public class JsonArrayDeserializer extends JsonDeserializer<JsonArray> {

    //constructores...


    public JsonArray deserialize(JsonParser p, DeserializationContext ctxt) {
         final TreeNode treeNode = parser.readValueAsTree();
         return new com.google.gson.JsonParser()
              .parse(treeNode.toString()).getAsJsonArray();
    }
}

You will need to look at the API of JsonParser It is not complicated to understand but it has many methods to traverse a JSON.

And then you need to write down the field:

@JsonDeserialize(using = JsonArrayDeserializer.class)
private JsonArray actividades;
    
answered by 16.10.2018 / 12:17
source