JSON Jackon deserialze filepath (String) in json to File object in java -
i using object mapper jackson map json file nested set of java beans. nested beans , string, integer , enum objects correctly set in based on defined in json.
some of string represent filepath , nice have jackon object mapper directly map filepath string java file object.
is possibly?
i think, works default. please, see example:
import java.io.file; import java.io.ioexception; import com.fasterxml.jackson.core.jsonfactory; import com.fasterxml.jackson.core.jsonparser; import com.fasterxml.jackson.databind.objectmapper; public class jacksonprogram { public static void main(string[] args) throws ioexception { objectmapper objectmapper = new objectmapper(); jsonfactory jsonfactory = new jsonfactory(); jsonparser parser = jsonfactory.createjsonparser("{\"id\":\"1s200d\", \"path\":\"/tmp/test/file.txt\"}"); entity employee = objectmapper.readvalue(parser, entity.class); system.out.println(employee); } } class entity { private string id; private file path; public string getid() { return id; } public void setid(string id) { this.id = id; } public file getpath() { return path; } public void setpath(file path) { this.path = path; } @override public string tostring() { return "entity [id=" + id + ", path=" + path + "]"; } }
as can see, declared property file
.
Comments
Post a Comment