Replies: 3 comments
-
I'd need a reproduction of the issue including how you are invoking read, wrt Date types. And even more importantly, how are you registering your As to |
Beta Was this translation helpful? Give feedback.
-
Sure. Here are the relevant pieces. StoryIf a JSON packet contains an iso8601 date/time deserialize it to a Java Sample JSON{
"string" : "boxlang",
"numeric" : 1,
"float" : 41.1,
"struct" : { "one" : "wood" },
"array" : [1,2,3],
"date" : "2022-10-31",
"dateTime": "2022-10-31T09:00:00.594Z",
"dateTime2": "2022-10-31T09:00:00Z"
} BuilderHere is how we build our builder for our app: /**
* The JSON builder library we use
*/
private static final JSON JSON_BUILDER = JSON.builder(
// Use a custom factory with enabled parsing features
new JsonFactory()
.enable( JsonParser.Feature.ALLOW_COMMENTS )
.enable( JsonParser.Feature.ALLOW_YAML_COMMENTS )
)
// Enable JSON features
// https://fasterxml.github.io/jackson-jr/javadoc/jr-objects/2.8/com/fasterxml/jackson/jr/ob/JSON.Feature.html
.enable(
JSON.Feature.PRETTY_PRINT_OUTPUT,
JSON.Feature.USE_BIG_DECIMAL_FOR_FLOATS,
JSON.Feature.USE_FIELDS,
JSON.Feature.WRITE_NULL_PROPERTIES
)
// Add Jackson annotation support
.register( JacksonAnnotationExtension.std )
// Add JavaTime Extension
.register( new JacksonJrJavaTimeExtension() )
// Add Custom Serializers/ Deserializers
.register( new JacksonJrExtension() {
@Override
protected void register( ExtensionContext extensionContext ) {
extensionContext.insertProvider( new BoxJsonProvider() );
}
} )
// Yeaaaahaaa!
.build(); Our BoxJson Provider/**
* Custom BoxLang Serializers by type
*/
@Override
public ValueWriter findValueWriter( JSONWriter writeContext, Class<?> type ) {
if ( type == DateTime.class || type == LocalDate.class || type == Date.class || type == java.sql.Date.class ) {
return new DateTime();
}
if ( IClassRunnable.class.isAssignableFrom( type ) ) {
return new BoxClassSerializer();
}
if ( type == DynamicObject.class ) {
return new DynamicObjectSerializer();
}
return null;
}
/**
* Custom BoxLang Deserializers by type
*/
@Override
public ValueReader findValueReader( JSONReader readContext, Class<?> type ) {
// DateTime Objects
if ( type.equals( DateTime.class ) ) {
return new DateTimeDeserializer();
}
// Java Date Objects
if ( type.equals( LocalDate.class ) ) {
return new LocalDateDeserializer();
}
// Java DateTime Objects
if ( type.equals( java.time.LocalDateTime.class ) ) {
return new LocalDateTimeDeserializer();
}
// All Map types funneled through Struct
if ( type.isAssignableFrom( Map.class ) ) {
return new StructDeserializer();
}
// All Lists funneled through Array
if ( type.isAssignableFrom( List.class ) ) {
return new ArrayDeserializer();
}
return null;
} LocalDateTimeDeserializerpublic class LocalDateTimeDeserializer extends ValueReader {
/**
* Constructor
*/
public LocalDateTimeDeserializer() {
super( LocalDateTime.class );
}
@Override
public Object read( JSONReader reader, JsonParser jsonParser ) throws IOException {
return DateTimeCaster.cast( jsonParser.getText() );
}
} |
Beta Was this translation helpful? Give feedback.
-
Quick not on
in which element/value readers are passed in to be optionally used by custom readers. As to date/time classes, I think what'd be needed is really a self-contained unit test just for one types: I suspect that whatever is the problem it applies to all types. |
Beta Was this translation helpful? Give feedback.
-
I now know how to create custom deserializer. However, I can't seem to pin point how to create one to convert a json iso8601 string to a Java LocalDate time object.
Here is a portion of my
ReaderWriterProvider
However, it seems jackson jr, is ALWAYS treating iso8601 date/times as just strings and leaves the item as a string.
Beta Was this translation helpful? Give feedback.
All reactions