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:
嗨,我正在使用Voldemort来存储我的数据。我的关键是一个单词,值是单词和URL的出现次数。例如:
key :question
value: 10, www.*.com
I am using Json object to put my values. My code looks like this
我正在使用Json对象来放置我的值。我的代码看起来像这样
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
我的store.xml看起来像这样
<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)
线程“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)由java.io.ObjectOutputStream.writeObject(Unknown Source)at java.io.ObjectOutputStream.writeObject(Unknown Source)at java.io.ObjectOutputStream.writeObject0(Unknown Source)中的java.io.NotSerializableException:org.json.JSONObject引起的voldemort.serialization.ObjectSerializer.toBytes(ObjectSerializer.java:44)
**
**
Could you please tell me how to serialize JSON object.Thanks in advance.
你能告诉我如何序列化JSON对象。提前谢谢。
4 个解决方案
#1
6
Have a look at Gson library. It does nice JSON to object mapping and serialization.
看看Gson图书馆。它对对象映射和序列化做了很好的JSON。
#2
5
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.
迟到的答案,但我自己正在寻找这个,我找到了解决方案。只是为了澄清,我的问题是我的类包含一个org.json.JSONobject,它无法序列化,这是在Android Bundle对象中传递对象的必要步骤。
- Add the keyword transient to the field.
- 将关键字transient添加到字段中。
- Provide the functions writeObject and readObject for the serializer to call. This looks like an override function, but actually isn't decorated with @Override.
- 为序列化程序提供函数writeObject和readObject以进行调用。这看起来像一个覆盖函数,但实际上并没有用@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.
3.在loadJSON函数中,调用接受字符串的JSONObject构造函数,并将其分配给瞬态字段。
I found this to be far easier than replacing the library.
我发现这比替换库要容易得多。
#3
3
Use the .toString() method of JSONObject to get the text.
使用JSONObject的.toString()方法获取文本。
#4
0
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.
如果您仍然希望Java内置序列化而不必将JSON对象编组为字符串表示法,那么您可以做的一件事是从org.json扩展JSONObject和JSONArray并实现Serializable。
Then you can use your own versions of JSONObject and JSONArray across the board instead of the originals.
然后,您可以全面使用自己的JSONObject和JSONArray版本,而不是原始版本。
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.
确保在子类上定义所有构造函数并调用它们的super()对应项,并实现从属性返回父类型(如getJSONObject()和getJSONArray())的特定方法。
#1
6
Have a look at Gson library. It does nice JSON to object mapping and serialization.
看看Gson图书馆。它对对象映射和序列化做了很好的JSON。
#2
5
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.
迟到的答案,但我自己正在寻找这个,我找到了解决方案。只是为了澄清,我的问题是我的类包含一个org.json.JSONobject,它无法序列化,这是在Android Bundle对象中传递对象的必要步骤。
- Add the keyword transient to the field.
- 将关键字transient添加到字段中。
- Provide the functions writeObject and readObject for the serializer to call. This looks like an override function, but actually isn't decorated with @Override.
- 为序列化程序提供函数writeObject和readObject以进行调用。这看起来像一个覆盖函数,但实际上并没有用@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.
3.在loadJSON函数中,调用接受字符串的JSONObject构造函数,并将其分配给瞬态字段。
I found this to be far easier than replacing the library.
我发现这比替换库要容易得多。
#3
3
Use the .toString() method of JSONObject to get the text.
使用JSONObject的.toString()方法获取文本。
#4
0
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.
如果您仍然希望Java内置序列化而不必将JSON对象编组为字符串表示法,那么您可以做的一件事是从org.json扩展JSONObject和JSONArray并实现Serializable。
Then you can use your own versions of JSONObject and JSONArray across the board instead of the originals.
然后,您可以全面使用自己的JSONObject和JSONArray版本,而不是原始版本。
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.
确保在子类上定义所有构造函数并调用它们的super()对应项,并实现从属性返回父类型(如getJSONObject()和getJSONArray())的特定方法。