Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. final JsonSerializer serializer = new JsonSerializer();
  2. serializer.serialize( System.out, myObj );
  3. System.out.println("done");
  4.  
  5. public class JsonSerializer
  6. {
  7.  
  8. private ObjectMapper getConfiguredObjectMapper()
  9. {
  10. final ObjectMapper mapper = new ObjectMapper();
  11. mapper.enable( SerializationConfig.Feature.INDENT_OUTPUT );
  12. mapper.setVisibility( JsonMethod.FIELD, Visibility.ANY );
  13. mapper.setVisibility( JsonMethod.GETTER, Visibility.NONE );
  14. mapper.configure( SerializationConfig.Feature.AUTO_DETECT_GETTERS, false );
  15. mapper.configure( SerializationConfig.Feature.AUTO_DETECT_IS_GETTERS, false );
  16. mapper.configure( SerializationConfig.Feature.AUTO_DETECT_FIELDS, false );
  17.  
  18.  
  19.  
  20. final SimpleModule module = new SimpleModule( "ConnectorSerialization", new Version( 0, 1, 0, null ) );
  21. module.addSerializer( new InputConnectorSerializer() );
  22. module.addSerializer( new OutputConnectorSerializer() );
  23. module.addSerializer( new StateSerializer() );
  24. mapper.registerModule( module );
  25.  
  26. return mapper;
  27. }
  28.  
  29.  
  30. public void serialize( final OutputStream outputStream, final Object root )
  31. {
  32.  
  33.  
  34. final ObjectMapper mapper = getConfiguredObjectMapper();
  35.  
  36.  
  37. try
  38. {
  39. mapper.writeValue( outputStream, root );
  40.  
  41. }
  42. catch (final JsonGenerationException e)
  43. {
  44. // TODO Auto-generated catch block
  45. e.printStackTrace();
  46. }
  47. catch (final JsonMappingException e)
  48. {
  49. // TODO Auto-generated catch block
  50. e.printStackTrace();
  51. }
  52. catch (final IOException e)
  53. {
  54. // TODO Auto-generated catch block
  55. e.printStackTrace();
  56. }
  57.  
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement