eclipspelink、MOXy、JAXB、Jsonツール類

3680 ワード

StringからJSONに移行する対象は最近JSONを使って伝達されています。JAXBはJAVA annotationに基づいていますので、コードが簡潔なので、JAXBをサポートするMOXyを選択しています。以前はgoogle Gsonを使っていました。これはマニュアルでJSONを解析するのに適しています。Gsonでのfrom Json法は、簡単なオブジェクトを解析することもできます。しかし、複雑なオブジェクトは、配列を含め、他のオブジェクトを埋め込むことに成功しませんでした。
初めてMOXyを使って、バージョン2.5.0、JSONの解析エラー:
MOXy deserialzation exception:A descriptor with default root element was not found in the project
 
ネットで調べてみましたが、statckoverflowのです。
http://stackoverflow.com/questions/14246033/moxy-deserialization-exception-a-descriptor-with-default-root-element-was-not-f
上記のようにroot属性は指定されていません。
public staticT from Json(StreamSource stream Source,Class<>cls,boot has Root)
throws JAXBException、Class CastException{
Mapproperties=new HashMap
properties.put(JAXBCN text Properties.MEDIA_TYPE,「appication/json」);
properties.put(JAXBCN text Properties.JSON_INCLUDE_ROOT,has Root)//-->これはfalseに設定する必要があります。
.createConteet(new Class[]{cls}properties);
ユニマルシェラーun=jc.reate Unimasharer()
JAXBElement<T>elem=(JAXBElement<T>)unmashare(steamSource、cls);JSONがrootを含まない場合は、clsタイプを指定しなければなりません。ですから、ここではStreamSourceを使わなければなりません。直接readerを使ってはいけません。そうでないと、clsタイプを指定することができません。
return  (T)(elem.getValue();
 
)
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import org.eclipse.persistence.jaxb.JAXBContextProperties;

public class DtoUtil {
	@SuppressWarnings("unchecked")
	public static <T>T fromJson(StreamSource streamSource, Class<?> cls, boolean hasRoot)
			throws JAXBException, ClassCastException {
		Map<String, Object> properties = new HashMap<String, Object>(2);
		properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
		properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, hasRoot);
		JAXBContext jc = org.eclipse.persistence.jaxb.JAXBContextFactory
				.createContext(new Class[] { cls }, properties);
		Unmarshaller un = jc.createUnmarshaller();
		JAXBElement<T> elem = (JAXBElement<T>) un.unmarshal(streamSource,cls);
		return  (T)(elem.getValue());
	}
	public static <T> String toJson(T obj,Class<?>cls,boolean hasRoot) throws JAXBException,
			ClassCastException {
		Map<String, Object> properties = new HashMap<String, Object>(2);
		properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
		properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, hasRoot);
		JAXBContext jc = org.eclipse.persistence.jaxb.JAXBContextFactory
				.createContext(new Class[] { cls }, properties);
		Marshaller marshaller = jc.createMarshaller();
		StringWriter writer = new StringWriter();
		marshaller.marshal(obj, writer);
		return writer.toString();
	}
}
 
 
maven dependency:
<dependency>
<groupId>org.eclipse.persistence
<artftitId>eclipspelink
<version>2.5.0

 
使い方、例えば:
DtoUtil.froomJson(new StreamSource、cls、has Root);
DtoUtil.toJson(Obj,cls,has Root);