/**
 * Copyright 2008-2009. Chongqing CRun Information Industry Co.,Ltd. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.jackson;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.springframework.util.StringUtils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;

/**
 * @author 田平 create 2017年10月27日下午4:31:34
 */
public class URIDeserializer extends StdScalarDeserializer<URI> {

	private static final long serialVersionUID = 1L;

	protected URIDeserializer(Class<URI> date) {
		super(date);
	}

	/**
	 * @see com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext)
	 */
	@Override
	public URI deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
		if (StringUtils.hasText(p.getText())) {
			try {
				return new URI(p.getText());
			} catch (URISyntaxException e) {
				throw new RuntimeException(e);
			}
		} else {
			return null;
		}
	}

}
