-1

I want to save a very complex java object to redis as a value. Therefore I have tried to convert this object to Json (gson lib) but it isn't working for my purpose (see other question: GSON java.lang.IllegalArgumentException: class 'xx' declares multiple JSON fields named 'XX' AND StackOverflowError).

I want to store/serialize this mentioned complex java object of an application to redis as a value and load back later to the complex java object (consindering the following comment: Can Jedis get/set an Java POJO?). It is just for evaluation purpose.

How can I accomplish to store this complex object (preferable with json conversion) to redis and back? Every comment would be helpful


Update1: The converstion to json and back is working, but with futher process in the application the system is raising a java.util.LinkedHashMap incompatible with XXX.

  SuperclassExclusionStrategy ex = new SuperclassExclusionStrategy();
  Gson gson = new GsonBuilder().addDeserializationExclusionStrategy(ex).addSerializationExclusionStrategy(ex).create();

  String json = gson.toJson(daoResult);

  daoResultJson = gson.fromJson(json, JavaObject_DAOListResult.class);

There are some objects in the subclasses, which hasn't been converted back in the original state.


Solved

I have solved my problem in using another lib called json-io

Now I can work perfectly fine with with my object. In Gson there were a lot of null properties whis were missing in the json string. With json-io the null properties were converted as well. Here is the solution:

 // convert daoResult to JSON
 jsonIO = JsonWriter.objectToJson(daoResult);
 // convert stored JSON to daoResult
 daoResultJson =  (DAOListResult) JsonReader.jsonToJava(jsonIO);
Community
  • 1
  • 1
imalik8088
  • 1,501
  • 5
  • 21
  • 39

1 Answers1

0

Assuming you fix your other issue in converting your object to JSON, which is separate from this question, I guess, you can just set your JSON string to a Redis key, and then get it back later:

127.0.0.1:6379> SET blah '{"some": "x", "some_other": "y"}'
OK
127.0.0.1:6379> GET blah
"{\"some\": \"x\", \"some_other\": \"y\"}"

You'll need to build a serializer/deserializer between your Java object and JSON, but this is all you need to do with Redis. You could alternatively use a Redis Hash with HSET and HGET to accomplish the same, but you don't provide enough information in your question for me to be able to recommend which is better for your situation.

Eli
  • 36,793
  • 40
  • 144
  • 207
  • I was able to convert the complex java object to json and the otherway around but the further processing of the complex object has raised a error `java.util.LinkedHashMap incompatible with XXX` – imalik8088 Jul 20 '14 at 19:05
  • The current problem is to convert the Json String back to the original object, with the giving error `java.util.LinkedHashMap incompatible with XXX` – imalik8088 Jul 20 '14 at 19:15