System.out messes up after Jackson serialization

I am trying to serialize some objects in System.out (for debugging). As soon as i call

final JsonSerializer serializer = new JsonSerializer();
serializer.serialize( System.out, myObj );
System.out.println("done");

it prints json, but "done" never prints. Debugging these lines clearly shows that the 3rd line is running, but the output never appears.

Is this Jackson's mistake, or am I doing something wrong?

EDIT:

public class JsonSerializer
{

private ObjectMapper getConfiguredObjectMapper()
{
  final ObjectMapper mapper = new ObjectMapper();
  mapper.enable( SerializationConfig.Feature.INDENT_OUTPUT );
  mapper.setVisibility( JsonMethod.FIELD, Visibility.ANY );
  mapper.setVisibility( JsonMethod.GETTER, Visibility.NONE );
  mapper.configure( SerializationConfig.Feature.AUTO_DETECT_GETTERS, false );
  mapper.configure( SerializationConfig.Feature.AUTO_DETECT_IS_GETTERS, false );
  mapper.configure( SerializationConfig.Feature.AUTO_DETECT_FIELDS, false );



  final SimpleModule module = new SimpleModule( "ConnectorSerialization", new Version( 0, 1, 0, null ) );
  module.addSerializer( new InputConnectorSerializer() );
  module.addSerializer( new OutputConnectorSerializer() );
  module.addSerializer( new StateSerializer() );
  mapper.registerModule( module );

  return mapper;
  }


public void serialize( final OutputStream outputStream, final Object root )
{


  final ObjectMapper mapper = getConfiguredObjectMapper();


  try
  {
     mapper.writeValue( outputStream, root );

  }
  catch (final JsonGenerationException e)
  {
     // TODO Auto-generated catch block
     e.printStackTrace();
  }
  catch (final JsonMappingException e)
  {
     // TODO Auto-generated catch block
     e.printStackTrace();
  }
  catch (final IOException e)
  {
     // TODO Auto-generated catch block
     e.printStackTrace();
  }

}
}
+4
source share
1 answer

Since all the answers from other users have been deleted, I'm going to answer my question. Thanks to the user who stated that this is a problem when Jackson automatically closes the input stream.

, JsonGenerator.Feature.AUTO_CLOSE_TARGET false:

mapper.configure(JsonGenerator.Feature.AUTO_CLOSE_TARGET, false);
+7

Source: https://habr.com/ru/post/1535305/


All Articles