@Test public void testDataEventSerializer() throws Exception { Operation operation = mock( Operation.class ); when( operation.getId() ).thenReturn( "foo" ); Date date = new Date(); URI uri = new URI( "http: List<Object> objects = new ArrayList<>(); objects.add( 100 ); objects.add( 100.50 ); objects.add( new BigDecimal( "10000000000000000000.50" ) ); objects.add( true ); objects.add( date ); objects.add( "A String" ); objects.add( uri ); List<String> names = new ArrayList<>(); names.add( "some int" ); names.add( "some Double" ); names.add( "some Decimal" ); names.add( "some Boolean" ); names.add( "some Date" ); names.add( "some String" ); names.add( "some Serializable" ); List<Class> classes = Arrays .asList( Integer.class, Double.class, BigDecimal.class, Boolean.class, Date.class, String.class, Object.class ); Row row = new DeserializedRow( names, classes, objects ); List<Row> rowsList = Collections.singletonList( row ); Rows rows = new Rows( rowsList, Rows.TYPE.OUT, Rows.STATE.ACTIVE ); DataEvent<Operation> dataEvent = new DataEvent<>( operation, rows ); DataEventSerializer serializer = new DataEventSerializer(); String serialized = serializer.serialize( dataEvent ); System.out.println( serialized ); DataEvent deserialized = serializer.deserialize( serialized ); assertTrue( serializer.getSupportedClasses().contains( DataEvent.class ) ); assertEquals( dataEvent, deserialized ); }
public DataEventSerializer() { super( DataEvent.class ); SimpleModule module = new SimpleModule(); module.addSerializer( DataEvent.class, new JsonSerializer<DataEvent>() { @Override public void serialize( DataEvent dataEvent, JsonGenerator jsonGenerator, SerializerProvider serializerProvider ) throws IOException, JsonProcessingException { jsonGenerator.writeStartObject(); Rows rows = (Rows) dataEvent.getData(); jsonGenerator.writeStringField( "model-id", dataEvent.getSource().getId() ); jsonGenerator.writeStringField( "type", rows.getType().toString() ); jsonGenerator.writeStringField( "state", rows.getState().toString() ); jsonGenerator.writeArrayFieldStart( "rows" ); for ( Row row : rows ) { jsonGenerator.writeStartObject(); jsonGenerator.writeArrayFieldStart( "names" ); for ( String name : row.getColumnNames() ) { jsonGenerator.writeString( name ); } jsonGenerator.writeEndArray(); jsonGenerator.writeArrayFieldStart( "objects" ); for ( Object obj : row.getObjects() ) { jsonGenerator.writeStartObject(); if ( obj == null ) { jsonGenerator.writeStringField( "type", "Null" ); jsonGenerator.writeEndObject(); continue; } switch ( obj.getClass().getSimpleName() ) { case "String": jsonGenerator.writeStringField( "type", "String" ); jsonGenerator.writeStringField( "obj", obj.toString() ); break; case "Date": jsonGenerator.writeStringField( "type", "Date" ); jsonGenerator.writeStringField( "obj", DATE_TIME_INSTANCE.format( (Date) obj ) ); break; case "Integer": jsonGenerator.writeStringField( "type", "Integer" ); jsonGenerator.writeNumberField( "obj", (Integer) obj ); break; case "Long": jsonGenerator.writeStringField( "type", "Long" ); jsonGenerator.writeNumberField( "obj", (Long) obj ); break; case "Double": jsonGenerator.writeStringField( "type", "Double" ); jsonGenerator.writeNumberField( "obj", (Double) obj ); break; case "BigDecimal": jsonGenerator.writeStringField( "type", "BigDecimal" ); jsonGenerator.writeStringField( "obj", obj.toString() ); break; case "Boolean": jsonGenerator.writeStringField( "type", "Boolean" ); jsonGenerator.writeBooleanField( "obj", (Boolean) obj ); break; case "byte[]": jsonGenerator.writeStringField( "type", "byte[]" ); jsonGenerator.writeStringField( "obj", new String( ( (byte[]) obj ), "UTF-8" ) ); break; default: if ( obj instanceof Serializable ) { jsonGenerator.writeStringField( "type", "Object" ); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream( outputStream ); objectOutputStream.writeObject( obj ); objectOutputStream.close(); outputStream.close(); byte[] bytes = outputStream.toByteArray(); jsonGenerator.writeStringField( "obj", Base64.encodeBase64String( bytes ) ); } } jsonGenerator.writeEndObject(); } jsonGenerator.writeEndArray(); jsonGenerator.writeEndObject(); } jsonGenerator.writeEndArray(); jsonGenerator.writeEndObject(); } } ); module.addDeserializer( DataEvent.class, new StdNodeBasedDeserializer<DataEvent>( DataEvent.class ) { @Override public DataEvent convert( JsonNode jsonNode, DeserializationContext deserializationContext ) throws IOException { Rows.TYPE type = Rows.TYPE.valueOf( jsonNode.get( "type" ).asText() ); Rows.STATE state = Rows.STATE.valueOf( jsonNode.get( "state" ).asText() ); List<Row> rows = new ArrayList<>(); JsonNode json_rows = jsonNode.get( "rows" ); for ( JsonNode row : json_rows ) { List<Class> types = new ArrayList<>(); List<String> names = new ArrayList<>(); for ( JsonNode name : row.get( "names" ) ) { names.add( name.asText() ); } List<Object> objects = new ArrayList<>(); for ( JsonNode obj : row.get( "objects" ) ) { JsonNode t = obj.get( "type" ); JsonNode rawObject = obj.get( "obj" ); Object object = null; String objType = t.asText(); switch ( objType ) { case "Null": types.add( Void.class ); break; case "String": types.add( String.class ); object = rawObject.asText(); break; case "Integer": types.add( Integer.class ); object = rawObject.asInt(); break; case "Long": types.add( Long.class ); object = rawObject.asLong(); break; case "Date": types.add( Date.class ); try { object = DATE_TIME_INSTANCE.parse( rawObject.asText() ); } catch ( ParseException e ) { e.printStackTrace(); } break; case "Double": types.add( Double.class ); object = rawObject.asDouble(); break; case "BigDecimal": types.add( BigDecimal.class ); object = new BigDecimal( rawObject.asText() ); break; case "Boolean": types.add( Boolean.class ); object = rawObject.asBoolean(); break; case "byte[]": types.add( byte[].class ); object = rawObject.asText().getBytes( "UTF-8" ); break; case "Object": try { types.add( Object.class ); object = new ObjectInputStream( new ByteArrayInputStream( Base64.decodeBase64( rawObject.asText() ) ) ).readObject(); } catch ( ClassNotFoundException e ) { e.printStackTrace(); } break; } objects.add( object ); } Row r = new DeserializedRow( names, types, objects ); rows.add( r ); } Rows rowsObj = new Rows( rows, type, state ); return new DataEvent( new RemoteSource( jsonNode.get( "model-id" ).asText() ), rowsObj ); } } ); mapper.registerModule( module ); }
DataEventSerializer extends BaseSerializer<DataEvent> { public DataEventSerializer() { super( DataEvent.class ); SimpleModule module = new SimpleModule(); module.addSerializer( DataEvent.class, new JsonSerializer<DataEvent>() { @Override public void serialize( DataEvent dataEvent, JsonGenerator jsonGenerator, SerializerProvider serializerProvider ) throws IOException, JsonProcessingException { jsonGenerator.writeStartObject(); Rows rows = (Rows) dataEvent.getData(); jsonGenerator.writeStringField( "model-id", dataEvent.getSource().getId() ); jsonGenerator.writeStringField( "type", rows.getType().toString() ); jsonGenerator.writeStringField( "state", rows.getState().toString() ); jsonGenerator.writeArrayFieldStart( "rows" ); for ( Row row : rows ) { jsonGenerator.writeStartObject(); jsonGenerator.writeArrayFieldStart( "names" ); for ( String name : row.getColumnNames() ) { jsonGenerator.writeString( name ); } jsonGenerator.writeEndArray(); jsonGenerator.writeArrayFieldStart( "objects" ); for ( Object obj : row.getObjects() ) { jsonGenerator.writeStartObject(); if ( obj == null ) { jsonGenerator.writeStringField( "type", "Null" ); jsonGenerator.writeEndObject(); continue; } switch ( obj.getClass().getSimpleName() ) { case "String": jsonGenerator.writeStringField( "type", "String" ); jsonGenerator.writeStringField( "obj", obj.toString() ); break; case "Date": jsonGenerator.writeStringField( "type", "Date" ); jsonGenerator.writeStringField( "obj", DATE_TIME_INSTANCE.format( (Date) obj ) ); break; case "Integer": jsonGenerator.writeStringField( "type", "Integer" ); jsonGenerator.writeNumberField( "obj", (Integer) obj ); break; case "Long": jsonGenerator.writeStringField( "type", "Long" ); jsonGenerator.writeNumberField( "obj", (Long) obj ); break; case "Double": jsonGenerator.writeStringField( "type", "Double" ); jsonGenerator.writeNumberField( "obj", (Double) obj ); break; case "BigDecimal": jsonGenerator.writeStringField( "type", "BigDecimal" ); jsonGenerator.writeStringField( "obj", obj.toString() ); break; case "Boolean": jsonGenerator.writeStringField( "type", "Boolean" ); jsonGenerator.writeBooleanField( "obj", (Boolean) obj ); break; case "byte[]": jsonGenerator.writeStringField( "type", "byte[]" ); jsonGenerator.writeStringField( "obj", new String( ( (byte[]) obj ), "UTF-8" ) ); break; default: if ( obj instanceof Serializable ) { jsonGenerator.writeStringField( "type", "Object" ); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream( outputStream ); objectOutputStream.writeObject( obj ); objectOutputStream.close(); outputStream.close(); byte[] bytes = outputStream.toByteArray(); jsonGenerator.writeStringField( "obj", Base64.encodeBase64String( bytes ) ); } } jsonGenerator.writeEndObject(); } jsonGenerator.writeEndArray(); jsonGenerator.writeEndObject(); } jsonGenerator.writeEndArray(); jsonGenerator.writeEndObject(); } } ); module.addDeserializer( DataEvent.class, new StdNodeBasedDeserializer<DataEvent>( DataEvent.class ) { @Override public DataEvent convert( JsonNode jsonNode, DeserializationContext deserializationContext ) throws IOException { Rows.TYPE type = Rows.TYPE.valueOf( jsonNode.get( "type" ).asText() ); Rows.STATE state = Rows.STATE.valueOf( jsonNode.get( "state" ).asText() ); List<Row> rows = new ArrayList<>(); JsonNode json_rows = jsonNode.get( "rows" ); for ( JsonNode row : json_rows ) { List<Class> types = new ArrayList<>(); List<String> names = new ArrayList<>(); for ( JsonNode name : row.get( "names" ) ) { names.add( name.asText() ); } List<Object> objects = new ArrayList<>(); for ( JsonNode obj : row.get( "objects" ) ) { JsonNode t = obj.get( "type" ); JsonNode rawObject = obj.get( "obj" ); Object object = null; String objType = t.asText(); switch ( objType ) { case "Null": types.add( Void.class ); break; case "String": types.add( String.class ); object = rawObject.asText(); break; case "Integer": types.add( Integer.class ); object = rawObject.asInt(); break; case "Long": types.add( Long.class ); object = rawObject.asLong(); break; case "Date": types.add( Date.class ); try { object = DATE_TIME_INSTANCE.parse( rawObject.asText() ); } catch ( ParseException e ) { e.printStackTrace(); } break; case "Double": types.add( Double.class ); object = rawObject.asDouble(); break; case "BigDecimal": types.add( BigDecimal.class ); object = new BigDecimal( rawObject.asText() ); break; case "Boolean": types.add( Boolean.class ); object = rawObject.asBoolean(); break; case "byte[]": types.add( byte[].class ); object = rawObject.asText().getBytes( "UTF-8" ); break; case "Object": try { types.add( Object.class ); object = new ObjectInputStream( new ByteArrayInputStream( Base64.decodeBase64( rawObject.asText() ) ) ).readObject(); } catch ( ClassNotFoundException e ) { e.printStackTrace(); } break; } objects.add( object ); } Row r = new DeserializedRow( names, types, objects ); rows.add( r ); } Rows rowsObj = new Rows( rows, type, state ); return new DataEvent( new RemoteSource( jsonNode.get( "model-id" ).asText() ), rowsObj ); } } ); mapper.registerModule( module ); } }
DataEventSerializer extends BaseSerializer<DataEvent> { public DataEventSerializer() { super( DataEvent.class ); SimpleModule module = new SimpleModule(); module.addSerializer( DataEvent.class, new JsonSerializer<DataEvent>() { @Override public void serialize( DataEvent dataEvent, JsonGenerator jsonGenerator, SerializerProvider serializerProvider ) throws IOException, JsonProcessingException { jsonGenerator.writeStartObject(); Rows rows = (Rows) dataEvent.getData(); jsonGenerator.writeStringField( "model-id", dataEvent.getSource().getId() ); jsonGenerator.writeStringField( "type", rows.getType().toString() ); jsonGenerator.writeStringField( "state", rows.getState().toString() ); jsonGenerator.writeArrayFieldStart( "rows" ); for ( Row row : rows ) { jsonGenerator.writeStartObject(); jsonGenerator.writeArrayFieldStart( "names" ); for ( String name : row.getColumnNames() ) { jsonGenerator.writeString( name ); } jsonGenerator.writeEndArray(); jsonGenerator.writeArrayFieldStart( "objects" ); for ( Object obj : row.getObjects() ) { jsonGenerator.writeStartObject(); if ( obj == null ) { jsonGenerator.writeStringField( "type", "Null" ); jsonGenerator.writeEndObject(); continue; } switch ( obj.getClass().getSimpleName() ) { case "String": jsonGenerator.writeStringField( "type", "String" ); jsonGenerator.writeStringField( "obj", obj.toString() ); break; case "Date": jsonGenerator.writeStringField( "type", "Date" ); jsonGenerator.writeStringField( "obj", DATE_TIME_INSTANCE.format( (Date) obj ) ); break; case "Integer": jsonGenerator.writeStringField( "type", "Integer" ); jsonGenerator.writeNumberField( "obj", (Integer) obj ); break; case "Long": jsonGenerator.writeStringField( "type", "Long" ); jsonGenerator.writeNumberField( "obj", (Long) obj ); break; case "Double": jsonGenerator.writeStringField( "type", "Double" ); jsonGenerator.writeNumberField( "obj", (Double) obj ); break; case "BigDecimal": jsonGenerator.writeStringField( "type", "BigDecimal" ); jsonGenerator.writeStringField( "obj", obj.toString() ); break; case "Boolean": jsonGenerator.writeStringField( "type", "Boolean" ); jsonGenerator.writeBooleanField( "obj", (Boolean) obj ); break; case "byte[]": jsonGenerator.writeStringField( "type", "byte[]" ); jsonGenerator.writeStringField( "obj", new String( ( (byte[]) obj ), "UTF-8" ) ); break; default: if ( obj instanceof Serializable ) { jsonGenerator.writeStringField( "type", "Object" ); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream( outputStream ); objectOutputStream.writeObject( obj ); objectOutputStream.close(); outputStream.close(); byte[] bytes = outputStream.toByteArray(); jsonGenerator.writeStringField( "obj", Base64.encodeBase64String( bytes ) ); } } jsonGenerator.writeEndObject(); } jsonGenerator.writeEndArray(); jsonGenerator.writeEndObject(); } jsonGenerator.writeEndArray(); jsonGenerator.writeEndObject(); } } ); module.addDeserializer( DataEvent.class, new StdNodeBasedDeserializer<DataEvent>( DataEvent.class ) { @Override public DataEvent convert( JsonNode jsonNode, DeserializationContext deserializationContext ) throws IOException { Rows.TYPE type = Rows.TYPE.valueOf( jsonNode.get( "type" ).asText() ); Rows.STATE state = Rows.STATE.valueOf( jsonNode.get( "state" ).asText() ); List<Row> rows = new ArrayList<>(); JsonNode json_rows = jsonNode.get( "rows" ); for ( JsonNode row : json_rows ) { List<Class> types = new ArrayList<>(); List<String> names = new ArrayList<>(); for ( JsonNode name : row.get( "names" ) ) { names.add( name.asText() ); } List<Object> objects = new ArrayList<>(); for ( JsonNode obj : row.get( "objects" ) ) { JsonNode t = obj.get( "type" ); JsonNode rawObject = obj.get( "obj" ); Object object = null; String objType = t.asText(); switch ( objType ) { case "Null": types.add( Void.class ); break; case "String": types.add( String.class ); object = rawObject.asText(); break; case "Integer": types.add( Integer.class ); object = rawObject.asInt(); break; case "Long": types.add( Long.class ); object = rawObject.asLong(); break; case "Date": types.add( Date.class ); try { object = DATE_TIME_INSTANCE.parse( rawObject.asText() ); } catch ( ParseException e ) { e.printStackTrace(); } break; case "Double": types.add( Double.class ); object = rawObject.asDouble(); break; case "BigDecimal": types.add( BigDecimal.class ); object = new BigDecimal( rawObject.asText() ); break; case "Boolean": types.add( Boolean.class ); object = rawObject.asBoolean(); break; case "byte[]": types.add( byte[].class ); object = rawObject.asText().getBytes( "UTF-8" ); break; case "Object": try { types.add( Object.class ); object = new ObjectInputStream( new ByteArrayInputStream( Base64.decodeBase64( rawObject.asText() ) ) ).readObject(); } catch ( ClassNotFoundException e ) { e.printStackTrace(); } break; } objects.add( object ); } Row r = new DeserializedRow( names, types, objects ); rows.add( r ); } Rows rowsObj = new Rows( rows, type, state ); return new DataEvent( new RemoteSource( jsonNode.get( "model-id" ).asText() ), rowsObj ); } } ); mapper.registerModule( module ); } DataEventSerializer(); }
DataEventSerializer extends BaseSerializer<DataEvent> { public DataEventSerializer() { super( DataEvent.class ); SimpleModule module = new SimpleModule(); module.addSerializer( DataEvent.class, new JsonSerializer<DataEvent>() { @Override public void serialize( DataEvent dataEvent, JsonGenerator jsonGenerator, SerializerProvider serializerProvider ) throws IOException, JsonProcessingException { jsonGenerator.writeStartObject(); Rows rows = (Rows) dataEvent.getData(); jsonGenerator.writeStringField( "model-id", dataEvent.getSource().getId() ); jsonGenerator.writeStringField( "type", rows.getType().toString() ); jsonGenerator.writeStringField( "state", rows.getState().toString() ); jsonGenerator.writeArrayFieldStart( "rows" ); for ( Row row : rows ) { jsonGenerator.writeStartObject(); jsonGenerator.writeArrayFieldStart( "names" ); for ( String name : row.getColumnNames() ) { jsonGenerator.writeString( name ); } jsonGenerator.writeEndArray(); jsonGenerator.writeArrayFieldStart( "objects" ); for ( Object obj : row.getObjects() ) { jsonGenerator.writeStartObject(); if ( obj == null ) { jsonGenerator.writeStringField( "type", "Null" ); jsonGenerator.writeEndObject(); continue; } switch ( obj.getClass().getSimpleName() ) { case "String": jsonGenerator.writeStringField( "type", "String" ); jsonGenerator.writeStringField( "obj", obj.toString() ); break; case "Date": jsonGenerator.writeStringField( "type", "Date" ); jsonGenerator.writeStringField( "obj", DATE_TIME_INSTANCE.format( (Date) obj ) ); break; case "Integer": jsonGenerator.writeStringField( "type", "Integer" ); jsonGenerator.writeNumberField( "obj", (Integer) obj ); break; case "Long": jsonGenerator.writeStringField( "type", "Long" ); jsonGenerator.writeNumberField( "obj", (Long) obj ); break; case "Double": jsonGenerator.writeStringField( "type", "Double" ); jsonGenerator.writeNumberField( "obj", (Double) obj ); break; case "BigDecimal": jsonGenerator.writeStringField( "type", "BigDecimal" ); jsonGenerator.writeStringField( "obj", obj.toString() ); break; case "Boolean": jsonGenerator.writeStringField( "type", "Boolean" ); jsonGenerator.writeBooleanField( "obj", (Boolean) obj ); break; case "byte[]": jsonGenerator.writeStringField( "type", "byte[]" ); jsonGenerator.writeStringField( "obj", new String( ( (byte[]) obj ), "UTF-8" ) ); break; default: if ( obj instanceof Serializable ) { jsonGenerator.writeStringField( "type", "Object" ); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream( outputStream ); objectOutputStream.writeObject( obj ); objectOutputStream.close(); outputStream.close(); byte[] bytes = outputStream.toByteArray(); jsonGenerator.writeStringField( "obj", Base64.encodeBase64String( bytes ) ); } } jsonGenerator.writeEndObject(); } jsonGenerator.writeEndArray(); jsonGenerator.writeEndObject(); } jsonGenerator.writeEndArray(); jsonGenerator.writeEndObject(); } } ); module.addDeserializer( DataEvent.class, new StdNodeBasedDeserializer<DataEvent>( DataEvent.class ) { @Override public DataEvent convert( JsonNode jsonNode, DeserializationContext deserializationContext ) throws IOException { Rows.TYPE type = Rows.TYPE.valueOf( jsonNode.get( "type" ).asText() ); Rows.STATE state = Rows.STATE.valueOf( jsonNode.get( "state" ).asText() ); List<Row> rows = new ArrayList<>(); JsonNode json_rows = jsonNode.get( "rows" ); for ( JsonNode row : json_rows ) { List<Class> types = new ArrayList<>(); List<String> names = new ArrayList<>(); for ( JsonNode name : row.get( "names" ) ) { names.add( name.asText() ); } List<Object> objects = new ArrayList<>(); for ( JsonNode obj : row.get( "objects" ) ) { JsonNode t = obj.get( "type" ); JsonNode rawObject = obj.get( "obj" ); Object object = null; String objType = t.asText(); switch ( objType ) { case "Null": types.add( Void.class ); break; case "String": types.add( String.class ); object = rawObject.asText(); break; case "Integer": types.add( Integer.class ); object = rawObject.asInt(); break; case "Long": types.add( Long.class ); object = rawObject.asLong(); break; case "Date": types.add( Date.class ); try { object = DATE_TIME_INSTANCE.parse( rawObject.asText() ); } catch ( ParseException e ) { e.printStackTrace(); } break; case "Double": types.add( Double.class ); object = rawObject.asDouble(); break; case "BigDecimal": types.add( BigDecimal.class ); object = new BigDecimal( rawObject.asText() ); break; case "Boolean": types.add( Boolean.class ); object = rawObject.asBoolean(); break; case "byte[]": types.add( byte[].class ); object = rawObject.asText().getBytes( "UTF-8" ); break; case "Object": try { types.add( Object.class ); object = new ObjectInputStream( new ByteArrayInputStream( Base64.decodeBase64( rawObject.asText() ) ) ).readObject(); } catch ( ClassNotFoundException e ) { e.printStackTrace(); } break; } objects.add( object ); } Row r = new DeserializedRow( names, types, objects ); rows.add( r ); } Rows rowsObj = new Rows( rows, type, state ); return new DataEvent( new RemoteSource( jsonNode.get( "model-id" ).asText() ), rowsObj ); } } ); mapper.registerModule( module ); } DataEventSerializer();  }
DataEventSerializer extends BaseSerializer<DataEvent> { public DataEventSerializer() { super( DataEvent.class ); SimpleModule module = new SimpleModule(); module.addSerializer( DataEvent.class, new JsonSerializer<DataEvent>() { @Override public void serialize( DataEvent dataEvent, JsonGenerator jsonGenerator, SerializerProvider serializerProvider ) throws IOException, JsonProcessingException { jsonGenerator.writeStartObject(); Rows rows = (Rows) dataEvent.getData(); jsonGenerator.writeStringField( "model-id", dataEvent.getSource().getId() ); jsonGenerator.writeStringField( "type", rows.getType().toString() ); jsonGenerator.writeStringField( "state", rows.getState().toString() ); jsonGenerator.writeArrayFieldStart( "rows" ); for ( Row row : rows ) { jsonGenerator.writeStartObject(); jsonGenerator.writeArrayFieldStart( "names" ); for ( String name : row.getColumnNames() ) { jsonGenerator.writeString( name ); } jsonGenerator.writeEndArray(); jsonGenerator.writeArrayFieldStart( "objects" ); for ( Object obj : row.getObjects() ) { jsonGenerator.writeStartObject(); if ( obj == null ) { jsonGenerator.writeStringField( "type", "Null" ); jsonGenerator.writeEndObject(); continue; } switch ( obj.getClass().getSimpleName() ) { case "String": jsonGenerator.writeStringField( "type", "String" ); jsonGenerator.writeStringField( "obj", obj.toString() ); break; case "Date": jsonGenerator.writeStringField( "type", "Date" ); jsonGenerator.writeStringField( "obj", DATE_TIME_INSTANCE.format( (Date) obj ) ); break; case "Integer": jsonGenerator.writeStringField( "type", "Integer" ); jsonGenerator.writeNumberField( "obj", (Integer) obj ); break; case "Long": jsonGenerator.writeStringField( "type", "Long" ); jsonGenerator.writeNumberField( "obj", (Long) obj ); break; case "Double": jsonGenerator.writeStringField( "type", "Double" ); jsonGenerator.writeNumberField( "obj", (Double) obj ); break; case "BigDecimal": jsonGenerator.writeStringField( "type", "BigDecimal" ); jsonGenerator.writeStringField( "obj", obj.toString() ); break; case "Boolean": jsonGenerator.writeStringField( "type", "Boolean" ); jsonGenerator.writeBooleanField( "obj", (Boolean) obj ); break; case "byte[]": jsonGenerator.writeStringField( "type", "byte[]" ); jsonGenerator.writeStringField( "obj", new String( ( (byte[]) obj ), "UTF-8" ) ); break; default: if ( obj instanceof Serializable ) { jsonGenerator.writeStringField( "type", "Object" ); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream( outputStream ); objectOutputStream.writeObject( obj ); objectOutputStream.close(); outputStream.close(); byte[] bytes = outputStream.toByteArray(); jsonGenerator.writeStringField( "obj", Base64.encodeBase64String( bytes ) ); } } jsonGenerator.writeEndObject(); } jsonGenerator.writeEndArray(); jsonGenerator.writeEndObject(); } jsonGenerator.writeEndArray(); jsonGenerator.writeEndObject(); } } ); module.addDeserializer( DataEvent.class, new StdNodeBasedDeserializer<DataEvent>( DataEvent.class ) { @Override public DataEvent convert( JsonNode jsonNode, DeserializationContext deserializationContext ) throws IOException { Rows.TYPE type = Rows.TYPE.valueOf( jsonNode.get( "type" ).asText() ); Rows.STATE state = Rows.STATE.valueOf( jsonNode.get( "state" ).asText() ); List<Row> rows = new ArrayList<>(); JsonNode json_rows = jsonNode.get( "rows" ); for ( JsonNode row : json_rows ) { List<Class> types = new ArrayList<>(); List<String> names = new ArrayList<>(); for ( JsonNode name : row.get( "names" ) ) { names.add( name.asText() ); } List<Object> objects = new ArrayList<>(); for ( JsonNode obj : row.get( "objects" ) ) { JsonNode t = obj.get( "type" ); JsonNode rawObject = obj.get( "obj" ); Object object = null; String objType = t.asText(); switch ( objType ) { case "Null": types.add( Void.class ); break; case "String": types.add( String.class ); object = rawObject.asText(); break; case "Integer": types.add( Integer.class ); object = rawObject.asInt(); break; case "Long": types.add( Long.class ); object = rawObject.asLong(); break; case "Date": types.add( Date.class ); try { object = DATE_TIME_INSTANCE.parse( rawObject.asText() ); } catch ( ParseException e ) { e.printStackTrace(); } break; case "Double": types.add( Double.class ); object = rawObject.asDouble(); break; case "BigDecimal": types.add( BigDecimal.class ); object = new BigDecimal( rawObject.asText() ); break; case "Boolean": types.add( Boolean.class ); object = rawObject.asBoolean(); break; case "byte[]": types.add( byte[].class ); object = rawObject.asText().getBytes( "UTF-8" ); break; case "Object": try { types.add( Object.class ); object = new ObjectInputStream( new ByteArrayInputStream( Base64.decodeBase64( rawObject.asText() ) ) ).readObject(); } catch ( ClassNotFoundException e ) { e.printStackTrace(); } break; } objects.add( object ); } Row r = new DeserializedRow( names, types, objects ); rows.add( r ); } Rows rowsObj = new Rows( rows, type, state ); return new DataEvent( new RemoteSource( jsonNode.get( "model-id" ).asText() ), rowsObj ); } } ); mapper.registerModule( module ); } DataEventSerializer();  static final DateFormat DATE_TIME_INSTANCE; }