データ交換フォーマット(data-inntrcenge format)JSON


フロントエンドJSON:
JavaScriptの中のJSON
http://www.dreamdu.com/blog/2008/10/19/json_同前javascript/
javascriptでJSONを処理します。
http://jelly.iteye.com/blog/138707
バックグラウンドJava JSON:
java Jsonのフレームワークは、現在の比較的良い(性能などの考慮)二つがjacksonとgsonです。比較:
http://stackoverflow.com/questions/2378402/jackson-vs-gsonはjson-tools、jackson、gsonなどの中で、jacksonの性能はきっと最も良くて、重点的にそのAPIを捉えてすみます。
JACKSON:
1 jackson Serialization:
http://stackoverflow.com/questions/5430524/serialization-of-key-value-pairs-in-jackson
2 Using jackson deserialize a json String to java neted oject:
http://stackoverflow.com/questions/8921089/jackson-converting-json-property-to-nested-object-with-dot-notation
3 json Stringの中のkeyは、対応するjavantityの中で@JsonProperty注解を使って対応します。
4つのnon-static inner classに対して、jacksonはserializableを序次化することができますが、deserializableは逆順序でjson文字列を作ってこのnon-static inner classにあげてはいけません。詳細は以下の通りです。
http://jira.codehaus.org/browse/JACKSON-594
http://www.cowtowncoder.com/blog/archives/2010/08/entry_411.
http://stackoverflow.com/questions/7144912/why-is-a-serializable-inner-class-not-serializable
5@Json Creator。。。。。。
古いバージョンのJacksonはデフォルトの無参構造方法しか使えません。
新しいサポートは@Json Creatorを通じて有参与構造方法で逆序列化されます。(無参構造法が定義されていない時に特に有用です。)そして、enumに対して逆序列化されます。
。。。。。。
http://wiki.fasterxml.com/JacksonFeatureCreators
http://stackoverflow.com/questions/11838039/jackson-3rd-party-class-with-no-default-constructor
http://stackoverflow.com/questions/9300191/how-to-annotate-enum-fields-for-deserialization-using-jackson-json?rq=1
http://stackoverflow.com/questions/8790389/jackson-deserialize-one-base-enums
参照
例:反秩序化が必要な類CacheKeyの構造は以下の通りである。

@JsonIgnoreProperties(ignoreUnknown=true)
public class CacheKey implements Serializable {

    private String key;
    private CacheType cacheType;

    public CacheKey(String key, CacheType cacheType) {
        Validate.notEmpty(key, "key of CacheKey must be not empty");
        Validate.notNull(cacheType, "cacheType of CacheKey must be not null");
        this.key = key;
        this.cacheType = cacheType;
    }
}

public enum CacheType {

    CACHE_TYPE_A("cache-type-a", 60*60*4), // caching 4 hours
    CACHE_TYPE_A("cache-type-b", 60*60*24); // caching 1 day

    private final String prefix; // prefix of cacheType's key
    private int exp; // default expire time in seconds

    private CacheType(String prefix, int exp) {
        this.prefix = prefix;
        this.exp = exp;
    }
}
方式の一:Factory-based Creator(種類CacheKeyに以下の静的工場方法を追加し、@Json Creatorと注釈する):

public class CacheKey implements Serializable {
    @JsonCreator
    public static CacheKey fromValue(@JsonProperty("key") String key, @JsonProperty("type") String type) {

        CacheType cacheType = null;
        for (CacheType c: CacheType.values()) {
            if (c.getPrefix().equals(type)) {
                cacheType = c;
            }
        }

        if (null == cacheType) {
            throw new IllegalArgumentException("Invalid type of cache: " + type);
        }

        return new CacheKey(key, cacheType);
    }
}
方式二:コンストラクタ-based Creator:

public class CacheKey implements Serializable {

    @JsonCreator
    public CacheKey(@JsonProperty("key") String key, @JsonProperty("type") CacheType cacheType) {
        Validate.notEmpty(key, "key of CacheKey must be not empty");
        Validate.notNull(cacheType, "cacheType of CacheKey must be not null");
        this.key = key;
        this.cacheType = cacheType;
    }
}

public enum CacheType {
    @JsonCreator
    public static CacheType fromPrefix(String prefix) {
        for (CacheType c: CacheType.values()) {
            if (c.getPrefix().equals(prefix)) {
                return c;
            }
        }
        throw new IllegalArgumentException("Invalid CacheType: [" + prefix +
                "]. Acceptable CacheType can be " +
                Arrays.asList(CacheType.values()));
    }
}
6つのObject Mapperの使用例:
ObjectMapper  mapper=  new ObjectMapper();
		GetJSONData getJons = new GetJSONData();
		String json = getJons.getJsonString(urlPath);
		String jsonString = json.substring(json.indexOf("\"data\":") + 7,
				json.length() - 1);
		List<RequestsCount> someClassList =
			    mapper.readValue(jsonString, TypeFactory.defaultInstance().constructCollectionType(List.class, RequestsCount.class));