Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 this

<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.

like image 256
XXX Avatar asked Nov 24 '10 00:11

XXX


People also ask

Is JSON serializable in Java?

JSON-Java is a Java serialization/deserialization library. It parses JSON documents into Java objects and generates new JSON documents from the Java classes.

Can you serialize JSON?

NET objects as JSON (serialize) To write JSON to a string or to a file, call the JsonSerializer. Serialize method. The JSON output is minified (whitespace, indentation, and new-line characters are removed) by default.

How do you serialize an object in Java?

To make a Java object serializable we implement the java. io. Serializable interface. The ObjectOutputStream class contains writeObject() method for serializing an Object.

What is a JSON serialized object?

Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON). Serialization is the process of converting the state of an object, that is, the values of its properties, into a form that can be stored or transmitted.


1 Answers

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

like image 165
Fakrudeen Avatar answered Sep 24 '22 21:09

Fakrudeen