Introduction
When you're writing your own client and server protocol, you have to exchange data between them. There are many formats available (csv, xml, serialized objects...) and you can always define your own protocol.One format that has been getting more popular recently is JSON (JavaScript Object Notation, see http://en.wikipedia.org/wiki/JSON). It's a text format that allows objects to be serialized in a human-readable form. There's a library for Java called Json-lib (http://json-lib.sourceforge.net/) that performs this serialization for you. This allows you to exchange JSON between your server and client without having to write any JSON code.
The Json-lib library
You can find some examples on how to use Json-lib here: http://json-lib.sourceforge.net/snippets.html. I use it like this:On the server (serialize to JSON):
1
2
3
|
User user = ...; //get the user from database
String json = JSONObject.fromObject(user).toString(); //Send the json to the client |
1
2
|
JSONObject jo = JSONObject.fromObject(response); User user = (User) JSONObject.toBean(jo, User. class );
|
1
|
net.sf.json.JSONException: java.lang.NoSuchMethodException: Unknown property on class
|
The solution
After searching for a way of telling Json-lib to ignore missing properties and not finding it, I decided the best approach would be to create my own PropertySetStrategy. A PropertySetStrategy defines how Json-lib sets the properties on your objects. The default strategy works pretty well, except that it throws an exception when a property is not found. So I decided to create a wrapper around this default PropertySetStrategy that ignores any exceptions happening when setting a property.This is my PropertySetStrategyWrapper:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import net.sf.json.JSONException;
import net.sf.json.util.PropertySetStrategy;
public class PropertyStrategyWrapper extends PropertySetStrategy {
private PropertySetStrategy original;
public PropertyStrategyWrapper(PropertySetStrategy original) {
this .original = original;
}
@Override
public void setProperty(Object o, String string, Object o1) throws JSONException {
try {
original.setProperty(o, string, o1);
} catch (Exception ex) {
//ignore
}
}
} |
Now all we need to do is tell Json-lib to use our wrapper. Our client code now looks like this:
1
2
3
4
5
|
JSONObject jo = JSONObject.fromObject(response); JsonConfig cfg = new JsonConfig();
cfg.setPropertySetStrategy( new PropertyStrategyWrapper(PropertySetStrategy.DEFAULT));
cfg.setRootClass(User. class );
User user = (User) JSONObject.toBean(jo, cfg); |
相关推荐
1、json的核心包:json-lib-2.4-jdk15.jar; 2、json-lib-2.4-jdk15.jar依赖的jar包:commons-beanutils-1.9.3.jar,commons-collections-3.2.2.jar,commons-lang-2.6.jar,commons-logging-1.2.jar,ezmorph-1.0.6...
json基础jar包,包含json-lib-2.3-jdk15.jar源码
json-lib-2.4-jdk15.jar json-lib-2.4-jdk15-sources.jar json-lib-2.4-jdk15-javadoc.jar 包含文件有:json-lib-2.4-jdk15.jar json-lib-2.4-jdk15-sources.jar json-lib-2.4-jdk15-javadoc.jar
json-lib体积小、无依赖,性能表现也不错,适合在各种Java项目中使用。 json-lib的主要功能有: 1. 简单易用:json-lib的API简单易于使用,一般只需要一行或两行代码就可以完成Java对象和JSON的相互转换。 2. 支持对象图:...
json-lib-2.2.3-jdk15-javadoc.jar json 源代码
json架包,包好json-lib-2.2.3-jdk13.jar,json-lib-2.4-jdk14.jar以及附带的支持包ezmorph-1.0.6.jar,commons-logging.jar,commons-lang.jar等
json-lib-2.4-jdk15所需全部JAR包,资源清单 json-lib-2.4-jdk15.jar 依赖: ezmorph-1.0.6.jar commons-beanutils-1.8.3.jar commons-lang-2.5.jar morph-1.1.1.jar commons-logging-1.1.1.jar commons-collections-...
json-lib-2.4-jdk15-sources.jar 是net.sf.json-lib-jdk15-2.4的源码文件
json-lib-1.1-jdk13-javadoc,欢迎下载。
json-lib-2.2.3-jdk13.jar commons-beanutils-1.7.0.jar commons-collections-3.2.1.jar commons-lang-2.4.jar commons-logging-1.1.1.jar ezmorph-1.0.6.jar
json-lib-2.4-jdk15.jar所需全部JAR包需要的伙伴们下载
json-lib-2.4-jdk15依赖的库文件: 压缩包内容: commons-beanutils-1.8.0.jar commons-collections-3.1.jar commons-lang-2.5.jar commons-logging-1.1.1.jar ezmorph-1.0.6.jar json-lib-2.4-jdk15-javadoc.jar ...
json-lib-2.4-jdk13.jar
json-lib-2.3-jdk15开发包和所有依赖包
json-lib-2.4-jdk15和全部依赖jar包 所需要的开发工具
json-lib-2.4-jdk15及其全部依赖jar包 commons-beanutils-1.8.0.jar、commons-collections-3.2.1.jar、commons-lang-2.6.jar、commons-logging-1.1.1.jar、ezmorph-1.0.6.jar、json-lib-2.4-jdk15.jar、xom-1.2.6....
json-lib-2.4-jdk 15及其全部依赖jar包 commons-beanutils-1.8.0.jar、commons-collections-3.2.1.jar、commons-lang-2.6.jar、commons-logging-1.1.1.jar、ezmorph-1.0.6.jar、json-lib-2.4-jdk15.jar
json-lib-2.4-jdk15.jar适用于commons-collections4-4.1.jar
commons-beanutils-1.8.0.jar commons-collections.jar commons-lang-2.4.jar commons-logging-1.1.jar ezmorph-1.0.4.jar json-lib-2.3-jdk15.jar jsonplugin-0.32.jar