Created
February 6, 2011 07:52
-
-
Save ussy/813214 to your computer and use it in GitHub Desktop.
plugin ファイルは src/main/resources/META-INF/services/ に配置
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.seasar.cubby.plugins.jackson.spi; | |
import java.io.IOException; | |
import org.codehaus.jackson.map.ObjectMapper; | |
import org.seasar.cubby.spi.ContainerProvider; | |
import org.seasar.cubby.spi.JsonProvider; | |
import org.seasar.cubby.spi.ProviderFactory; | |
import org.seasar.cubby.spi.container.Container; | |
import org.seasar.cubby.spi.container.LookupException; | |
import org.slf4j.LoggerFactory; | |
public class JacksonJsonProvider implements JsonProvider { | |
private ObjectMapper mapper; | |
@Override | |
public String toJson(final Object o) { | |
if (mapper == null) { | |
mapper = createObjectMapper(); | |
} | |
try { | |
return mapper.writeValueAsString(o); | |
} catch (IOException e) { | |
LoggerFactory.getLogger(this.getClass()).error(e.getMessage(), e); | |
return null; | |
} | |
} | |
private ObjectMapper createObjectMapper() { | |
final Container container = ProviderFactory.get(ContainerProvider.class).getContainer(); | |
try { | |
return container.lookup(ObjectMapper.class); | |
} catch (final LookupException e) { | |
return new ObjectMapper(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.seasar.cubby.plugins.jackson; | |
import org.seasar.cubby.plugin.AbstractPlugin; | |
import org.seasar.cubby.plugins.jackson.spi.JacksonJsonProvider; | |
import org.seasar.cubby.spi.JsonProvider; | |
import org.seasar.cubby.spi.Provider; | |
public class JacksonPlugin extends AbstractPlugin { | |
/** JSON のプロバイダ。 */ | |
private final JsonProvider jsonProvider = new JacksonJsonProvider(); | |
/** | |
* インスタンス化します。 | |
*/ | |
public JacksonPlugin() { | |
support(JsonProvider.class); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public <S extends Provider> S getProvider(final Class<S> service) { | |
if (JsonProvider.class.equals(service)) { | |
return service.cast(jsonProvider); | |
} | |
return null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
org.seasar.cubby.plugins.jackson.JacksonPlugin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment