Skip to content

Commit

Permalink
Fix #29
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 2, 2015
1 parent 6362a83 commit 845b52d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
13 changes: 13 additions & 0 deletions jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/JSON.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ public enum Feature
*/
WRITE_ENUMS_USING_INDEX(false),

/**
* Feature that determines whether Date (and date/time) values
* (and Date-based things like {@link java.util.Calendar}s) are to be
* serialized as numeric timestamps (true),
* or using a textual representation (false)
*<p>
* Feature is disabled by default, so that date/time values are
* serialized as text, NOT timestamp.
*
* @since 2.7
*/
WRITE_DATES_AS_TIMESTAMP(false),

/**
* Feature that can be enabled to use "pretty-printing", basic indentation
* to make resulting JSON easier to read by humans by adding white space
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
*/
public class JSONWriter
{
private final static TimeZone DEFAULT_TIMEZONE = TimeZone.getTimeZone("UTC");

/*
/**********************************************************************
/* Blueprint config
Expand All @@ -42,15 +44,17 @@ public class JSONWriter
protected final TreeCodec _treeCodec;

protected final boolean _requireSetter;

/*
/**********************************************************************
/* Instance config
/**********************************************************************
*/

protected final JsonGenerator _generator;


protected final TimeZone _timezone;

/*
/**********************************************************************
/* Blueprint construction
Expand All @@ -68,6 +72,7 @@ public JSONWriter(int features, TypeDetector td, TreeCodec tc)
_treeCodec = tc;
_generator = null;
_requireSetter = JSON.Feature.WRITE_READONLY_BEAN_PROPERTIES.isDisabled(features);
_timezone = DEFAULT_TIMEZONE;
}

/**
Expand All @@ -80,6 +85,7 @@ protected JSONWriter(JSONWriter base, JsonGenerator jgen)
_treeCodec = base._treeCodec;
_generator = jgen;
_requireSetter = JSON.Feature.WRITE_READONLY_BEAN_PROPERTIES.isDisabled(_features);
_timezone = DEFAULT_TIMEZONE;
}

/*
Expand Down Expand Up @@ -634,14 +640,21 @@ protected void writeNullField(SerializedString fieldName) throws IOException {
_generator.writeNull();
}
}

protected void writeDateValue(Date v) throws IOException {
// TODO: maybe allow serialization using timestamp?
writeStringValue(v.toString());
if (Feature.WRITE_DATES_AS_TIMESTAMP.isEnabled(_features)) {
writeLongValue(v.getTime());
} else {
writeStringValue(dateToString(v));
}
}

protected void writeDateField(String fieldName, Date v) throws IOException {
writeStringField(fieldName, v.toString());
if (Feature.WRITE_DATES_AS_TIMESTAMP.isEnabled(_features)) {
writeLongField(fieldName, v.getTime());
} else {
writeStringField(fieldName, dateToString(v));
}
}

protected void writeEnumValue(Enum<?> v) throws IOException {
Expand Down Expand Up @@ -721,4 +734,17 @@ protected String keyToString(Object rawKey)
}
return String.valueOf(rawKey);
}

/**
* @since 2.7
*/
protected String dateToString(Date v) {
if (v == null) {
return "";
}
// !!! 01-Dec-2015, tatu: Should really use proper DateFormat or something
// since this relies on system-wide defaults, and hard/impossible to
// change easily
return v.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.ob.TestBase;

public class ReadEnumMapTest extends TestBase
// for [jackson-jr#21]
public class ReadEnumMap21Test extends TestBase
{
enum DEF { D, E, F; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.jr.ob.JSON;
import com.fasterxml.jackson.jr.ob.TestBase;

// for [jackson-jr#25], allowing single-int constructors
public class ReadWithCtors25Test extends TestBase
{
static class FromInt1 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public void testUnknownType() throws Exception
}
}

// For [Issue#16]
// For [jackson-jr#16]
public void testTypedMaps() throws Exception
{
final Address from = new Address("xyz");
Expand All @@ -136,4 +136,27 @@ public void testTypedMaps() throws Exception
assertNotNull(map);
assertEquals(2, map.size());
}

// For [jackson-jr#29]
public void testSimpleDates() throws Exception
{
final Date input = new Date(0L);
JSON j = JSON.std;

assertFalse(j.isEnabled(Feature.WRITE_DATES_AS_TIMESTAMP));

String json = j.asString(input);
// What to test? For now, accept two variants we may get, depending
// on timezone (which we can not, alas, control)
if (!json.contains("Dec 31")
&& !json.contains("Jan 1")) {
fail("Invalid output: "+json);
}

j = j.with(Feature.WRITE_DATES_AS_TIMESTAMP);
assertTrue(j.isEnabled(Feature.WRITE_DATES_AS_TIMESTAMP));

json = j.asString(input);
assertEquals("0", json);
}
}
3 changes: 3 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Project: jackson-jr
2.7.0 (not yet released)

#28: Remove misspelled `JSON.Feature.USE_IS_SETTERS`
#29: Add `JSON.Feature.WRITE_DATES_AS_TIMESTAMP`, enabling of which allows
serialization of `java.util.Date` as long
(suggested by Giulio P (gpiancastelli@github))
#31: Fix failure writing UUID, URL and URI
(reported by Mitsunori K (komamitsu@github))

Expand Down

0 comments on commit 845b52d

Please sign in to comment.