All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.espr.mvc.converter.StringToObjectConverter Maven / Gradle / Ivy

There is a newer version: 0.3.4
Show newest version
package it.espr.mvc.converter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import it.espr.mvc.json.Json;

public class StringToObjectConverter extends AStringToCastingConverter {

	private static final Logger log = LoggerFactory.getLogger(StringToObjectConverter.class);

	private Json json;

	public StringToObjectConverter(Json json) {
		this.json = json;
	}

	@Override
	public Class getType() {
		return Object.class;
	}

	@Override
	public Object convert(String value) throws StringToTypeConverterException {
		throw new UnsupportedOperationException("Not supported");
	}

	@Override
	public  Cast convert(Class c, String value) throws StringToTypeConverterException {
		if (json != null) {
			try {
				log.debug("Deserialising with {}", json);
				Cast data = this.json.deserialise(c, value);
				log.debug("Deserialised with {}", json);
				return data;
			} catch (Exception e) {
				log.error("Problem when deseriliasing {} to {}", value, c, e);
				throw new StringToTypeConverterException("Problem when converting from json", e);
			}
		} else {
			throw new UnsupportedOperationException("No json serialiser/deserialiser registered - implement it.espr.mvc.json.Json and bind it.");
		}
	}
}