user1180463
user1180463

Reputation: 245

Make JSONObject Serializable

JSONObject class of package org.codehaus.groovy.grails.web.json does not implement Serializable.

I want to make this object serializable as I am using session replication among application servers and JSONObject gets saved in session.

I have two options to achieve this:

  1. Subclass JSONObjects that implements Serializable.
  2. use toString() method on JSONObject object while saving this object in session.

Can anyone please suggest which one of two options should I use and what is the reason?

Upvotes: 4

Views: 9022

Answers (2)

Jake Sellers
Jake Sellers

Reputation: 2439

You don't need to serialize JSON, it is already in a store-able form(sort of the whole point of JSON). Grails has JSON parsers and "slurpers" all ready to use for you. So just store the JSON data as a string and use a parser on it to read it back out of the file.

Upvotes: 1

Bill the Lizard
Bill the Lizard

Reputation: 406095

I think the toString method is what's intended to be used here. That method already returns the JSON text of the object, which is suitable for transmitting or storing.

Upvotes: 6

Related Questions