-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial Commit for Java.util.Date Extension Support + LocalDateTime Unit Test #124
Open
Shounaks
wants to merge
7
commits into
FasterXML:2.17
Choose a base branch
from
Shounaks:2.17-date-extension
base: 2.17
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5859097
Initial Commit for Java.util.Date Extension Support + LocalDateTime U…
Shounaks 7c93547
Merge branch '2.17' into 2.17-date-extension
Shounaks 2fd907f
Renaming Test Class Name.
Shounaks efa53ed
Merge branch '2.17' into 2.17-date-extension
Shounaks 52ac658
Merge branch '2.17' into 2.17-date-extension
cowtowncoder 3a26bac
Merge branch '2.17' into 2.17-date-extension
cowtowncoder cc7534d
Merge branch '2.17' into 2.17-date-extension
cowtowncoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
...atime/src/main/java/com/fasterxml/jackson/jr/extension/javatime/date/DateValueReader.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.fasterxml.jackson.jr.extension.javatime.date; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.jr.ob.api.ValueReader; | ||
import com.fasterxml.jackson.jr.ob.impl.JSONReader; | ||
|
||
import java.io.IOException; | ||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
|
||
public class DateValueReader extends ValueReader { | ||
protected DateValueReader() { | ||
super(Date.class); | ||
} | ||
|
||
@Override | ||
public Object read(JSONReader reader, JsonParser p) throws IOException { | ||
try { | ||
return new SimpleDateFormat().parse(p.getText()); | ||
} catch (ParseException e) { | ||
throw new IOException(e); | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
...atime/src/main/java/com/fasterxml/jackson/jr/extension/javatime/date/DateValueWriter.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.fasterxml.jackson.jr.extension.javatime.date; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.jr.ob.api.ValueWriter; | ||
import com.fasterxml.jackson.jr.ob.impl.JSONWriter; | ||
|
||
import java.io.IOException; | ||
import java.util.Date; | ||
|
||
public class DateValueWriter implements ValueWriter { | ||
@Override | ||
public void writeValue(JSONWriter context, JsonGenerator g, Object value) throws IOException { | ||
String date = value.toString(); | ||
context.writeValue(date); | ||
} | ||
|
||
@Override | ||
public Class<?> valueType() { | ||
return Date.class; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...ain/java/com/fasterxml/jackson/jr/extension/javatime/date/JacksonJrJavaDateExtension.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.fasterxml.jackson.jr.extension.javatime.date; | ||
|
||
import com.fasterxml.jackson.jr.ob.JacksonJrExtension; | ||
import com.fasterxml.jackson.jr.ob.api.ExtensionContext; | ||
|
||
public class JacksonJrJavaDateExtension extends JacksonJrExtension { | ||
static final JavaDateReaderWriterProvider DEFAULT_RW_PROVIDER = new JavaDateReaderWriterProvider(); | ||
|
||
private JavaDateReaderWriterProvider readerWriterProvider = DEFAULT_RW_PROVIDER; | ||
|
||
@Override | ||
protected void register(ExtensionContext ctxt) { | ||
ctxt.insertProvider(readerWriterProvider); | ||
} | ||
|
||
public JacksonJrJavaDateExtension with(JavaDateReaderWriterProvider p) { | ||
readerWriterProvider = p; | ||
return this; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...n/java/com/fasterxml/jackson/jr/extension/javatime/date/JavaDateReaderWriterProvider.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.fasterxml.jackson.jr.extension.javatime.date; | ||
|
||
import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider; | ||
import com.fasterxml.jackson.jr.ob.api.ValueReader; | ||
import com.fasterxml.jackson.jr.ob.api.ValueWriter; | ||
import com.fasterxml.jackson.jr.ob.impl.JSONReader; | ||
import com.fasterxml.jackson.jr.ob.impl.JSONWriter; | ||
|
||
import java.util.Date; | ||
|
||
public class JavaDateReaderWriterProvider extends ReaderWriterProvider { | ||
public JavaDateReaderWriterProvider() { | ||
} | ||
|
||
@Override | ||
public ValueReader findValueReader(JSONReader readContext, Class<?> type) { | ||
return Date.class.isAssignableFrom(type) ? new DateValueReader() : null; | ||
} | ||
|
||
@Override | ||
public ValueWriter findValueWriter(JSONWriter writeContext, Class<?> type) { | ||
return Date.class.isAssignableFrom(type) ? new DateValueWriter() : null; | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
.../javatime/JacksonJrJavaTimeExtension.java → ...ldatetime/JacksonJrJavaTimeExtension.java
This file contains 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
This file contains 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
2 changes: 1 addition & 1 deletion
2
...on/javatime/LocalDateTimeValueReader.java → ...caldatetime/LocalDateTimeValueReader.java
This file contains 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
2 changes: 1 addition & 1 deletion
2
...on/javatime/LocalDateTimeValueWriter.java → ...caldatetime/LocalDateTimeValueWriter.java
This file contains 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
52 changes: 52 additions & 0 deletions
52
...rc/test/java/com/fasterxml/jackson/jr/extension/javatime/JacksonJrJavaExtensionsTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.fasterxml.jackson.jr.extension.javatime; | ||
|
||
import com.fasterxml.jackson.jr.extension.javatime.localdatetime.JacksonJrJavaTimeExtension; | ||
import com.fasterxml.jackson.jr.extension.javatime.date.JacksonJrJavaDateExtension; | ||
import com.fasterxml.jackson.jr.ob.JSON; | ||
import junit.framework.TestCase; | ||
|
||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.util.Date; | ||
|
||
public class JacksonJrJavaExtensionsTest extends TestCase { | ||
|
||
public void testLocalDateTimeTest() throws Exception { | ||
//Register the extension | ||
JSON json = JSON.builder().register(new JacksonJrJavaTimeExtension()).build(); | ||
|
||
//Test | ||
LocalDateTime sampleDateTime = LocalDateTime.of(2024, 2, 25, 2, 32); | ||
String expectedOutput = "{\"aVariable\":\"A_VARIABLE_TEST\",\"dateTime\":\"2024-02-25T02:32:00\"}"; | ||
String aVariableTest = json.asString(new LocalDateTimeClass(sampleDateTime, "A_VARIABLE_TEST")); | ||
assertEquals(aVariableTest, expectedOutput); | ||
} | ||
|
||
public void testDateTest() throws Exception { | ||
//Register the extension | ||
JSON json = JSON.builder().register(new JacksonJrJavaDateExtension()).build(); | ||
Date date = Date.from(Instant.now()); | ||
String expectedOutput = "{\"aVariable\":\"A_VARIABLE_TEST\",\"date\":\""+ date +"\"}"; | ||
String aVariableTest = json.asString(new DateClass(date, "A_VARIABLE_TEST")); | ||
assertEquals(aVariableTest, expectedOutput); | ||
} | ||
} | ||
|
||
class DateClass { | ||
public Date date; | ||
public String aVariable; | ||
|
||
public DateClass(Date date, String aVariable) { | ||
this.date = date; | ||
this.aVariable = aVariable; | ||
} | ||
} | ||
|
||
class LocalDateTimeClass { | ||
public LocalDateTime dateTime; | ||
public String aVariable; | ||
public LocalDateTimeClass(LocalDateTime dateTime, String aVariable) { | ||
this.dateTime = dateTime; | ||
this.aVariable = aVariable; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically this is not JAva 8 date/time type... so probably should be in core
jr-objects
?But also, it needs to support deserialization from Integer numbers (timestamp).