return nodeType == JsonNodeType.STRING ? (Coerce) node.textValue() : (Coerce) textOrWrite( node ); return nodeType == JsonNodeType.NUMBER ? (Coerce) Integer.valueOf( node.intValue() ) : (Coerce) Coercions.coerce( textOrWrite( node ), to ); return nodeType == JsonNodeType.NUMBER ? (Coerce) Long.valueOf( node.longValue() ) : (Coerce) Coercions.coerce( textOrWrite( node ), to ); return nodeType == JsonNodeType.NUMBER ? (Coerce) Float.valueOf( node.floatValue() ) : (Coerce) Coercions.coerce( textOrWrite( node ), to ); return nodeType == JsonNodeType.NUMBER ? (Coerce) Double.valueOf( node.doubleValue() ) : (Coerce) Coercions.coerce( textOrWrite( node ), to ); return nodeType == JsonNodeType.BOOLEAN ? (Coerce) Boolean.valueOf( node.booleanValue() ) : (Coerce) Coercions.coerce( textOrWrite( node ), to ); return (Coerce) convert( value, (Class) to ); return (Coerce) convert( value, (Class) to );
@Override public int hashCode() { return getCanonicalType().hashCode(); }
private JsonNode nodeOrParse( String value ) { try { return parse( value ); // presume this is a JSON string } catch( JsonParseException exception ) { return JsonNodeFactory.instance.textNode( value ); } catch( IOException exception ) { throw new CascadingException( "unable to read json", exception ); } }
@Test public void mapCoercions() { Map<String, Object> map = new LinkedHashMap<>(); map.put( "name", "John Doe" ); map.put( "list", Arrays.asList( "John", "Jane" ) ); JsonNode canonical = JSONCoercibleType.TYPE.canonical( map ); assertEquals( JsonNodeType.OBJECT, canonical.getNodeType() ); assertEquals( map, JSONCoercibleType.TYPE.coerce( canonical, Map.class ) ); }
@Test public void pojoCoercions() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule( new JavaTimeModule() ); JSONCoercibleType type = new JSONCoercibleType( mapper ); Instant instant = Instant.ofEpochSecond( 1525456424, 337000000 ); JsonNode canonical = type.canonical( instant ); String coerce = type.coerce( canonical, String.class ); assertEquals( "1525456424.337000000", coerce ); }
/** * Constructor JSONTextLine creates a new JSONTextLine instance for use with any of the * Hadoop based {@link cascading.flow.FlowConnector} instances. * * @param mapper of ObjectMapper * @param fields of Fields * @param sinkCompression of Compress * @param charsetName of String */ public JSONTextLine( ObjectMapper mapper, Fields fields, Compress sinkCompression, String charsetName ) { super( sinkCompression ); if( mapper != null ) this.mapper = mapper; if( fields == null ) throw new IllegalArgumentException( "fields may not be null" ); if( !fields.isDefined() ) throw new IllegalArgumentException( "fields argument must declare a single field" ); if( fields.size() != 1 ) throw new IllegalArgumentException( "may only declare a single source/sink field in the fields argument" ); fields = fields.hasTypes() ? fields : fields.applyTypes( new JSONCoercibleType( this.mapper ) ); setSinkFields( fields ); setSourceFields( fields ); // throws an exception if not found setCharsetName( charsetName ); }
private String textOrWrite( JsonNode value ) { if( value != null && value.isTextual() ) return value.textValue(); try { return write( value ); } catch( JsonProcessingException exception ) { throw new CascadingException( "unable to write value as json", exception ); } }
@Override public JsonNode canonical( Object value ) { if( value == null ) return null; Class from = value.getClass(); if( JsonNode.class.isAssignableFrom( from ) ) return (JsonNode) value; if( from == String.class ) return nodeOrParse( (String) value ); if( from == Integer.class || from == Integer.TYPE ) return JsonNodeFactory.instance.numberNode( (Integer) value ); if( from == Long.class || from == Long.TYPE ) return JsonNodeFactory.instance.numberNode( (Long) value ); if( from == Float.class || from == Float.TYPE ) return JsonNodeFactory.instance.numberNode( (Float) value ); if( from == Double.class || from == Double.TYPE ) return JsonNodeFactory.instance.numberNode( (Double) value ); if( from == Boolean.class || from == Boolean.TYPE ) return JsonNodeFactory.instance.booleanNode( (Boolean) value ); if( Collection.class.isAssignableFrom( from ) || Map.class.isAssignableFrom( from ) ) return mapper.valueToTree( value ); return JsonNodeFactory.instance.pojoNode( value ); }
private void testContainerCoercion( String value, JsonNodeType nodeType, Class resultType ) { JsonNode canonical = JSONCoercibleType.TYPE.canonical( value ); assertEquals( nodeType, canonical.getNodeType() ); assertEquals( value.replaceAll( "\\s", "" ), JSONCoercibleType.TYPE.coerce( canonical, resultType ) ); } }
@Test public void pojoCoercions() { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule( new JavaTimeModule() ); JSONCoercibleType type = new JSONCoercibleType( mapper ); Instant instant = Instant.ofEpochSecond( 1525456424, 337000000 ); JsonNode canonical = type.canonical( instant ); String coerce = type.coerce( canonical, String.class ); assertEquals( "1525456424.337000000", coerce ); }
/** * Constructor JSONTextLine creates a new JSONTextLine instance for use with the * {@link cascading.flow.local.LocalFlowConnector}. * * @param mapper of ObjectMapper * @param fields of Fields * @param compressor of type Compressor, see {@link Compressors} * @param charsetName of String */ public JSONTextLine( ObjectMapper mapper, Fields fields, Compressor compressor, String charsetName ) { if( mapper != null ) this.mapper = mapper; if( fields == null ) throw new IllegalArgumentException( "fields may not be null" ); if( !fields.isDefined() ) throw new IllegalArgumentException( "fields argument must declare a single field" ); if( fields.size() != 1 ) throw new IllegalArgumentException( "may only declare a single source/sink field in the fields argument" ); fields = fields.hasTypes() ? fields : fields.applyTypes( new JSONCoercibleType( this.mapper ) ); setSinkFields( fields ); setSourceFields( fields ); setCompressor( compressor ); // throws an exception if not found setCharsetName( charsetName ); }
private void testContainerCoercion( String value, JsonNodeType nodeType, Class resultType ) { JsonNode canonical = JSONCoercibleType.TYPE.canonical( value ); assertEquals( nodeType, canonical.getNodeType() ); assertEquals( value.replaceAll( "\\s", "" ), JSONCoercibleType.TYPE.coerce( canonical, resultType ) ); } }
@Override public boolean equals( Object object ) { if( this == object ) return true; if( !( object instanceof CoercibleType ) ) return false; return getCanonicalType().equals( ( (CoercibleType) object ).getCanonicalType() ); } }
@Test public void listCoercions() { List<Object> list = new LinkedList<>(); list.add( "John Doe" ); list.add( Arrays.asList( "John", "Jane" ) ); JsonNode canonical = JSONCoercibleType.TYPE.canonical( list ); assertEquals( JsonNodeType.ARRAY, canonical.getNodeType() ); assertEquals( list, JSONCoercibleType.TYPE.coerce( canonical, List.class ) ); }
@Test public void listCoercions() { List<Object> list = new LinkedList<>(); list.add( "John Doe" ); list.add( Arrays.asList( "John", "Jane" ) ); JsonNode canonical = JSONCoercibleType.TYPE.canonical( list ); assertEquals( JsonNodeType.ARRAY, canonical.getNodeType() ); assertEquals( list, JSONCoercibleType.TYPE.coerce( canonical, List.class ) ); }
private void testCoercion( Object value, JsonNodeType nodeType, Object resultValue, Class resultType ) { JsonNode canonical = JSONCoercibleType.TYPE.canonical( value ); assertEquals( nodeType, canonical.getNodeType() ); assertEquals( resultValue, JSONCoercibleType.TYPE.coerce( canonical, resultType ) ); }
@Test public void mapCoercions() { Map<String, Object> map = new LinkedHashMap<>(); map.put( "name", "John Doe" ); map.put( "list", Arrays.asList( "John", "Jane" ) ); JsonNode canonical = JSONCoercibleType.TYPE.canonical( map ); assertEquals( JsonNodeType.OBJECT, canonical.getNodeType() ); assertEquals( map, JSONCoercibleType.TYPE.coerce( canonical, Map.class ) ); }
private void testCoercion( Object value, JsonNodeType nodeType, Object resultValue, Class resultType ) { JsonNode canonical = JSONCoercibleType.TYPE.canonical( value ); assertEquals( nodeType, canonical.getNodeType() ); assertEquals( resultValue, JSONCoercibleType.TYPE.coerce( canonical, resultType ) ); }