0

Hi I'm trying to convert JSON data to class, I use the method of deserializing like that:

public class GroupDeserlialize extends StdDeserializer<Employe> {
public GroupDeserlialize()
{
  this(null);
}
public GroupDeserlialize(Class<?> vc)
{
  super(vc);
}
@Override
public Employe deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
  // TODO Auto-generated method stub

      JsonNode node = p.getCodec().readTree(p);
      int matricule = (Integer) node.get("matricule").intValue();
      String nom= (String) node.get("nom").textValue();
      String prenom= (String) node.get("prenom").textValue();
      String nom_ar= (String) node.get("nom_ar").textValue();
      String prenom_ar= (String) node.get("prenom_ar").textValue();
      int age = (Integer) node.get("age").numberValue();
      String description= (String) node.get("description").textValue();
      String email= (String) node.get("email").textValue();
      String adresse= (String) node.get("adresse").textValue();
      String ville= (String) node.get("ville").textValue();
      int codeP = (Integer) node.get("codeP").intValue();
      int numTele = (Integer) node.get("numTele").intValue();
     // Long idGrp = (Long) ((LongNode) node.get("idGrp")).numberValue();
   
    
      Long idGrp = (Long) node.get(0).numberValue();
       String nomGroup =  (String)  node.get("nomGroup").asText();   
return new Employe( matricule,  nom,  prenom,  nom_ar,  prenom_ar,  age, description,  email,  codeP,  numTele,  adresse,  ville, new Group(idGrp,nomGroup));
}}

The JSON what I want to send (this is what angular return to spring boot IDK if the JSON is malformed with /n):

{
  "matricule" : "125",
  "nom" : "Assaibi",
  "prenom" : "Takwa",
  "nom_ar" : "Assaibi",
  "prenom_ar" : "Takwa",
  "age" : 34,
  "description" : "kjjhhdh",
  "codeP" : "2087",
  "email" : "[email protected]",
  "numTele" : "51890562",
  "adresse" : "4270 citè folla agba Tunis",
  "ville" : "Tunis",
  "group" : "{\n  \"nomGroup\": \"groupeMembre\",\n  \"idGrp\": 3\n}"
}

This is the class employe which contains a group

@Entity
@JsonDeserialize(using = GroupDeserlialize.class)
public class Employe implements Serializable {
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO, generator = "myid")
   @GenericGenerator(name = "myid", strategy = "entity.MyGenerator")
   private Integer matricule;

   private String   nom;
   private String prenom;
   private String nom_ar;
   private String prenom_ar;
   private int age;
   private String description;
   private String email;
   private int codeP;
   private int numTele;
   private String adresse;
   private String  ville;
   
   @JoinColumn(name="idGrp")
   @ManyToOne(cascade = {CascadeType.ALL})  
   @Embedded
   Group group;
   
    public Employe(Integer matricule, String nom, String prenom, String nom_ar, String prenom_ar, int age,
        String description, String email, int codeP, int numTele, String adresse, String ville, Group group) {
      super();
      this.matricule = matricule;
      this.nom = nom;
      this.prenom = prenom;
      this.nom_ar = nom_ar;
      this.prenom_ar = prenom_ar;
      this.age = age;
      this.description = description;
      this.email = email;
      this.codeP = codeP;
      this.numTele = numTele;
      this.adresse = adresse;
      this.ville = ville;
    }
}

And this is the Group class

@Entity
@Table(name="groups")
public class Group implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long idGrp;
    
    private String NomGroup;
    
    public Group(){
    }

    public Group(Long id, String nomGroup) {
        super();
        this.idGrp = id;
        NomGroup = nomGroup;
    }
}

And the main class

public static void main(String[] args) throws  JsonProcessingException {
    SpringApplication.run(CniAppApplication.class, args);
    Employe s=new Employe();
    ObjectMapper mapper = new ObjectMapper();
    /*SimpleModule module = new SimpleModule();
    module.addDeserializer(Employe.class, new GroupDeserlialize());
    mapper.registerModule(module);

    try {
      Employe readValue = mapper.readValue(mapper.writeValueAsString(s), Employe.class);
    } catch (JsonMappingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (JsonProcessingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }*/
    Employe itemWithOwner = new ObjectMapper().readValue(mapper.writeValueAsString(s), Employe.class);
  }
}

The exception I get:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.fasterxml.jackson.databind.JsonNode.numberValue()" because the return value of "com.fasterxml.jackson.databind.JsonNode.get(int)" is null
    at entity.GroupDeserlialize.deserialize(GroupDeserlialize.java:48)
    at entity.GroupDeserlialize.deserialize(GroupDeserlialize.java:1)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4482)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3434)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3402)
    at cni.tn.CniApp.CniAppApplication.main(CniAppApplication.java:57)

1 Answer 1

0

I believe that the issue is caused by the following line:

Long idGrp = (Long) node.get(0).numberValue();

group in your JSON is another String. It happens to be JSON, but as far as your Deserializer knows it is a regular String. This means you need to use ObjectMapper to read it as a JSON. You can do this as follows:

public class GroupDeserlialize extends StdDeserializer<Employe> {

    private ObjectMapper objectMapper;

    public GroupDeserlialize(ObjectMapper objectMapper) {
        this(objectMapper, null);
    }

    public GroupDeserlialize(ObjectMapper objectMapper, Class<?> vc) {
        super(vc);
        this.objectMapper = objectMapper;
    }

    @Override
    public Employe deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
        JsonNode node = p.getCodec().readTree(p);
        int matricule = node.get("matricule").intValue();
        String nom= node.get("nom").textValue();
        String prenom= node.get("prenom").textValue();
        String nom_ar= node.get("nom_ar").textValue();
        String prenom_ar= node.get("prenom_ar").textValue();
        int age = (Integer) node.get("age").numberValue();
        String description= node.get("description").textValue();
        String email= node.get("email").textValue();
        String adresse=  node.get("adresse").textValue();
        String ville= node.get("ville").textValue();
        int codeP = node.get("codeP").intValue();
        int numTele = node.get("numTele").intValue();

        String groupJson = node.get("group").textValue();
        Group group = objectMapper.readValue(groupJson, Group.class);

        return new Employe(matricule, nom, prenom, nom_ar, prenom_ar, age, description, email, codeP, numTele, adresse, ville, group);
    }
}

This also means you need to change your main class:

public static void main(String[] args) throws  JsonProcessingException {
    SpringApplication.run(CniAppApplication.class, args);
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule();
    module.addDeserializer(Employe.class, new GroupDeserlialize(mapper));
    mapper.registerModule(module);

    try {
      Employe readValue = mapper.readValue(new File("input.json"), Employe.class);
    } catch (JsonMappingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (JsonProcessingException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
}

This assumes you have your JSON file in an input.json file. This is obviously for testing purposes only.

You also need to add setters to Group class:

public class Group {

    private Long idGrp;

    private String nomGroup;

    public Group(){
    }

    public Group(Long id, String nomGroup) {
        super();
        this.idGrp = id;
        this.nomGroup = nomGroup;
    }

    public void setIdGrp(Long idGrp) {
        this.idGrp = idGrp;
    }

    public void setNomGroup(String nomGroup) {
        this.nomGroup = nomGroup;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.