Deserialize Json Datatype File

Issue

I’m trying to deserialize a Json string to a custom class.
My json is:

externDossierNr:"foo10"
internDossierNr:"2016010"
rapport:File
testarray:Array[3]
testword:42
__proto__:Object 

externDossierNr, internDossierNr and rapport are data I get from my form.
I can deserialize externDossierNr and internDossierNr.
When I try to deserialize rapport it gives {}.
I’ve then added testarray and testword in javascript and I can read those as well.

I think the problem lies in the JsonType File.
Here I find different Json data types but File is not one of them.

I’m sending my file trough RestAngular

public save(item: any): any  {


    item["testword"] = 42;
    item["testarray"] = [2,5,9];

    console.log("item to save is ", item);
    console.log("rapport is ", item["rapport"]);

    if (item.id === undefined) {
        this.restService.save(this.metadata.apiDomain, item).then((addedItem: any) => {
            toastr.success(`${addedItem.naam} successfully created.`, `Overzicht Dossiers Created`);
        });
    } else {
        this.restService.update(this.metadata.apiDomain, item).then((updatedItem: any) => {
            toastr.success(`${updatedItem.naam} successfully updated.`, `Overzicht Dossiers Updated`);
        });
    }
}

For my deserialization I used this and ([this][2] from sghill not enough rep) article

Producing the following code
package be.ugent.lca.data.entities.deserializers;

import be.ugent.lca.data.entities.Dossier;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import java.io.IOException;

public class DossierDeserializer extends JsonDeserializer {
    @Override
    public Dossier deserialize(JsonParser jsonParser,
            DeserializationContext deserializationContext) throws IOException {
        ObjectCodec oc = jsonParser.getCodec();
        JsonNode root = oc.readTree(jsonParser);
        Dossier dossier = new Dossier();

        dossier.setExternDossierNr(root.get("externDossierNr").asText());
        dossier.setInternDossierNr(root.get("internDossierNr").asText());

        System.out.println("Intern Dossier nr is " + root.get("internDossierNr"));
        System.out.println("Test word " +  root.get("testword"));
        System.out.println("Test array " + root.get("testarray"));
        JsonNode node = root.get("rapport");
        if(node == null){
            System.out.println("Rapport is a null pointer.");
        }else{
            System.out.println("Rapport is " + node + " with size " + node.size());
        }
        return dossier;
    }
}

Is there somewhere I can find more info on the File Json data type.
How can I send it so I can read it and deserialize it?

Solution

My assumption that was wrong that I was already printing the Json.
I was just posting the javascript object.

As for not being able to send it is because altough I have multipart/formdata in my html form upon sending it trough restangular it becomes application/json.

On this restangular issue I’ve found how to package it in Formdata.

Answered By – turoni

Answer Checked By – Marilyn (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.