开发者

How to serialize JSON object in java

Hi I am using Voldemort to store my data. My key is a word and values are number of occurrence of the word and the URL. For example:

key :question
value: 10, www.stackoverflow.com

I am using Json object to put my values. My code looks like this

import org.json.JSONObject;
import com.metaparadigm.jsonrpc.JSONSerializer;
import voldemort.client.ClientConfig;
import voldemort.client.SocketStoreClientFactory;
import voldemort.client.StoreClient;
import voldemort.client.StoreClientFactory;

public class ClientExample { 
  public static void main (String [] args) { 
    String bootstrapUrl = "tcp://localhost:6666";

    ClientConfig cc = new ClientConfig (); 
    cc.setBootstrapUrls (bootstrapUrl); 
    String[] valuePair = new String[2];
    int val = 1;
    StoreClientFactory factory = new SocketStoreClientFactory (cc); 
    StoreClient client = factory.getStoreClient("test"); 
    JSONObject json = new JSONObject(); 
    json.put("occurence",val); 
    json.put("url", "www.cnn.com"); 
    client.put("foo", json); 
  } 
} 

And my store.xml looks like th开发者_JS百科is

<stores> 
  <store> 
    <name>test</name> 
    <persistence>bdb</persistence> 
    <routing>client</routing> 
    <replication-factor>1</replication-factor> 
    <required-reads>1</required-reads> 
    <required-writes>1</required-writes> 
    <key-serializer> 
      <type>string</type> 
    </key-serializer> 
    <value-serializer> 
      <type>java-serialization</type> 
     <schema-info>"Compount Types"</schema-info> 
    </value-serializer> 
  </store> 
</stores> 

While i was trying to run the code i am getting following exception: **

Exception in thread "main" voldemort.serialization.SerializationException: java.io.NotSerializableException: org.json.JSONObject at voldemort.serialization.ObjectSerializer.toBytes(ObjectSerializer.java:47) at voldemort.store.serialized.SerializingStore.put(SerializingStore.java:109) at voldemort.store.DelegatingStore.put(DelegatingStore.java:68) at voldemort.client.DefaultStoreClient.put(DefaultStoreClient.java:208) at voldemort.client.DefaultStoreClient.put(DefaultStoreClient.java:193) at ClientExample.main(ClientExample.java:27) Caused by: java.io.NotSerializableException: org.json.JSONObject at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.writeObject(Unknown Source) at voldemort.serialization.ObjectSerializer.toBytes(ObjectSerializer.java:44)

**

Could you please tell me how to serialize JSON object.Thanks in advance.


Have a look at Gson library. It does nice JSON to object mapping and serialization.


Late answer, but I was searching for this myself and I have found a solution. Just to clarify, my problem was that my class contained a org.json.JSONobject, and it couldn't be serialized, which is a necessary step to pass objects in Android Bundle objects.

  1. Add the keyword transient to the field.
  2. Provide the functions writeObject and readObject for the serializer to call. This looks like an override function, but actually isn't decorated with @Override.

Like this:

private void writeObject(ObjectOutputStream oos) throws IOException{
    oos.defaultWriteObject();
    oos.writeChars(_jsonObject.toString());
}

private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException {
    ois.defaultReadObject();
    loadJSON(ois.readUTF());
}

3. In the loadJSON function, you call the JSONObject constructor that takes a string, and assign it to the transient field.

I found this to be far easier than replacing the library.


Use the .toString() method of JSONObject to get the text.


In the case you'd still want Java built-in serialization without having to resort to marshal your JSON object into string notation, one thing you could do is extend JSONObject and JSONArray from org.json and just implement Serializable.

Then you can use your own versions of JSONObject and JSONArray across the board instead of the originals.

Make sure you define all constructors on your subclasses and call their super() counterparts as well as implement specific methods that return the parent types such as getJSONObject() and getJSONArray() from properties.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜