json转换为boolean 扩展接受值(json-lib)

项目中需要把某些值为1的转换为true,值为0的转换为false。
json-lib默认会将"true","yes","on"转换为true,把"false","no","off"转换为false。
public final class BooleanMorpher extends AbstractPrimitiveMorpher 被设计为final了。
思路重新写个类替换BooleanMorpher 。代码如下:

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

import net.sf.ezmorph.MorphException;
import net.sf.ezmorph.primitive.AbstractPrimitiveMorpher;

/**
 * 转换json为boolean类型
 * 
 * @author qiaoyao
 * @version 1.0,2010-6-2
 */
public class MapToBooleanMorpher extends AbstractPrimitiveMorpher {

	private boolean defaultValue;

	public MapToBooleanMorpher() {
	}

	public MapToBooleanMorpher(boolean defaultValue) {
		super(true);
		this.defaultValue = defaultValue;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}

		if (!(obj instanceof MapToBooleanMorpher)) {
			return false;
		}

		MapToBooleanMorpher other = (MapToBooleanMorpher) obj;
		EqualsBuilder builder = new EqualsBuilder();
		if ((super.isUseDefault()) && (other.isUseDefault())) {
			builder.append(getDefaultValue(), other.getDefaultValue());
			return builder.isEquals();
		}
		if ((!super.isUseDefault()) && (!other.isUseDefault())) {
			return builder.isEquals();
		}
		return false;
	}

	public boolean getDefaultValue() {
		return this.defaultValue;
	}

	@Override
	public int hashCode() {
		HashCodeBuilder builder = new HashCodeBuilder();
		if (super.isUseDefault()) {
			builder.append(getDefaultValue());
		}
		return builder.toHashCode();
	}

	public boolean morph(Object value) {
		if (value == null) {
			if (super.isUseDefault()) {
				return this.defaultValue;
			}
			throw new MorphException("value is null");
		}

		if (value instanceof Boolean)
			return ((Boolean) value).booleanValue();
		if (value instanceof Number) {
			if ((value instanceof Double)
					&& (((Double.isInfinite(((Number) value).doubleValue())) || (Double
							.isNaN(((Number) value).doubleValue()))))) {
				return true;
			}
			if ((value instanceof Float)
					&& (((Float.isInfinite(((Number) value).floatValue())) || (Float
							.isNaN(((Number) value).floatValue()))))) {
				return true;
			}
			long longValue = ((Number) value).longValue();
			return longValue != 0L;
		}
		String s = String.valueOf(value);

		if ((s.equalsIgnoreCase("true")) || (s.equalsIgnoreCase("yes"))
				|| (s.equalsIgnoreCase("on")) || (s.equalsIgnoreCase("1")))
			return true;
		if ((s.equalsIgnoreCase("false")) || (s.equalsIgnoreCase("no"))
				|| (s.equalsIgnoreCase("off")) || (s.equalsIgnoreCase("0")))
			return false;
		if (super.isUseDefault()) {
			return this.defaultValue;
		}

		throw new MorphException("Can't morph value: " + value);
	}

	@Override
	public Class<?> morphsTo() {
		return Boolean.TYPE;
	}

}


调用如下:

import com.winxuan.paipai.support.sdk.util.json.morpher.MapToBooleanMorpher;

import net.sf.ezmorph.MorpherRegistry;
import net.sf.ezmorph.object.DateMorpher;
import net.sf.ezmorph.primitive.BooleanMorpher;
import net.sf.json.util.JSONUtils;


/**
 * 自定义转为json需注册的Morpher
 * 
 * @author qiaoyao
 * @version 1.0,下午11:05:43
 */
public class CustomMorpherRegistry {

	public static final String[] DATE_FORMAT = { "yyyy-MM-dd HH:mm:ss" };

	/*
	 * 清除原BooleanMorpher,注册DateMorpher,MapToBooleanMorpher
	 */
	public static void registerMorpher() {
		JSONUtils.getMorpherRegistry().clear(BooleanMorpher.class);//(如果下面用true,此句是可选的)
		MorpherRegistry morpherRegistry = JSONUtils.getMorpherRegistry();
		morpherRegistry.registerMorpher(new DateMorpher(DATE_FORMAT));
		morpherRegistry.registerMorpher(new MapToBooleanMorpher(), true);//true表示覆盖之前默认的BooleanMorpher类。
	}

}


你可能感兴趣的:(apache,json,.net)