Skip to content

Commit

Permalink
Issue #85: Marshal Duration config fields as milliseconds for easier …
Browse files Browse the repository at this point in the history
…use in javascript.
  • Loading branch information
mk23 committed Jan 30, 2016
1 parent 7f8faab commit 3c9c09b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/github/mk23/jmxproxy/conf/AppConfig.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.mk23.jmxproxy.conf;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import io.dropwizard.jackson.JsonSnakeCase;

Expand Down Expand Up @@ -49,6 +50,7 @@ public class AppConfig {
*/
@Valid
@JsonProperty
@JsonSerialize(using = DurationSerializer.class)
private Duration cleanInterval = Duration.minutes(DEFAULT_CLEAN_INTERVAL);

/**
Expand All @@ -57,6 +59,7 @@ public class AppConfig {
*/
@Valid
@JsonProperty
@JsonSerialize(using = DurationSerializer.class)
private Duration cacheDuration = Duration.minutes(DEFAULT_CACHE_DURATION);

/**
Expand All @@ -65,6 +68,7 @@ public class AppConfig {
*/
@Valid
@JsonProperty
@JsonSerialize(using = DurationSerializer.class)
private Duration accessDuration = Duration.minutes(DEFAULT_ACCESS_DURATION);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.mk23.jmxproxy.conf;

import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;

import io.dropwizard.util.Duration;

import java.io.IOException;

/**
* <p>Custom serializer for configuration Duration fields.</p>
*
* Converts <code>io.dropwizard.util.Duration</code> types to milliseconds
* for JSON requests.
*
* @see <a href="http://dropwizard.github.io/dropwizard/0.9.2/dropwizard-util/apidocs/io/dropwizard/util/Duration.html">io.dropwizard.util.Duration</a>
*
* @author mk23
* @since 2016-01-29
* @version 3.2.1
*/
public class DurationSerializer extends JsonSerializer<Duration> {
/** {@inheritDoc} */
@Override
public final void serialize(
final Duration duration,
final JsonGenerator jgen,
final SerializerProvider provider
) throws IOException, JsonProcessingException {
jgen.writeNumber(duration.toMilliseconds());
}
}

0 comments on commit 3c9c09b

Please sign in to comment.