
org.hudsonci.rest.common.JacksonCodec Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
*
* Copyright (c) 2010-2011 Sonatype, Inc.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
*
*
*
*
*******************************************************************************/
package org.hudsonci.rest.common;
import org.codehaus.jackson.map.ObjectMapper;
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Singleton;
import java.io.IOException;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Jackson JSON codec.
*
* @author Jason Dillon
* @since 2.1.0
*/
@Named
@Singleton
public class JacksonCodec
implements JsonCodec
{
private final ObjectMapper mapper;
@Inject
public JacksonCodec(final ObjectMapper mapper) {
this.mapper = checkNotNull(mapper);
}
public JacksonCodec() {
this(new ObjectMapperProvider().get());
}
public ObjectMapper getMapper() {
return mapper;
}
public String encode(final Object value) throws IOException {
checkNotNull(value);
return mapper.writeValueAsString(value);
}
public T decode(final String value, final Class type) throws IOException {
checkNotNull(value);
checkNotNull(type);
return mapper.readValue(value, type);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy