Skip to content

Commit

Permalink
Updated c.n.j.JSONCoercibleType to support java.time.temporal.Tempora…
Browse files Browse the repository at this point in the history
…l and java.time.temporal.TemporalAmount types if the underlying ObjectMapper has supporting modules.

Also now leverages ObjectMapper#treeToValue.
  • Loading branch information
cwensel committed Aug 9, 2021
1 parent fc9dad1 commit b7f6523
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ Cascading Change Log

4.1 [unreleased]

Updated c.n.j.JSONCoercibleType to support java.time.temporal.Temporal and java.time.temporal.TemporalAmount types
if the underlying ObjectMapper has supporting modules. Also now leverages ObjectMapper#treeToValue.

Added c.t.t.CoercionFrom and c.t.t.ToCanonical interfaces to support faster type coercions.

Added local mode support for Apache Kafka, Neo4j, Splunk, and AWS S3 by merging with the cascading-local project.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import java.io.IOException;
import java.lang.reflect.Type;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAmount;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -215,10 +217,16 @@ public <T> CoercionFrom<JsonNode, T> to( Type to )
return node -> ifNull( node, n -> n.getNodeType() == JsonNodeType.BOOLEAN ? (T) Boolean.valueOf( n.booleanValue() ) : Coercions.coerce( textOrWrite( n ), to ) );

if( Map.class.isAssignableFrom( actualTo ) )
return node -> ifNull( node, n -> (T) convert( n, actualTo ) );
return node -> ifNull( node, n -> (T) convertTree( n, actualTo ) );

if( List.class.isAssignableFrom( actualTo ) )
return node -> ifNull( node, n -> (T) convert( n, actualTo ) );
return node -> ifNull( node, n -> (T) convertTree( n, actualTo ) );

if( Temporal.class.isAssignableFrom( actualTo ) )
return node -> ifNull( node, n -> (T) convertTree( n, actualTo ) );

if( TemporalAmount.class.isAssignableFrom( actualTo ) )
return node -> ifNull( node, n -> (T) convertTree( n, actualTo ) );

if( to instanceof CoercibleType )
return node -> ifNull( node, n -> (T) ( (CoercibleType<?>) to ).canonical( textOrWrite( n ) ) );
Expand Down Expand Up @@ -283,20 +291,33 @@ public <Coerce> Coerce coerce( Object value, Type to )
return nodeType == JsonNodeType.BOOLEAN ? (Coerce) Boolean.valueOf( node.booleanValue() ) : (Coerce) Coercions.coerce( textOrWrite( node ), to );

if( Map.class.isAssignableFrom( actualTo ) )
return (Coerce) convert( value, actualTo );
return (Coerce) convertTree( node, actualTo );

if( List.class.isAssignableFrom( actualTo ) )
return (Coerce) convert( value, actualTo );
return (Coerce) convertTree( node, actualTo );

if( Temporal.class.isAssignableFrom( actualTo ) )
return (Coerce) convertTree( node, actualTo );

if( TemporalAmount.class.isAssignableFrom( actualTo ) )
return (Coerce) convertTree( node, actualTo );

if( to instanceof CoercibleType )
return (Coerce) ( (CoercibleType<?>) to ).canonical( textOrWrite( node ) );

throw new CascadingException( "unknown type coercion requested, from: " + Util.getTypeName( from ) + " to: " + Util.getTypeName( to ) );
}

private Object convert( Object value, Class to )
private Object convertTree( JsonNode value, Class<?> to )
{
return mapper.convertValue( value, to );
try
{
return mapper.treeToValue( value, to );
}
catch( JsonProcessingException exception )
{
throw new CascadingException( "unable to coerce json node into " + to.getName(), exception );
}
}

private String textOrWrite( JsonNode value )
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016-2020 Chris K Wensel. All Rights Reserved.
* Copyright (c) 2016-2021 Chris K Wensel. All Rights Reserved.
*
* Project and contact information: http://www.cascading.org/
*
Expand Down Expand Up @@ -121,6 +121,24 @@ public void pojoCoercions()
assertEquals( "1525456424.337000000", coerce );
}

@Test
public void pojoCoercionsReversed()
{
ObjectMapper mapper = new ObjectMapper();

mapper.registerModule( new JavaTimeModule() );

JSONCoercibleType type = new JSONCoercibleType( mapper );

Instant instant = Instant.ofEpochSecond( 1525456424, 337000000 );

JsonNode canonical = type.canonical( instant );

Instant coerce = type.coerce( canonical, Instant.class );

assertEquals( instant, coerce );
}

private void testContainerCoercion( String value, JsonNodeType nodeType, Class resultType )
{
JsonNode canonical = JSONCoercibleType.TYPE.canonical( value );
Expand Down

0 comments on commit b7f6523

Please sign in to comment.