简体   繁体   中英

Unable to read data from CBOR File

My Aim is to store the instances of TargetModel class into CBOR Format using files. If there is any other approach, it works for me!

I am using Jackson CBOR API to write to and read the data from and write the data to a file.

I am using writeValue method of ObjectMapper Class to write an instance of the TargetModel but when I try to read the data it throws a JsonMappingException when I try to read the object using ReadValue .

Please help me! I am in serious trouble because of this.

Mainclass.java

package cborStoring;

public class MainClass {

public static void main (String[] args) throws Exception{

    String targetfilePath = "/home/vaio/Documents/mySampleFile";

    CBORWriter myCborWriter = new CBORWriter();

    TargetModel sampleModel = new TargetModel("rajat", "dfdsf");
//  TestClass sampleModel = new TestClass();

    Object inputObject = (Object)sampleModel;


    myCborWriter.writeObject(inputObject, targetfilePath);

    CBORReader myCborReader = new CBORReader();
    Object readObj = myCborReader.readObject(targetfilePath);

    TargetModel myModel = (TargetModel)readObj;
//  TestClass myModel = (TestClass)readObj;

    System.out.println("Program Ends!");

}
}

CBORWriter.java

package cborStoring;

public class CBORWriter {

public CBORWriter(){

}

public void writeObject(Object inputObject, String targetfilePath) throws Exception{

    FileOutputStream fos = new FileOutputStream(targetfilePath, true);

    if (!(new File(targetfilePath)).exists())
        (new File(targetfilePath)).createNewFile();

    CBORFactory f = new CBORFactory();
    ObjectMapper mapper = new ObjectMapper(f);

    mapper.writeValue(fos,inputObject);


    fos.close();
}

}

CBORReader.java

package cborStoring;

public class CBORReader {

public CBORReader(){

}

public Object readObject(String targetFilePath) throws Exception{

    FileInputStream fis = new FileInputStream(targetFilePath);

    CBORFactory f = new CBORFactory();

    ObjectMapper myMapper = new ObjectMapper(f);

    CBORParser myCborParser = f.createParser(fis);

    //Object readObject = myMapper.readValue(myCborParser, Object.class);
    //Object readObject = myMapper.readValue(myCborParser, TargetModel.class); : This does NOT Work throws JSONMappingException

    Object readObject = myMapper.readValue(myCborParser, TargetModel.class);

    return readObject;

}

}

I don't know what the underlying problem is, but code can be simplified quite a bit, like so:

ObjectMapper mapper = new ObjectMapper(new CBORFactory()); mapper.writeValue(file, objectToWrite); TargetModel result = mapper.readValue(file, TargetModel.class);

and there is usually not much point in separately constructing parsers or generators. Also, make sure to use the latest available version; 2.5.3 at this point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM