3

I have a class implementing org.apache.geode.pdx.PdxSerializable and need to store the objects of it in a file in java. For storing in files, the object needs to be Serializable but the class PDXSerializable is being used for storing data in gemfire and then hence we cannot use Serializable class.

rupweb
  • 3,052
  • 1
  • 30
  • 57
KCK
  • 2,015
  • 2
  • 17
  • 35

2 Answers2

4

Why don't you use custom object serialization?. Here's an example that I have quickly created;


 private class Foo implements PdxSerializable {
        private String bar;
        private Integer baz;

        public Foo(final String bar, final Integer baz) {

            this.bar = bar;
            this.baz = baz;
        }

        public String getBar() {
            return bar;
        }

        public Integer getBaz() {
            return baz;
        }
        public void toData(PdxWriter out) {
         //your pdx stuff
        }

        public void fromData(PdxReader in) {
          //your pdx work
        }
    }

//and a custom reader and writer 
     private void writeCustom(final Foo foo, final Path path) throws IOException {
        try(ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(path.toFile()))) {
            objectOutputStream.writeChars(foo.getBar());
            objectOutputStream.writeInt(foo.getBaz());
        }
    }

    private Foo readCustom(final Path path) throws IOException {
        try(ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(path.toFile()))) {
            String bar = objectInputStream.readUTF();
            Integer baz = objectInputStream.readInt();
            return new Foo(bar, baz);
        }
    }

Custom Serialization Oracle docs - https://www.oracle.com/technetwork/articles/java/javaserial-1536170.html

A similar question with great answers - Java Custom Serialization

Laksitha Ranasingha
  • 4,321
  • 1
  • 28
  • 33
1

You may want to have a look at JSONFormatter:

http://gemfire-95-javadocs.docs.pivotal.io/org/apache/geode/pdx/JSONFormatter.html

It has a static method that turns a PDXInstance into a JSON String.

Assuming the data is in GemFire then getting it out as a PDXInstance just requires you to write a simple client with "pdx-read-serialized" set to true. With that setting, when your client calls get or getAll the objects returned will be PDXInstances and you can use JSONFormatter to turn them into JSON directly.

Randy May
  • 348
  • 1
  • 8