0

Hello everyone i have a simple problem where i cant seem to convert a zookeeper object into json and vice versa using GSON library in jersey for java web services.The error i get is

Exception in thread "main" java.lang.StackOverflowError
    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381)
    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376)
    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:381)
    at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376)

and it keeps going on pretty large. From what i have searched this semms to be the issue of too deeply nested objects and recursion. Here's what i tried for a simple POC

ZooKeeper zoo;
        try {
            zoo = new ZooKeeper("localhost:2182",5000,null);
            String obj=gson.toJson(zoo, ZooKeeper.class);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

can someone clearly explain what is the actual problem and even if its possible to convert and use zookeeper objects as json(because of all the threads associated with it)

JayD
  • 748
  • 1
  • 13
  • 38

1 Answers1

1

ZooKeeper is a server and not a DTO

maybe you want to do a json with DTO configuration

my proposition

public static void main(String[] args) {
  ZooKeeper zoo;
  try {
    ZooKeeperConfDTO conf = new ZooKeeperConfDTO("localhost:2182", 5000, null);
    zoo = runZoo(conf);
    String json = new Gson().toJson(conf);
    System.out.println(json); //---->{"connectString":"localhost:2182","sessionTimeout":5000}
  } catch (Exception e) {
    e.printStackTrace();
  }
}

private static ZooKeeper runZoo(ZooKeeperConfDTO conf) throws IOException {
  return new ZooKeeper(conf.connectString, conf.sessionTimeout, conf.watcher);
}

and created class

import org.apache.zookeeper.Watcher;

public class ZooKeeperConfDTO {
  public String connectString;
  public int sessionTimeout;
  public Watcher watcher;

  public ZooKeeperConfDTO(String connectString, int sessionTimeout, Watcher watcher) {
    this.connectString = connectString;
    this.sessionTimeout = sessionTimeout;
    this.watcher = watcher;
  }
}

version 2:

create your TypeAdapter for ClientCnxn

import java.io.IOException;
import org.apache.zookeeper.ClientCnxn;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

public class ClientCnxnAdapter extends TypeAdapter<ClientCnxn> {
    @Override
    public void write(JsonWriter writer, ClientCnxn cnxn) throws IOException {
        writer.beginObject();
        writer.name("sessionId");
        writer.value(cnxn.getSessionId());
        writer.name("timeOut");
        writer.value(cnxn.getSessionTimeout());
        writer.endObject();
    }

    @Override
    public ClientCnxn read(JsonReader in) throws IOException {
        return null;
    }
}

and use it

public static void main(String[] args) {
    ZooKeeper zoo;
    try {
        zoo = new ZooKeeper("localhost:2182", 5000, null);
        Gson gson = new GsonBuilder().registerTypeAdapter(ClientCnxn.class, new ClientCnxnAdapter()).create() ;
        String json = gson.toJson(zoo);
        System.out.println(json); //---->{"cnxn":{"sessionId":0,"timeOut":0},"watchManager":{"dataWatches":{},"existWatches":{},"childWatches":{}}}
    } catch (Exception e) {
        e.printStackTrace();
    }
}
DEV-Jacol
  • 567
  • 3
  • 10
  • what i meant was that the zookeeper object has all the references for the state of the client such as Connected,disconnected expired and etc but with your approach i wont have access to all those settings will i? Anyways i have dropped that approach now but thanks for answering – JayD Mar 04 '19 at 06:02
  • @JayD look version 2 maybe it will help you – DEV-Jacol Mar 04 '19 at 07:51
  • thanks dude surely going to check this out and let you know – JayD Mar 04 '19 at 10:02