Equivalent to python shelf module in Java

Is there any module in Java equivalent to a python shelf module? I need this to achieve a dictionary, such as taxonomic access to data. A dictionary similar to taxonomic data access is a powerful way to save Python objects in a database always available format. I need something for the same purpose, but in Java.

+4
source share
2 answers

I need this too, so I wrote one. A little late, but maybe this will help.

It does not implement the close () method, but simply uses sync (), since it only keeps the file open when writing it.

import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.HashMap; public class Shelf extends HashMap<String, Object> { private static final long serialVersionUID = 7127639025670585367L; private final File file; public static Shelf open(File file) { Shelf shelf = null; try { if (file.exists()) { final FileInputStream fis = new FileInputStream(file); ObjectInputStream ois = new ObjectInputStream(fis); shelf = (Shelf) ois.readObject(); ois.close(); fis.close(); } else { shelf = new Shelf(file); shelf.sync(); } } catch (Exception e) { // TODO: handle errors } return shelf; } // Shelf objects can only be created or opened by the Shelf.open method private Shelf(File file) { this.file = file; sync(); } public void sync() { try { final FileOutputStream fos = new FileOutputStream(file); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(this); oos.close(); fos.close(); } catch (Exception e) { // TODO: handle errors } } // Simple Test Case public static void main(String[] args) { Shelf shelf = Shelf.open(new File("test.obj")); if (shelf.containsKey("test")) { System.out.println(shelf.get("test")); } else { System.out.println("Creating test string. Run the program again."); shelf.put("test", "Hello Shelf!"); shelf.sync(); } } } 
+2
source

You can use a serialization library such as Jackson , which serializes POJO to JSON.

Example from the tutorial :

Jackson org.codehaus.jackson.map.ObjectMapper "works just" to map JSON data to plain old Java objects ("POJOs"). For example, JSON data

 { "name" : { "first" : "Joe", "last" : "Sixpack" }, "gender" : "MALE", "verified" : false, "userImage" : "Rm9vYmFyIQ==" } 

Turning it into a user instance requires two Java lines:

 ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally User user = mapper.readValue(new File("user.json"), User.class); 

If the User class looks something like this (from entry on Tatu's blog):

 public class User { public enum Gender { MALE, FEMALE }; public static class Name { private String _first, _last; public String getFirst() { return _first; } public String getLast() { return _last; } public void setFirst(String s) { _first = s; } public void setLast(String s) { _last = s; } } private Gender _gender; private Name _name; private boolean _isVerified; private byte[] _userImage; public Name getName() { return _name; } public boolean isVerified() { return _isVerified; } public Gender getGender() { return _gender; } public byte[] getUserImage() { return _userImage; } public void setName(Name n) { _name = n; } public void setVerified(boolean b) { _isVerified = b; } public void setGender(Gender g) { _gender = g; } public void setUserImage(byte[] b) { _userImage = b; } } 
0
source

All Articles