Skip to content
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
wants to merge 7 commits into
base: 2.17
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

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).

}

@Override
public Object read(JSONReader reader, JsonParser p) throws IOException {
try {
return new SimpleDateFormat().parse(p.getText());
} catch (ParseException e) {
throw new IOException(e);
}
}
}
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;
}
}
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;
}
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.fasterxml.jackson.jr.extension.javatime;
package com.fasterxml.jackson.jr.extension.javatime.localdatetime;

import com.fasterxml.jackson.jr.ob.JacksonJrExtension;
import com.fasterxml.jackson.jr.ob.api.ExtensionContext;

public class JacksonJrJavaTimeExtension extends JacksonJrExtension {
final static JavaTimeReaderWriterProvider DEFAULT_RW_PROVIDER = new JavaTimeReaderWriterProvider();
static final JavaTimeReaderWriterProvider DEFAULT_RW_PROVIDER = new JavaTimeReaderWriterProvider();

private JavaTimeReaderWriterProvider readerWriterProvider = DEFAULT_RW_PROVIDER;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.jr.extension.javatime;
package com.fasterxml.jackson.jr.extension.javatime.localdatetime;

import com.fasterxml.jackson.jr.ob.api.ReaderWriterProvider;
import com.fasterxml.jackson.jr.ob.api.ValueReader;
Expand Down Expand Up @@ -37,7 +37,7 @@ public ValueWriter findValueWriter(JSONWriter writeContext, Class<?> type) {
* </li>
*</ul>
*
* @param formatter
* @param formatter {@link DateTimeFormatter} instance
*
* @return This provider instance for call chaining
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.jr.extension.javatime;
package com.fasterxml.jackson.jr.extension.javatime.localdatetime;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.jr.ob.api.ValueReader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.jr.extension.javatime;
package com.fasterxml.jackson.jr.extension.javatime.localdatetime;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.jr.ob.api.ValueWriter;
Expand Down
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;
}
}