Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 2nd, 2012  |  syntax: None  |  size: 0.93 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package fr.pilato.test.jackson;
  2.  
  3. import static org.junit.Assert.*;
  4.  
  5. import java.io.IOException;
  6.  
  7. import org.codehaus.jackson.JsonGenerationException;
  8. import org.codehaus.jackson.map.JsonMappingException;
  9. import org.codehaus.jackson.map.ObjectMapper;
  10. import org.codehaus.jackson.map.SerializationConfig.Feature;
  11. import org.junit.Test;
  12.  
  13. public class SerializeTest {
  14.  
  15.         @Test
  16.         public void test() {
  17.                 MyClass myClass = new MyClass("test1", MyEnum.ENUM1);
  18.                
  19.  
  20.                 ObjectMapper mapper = new ObjectMapper();
  21.                 mapper.configure(Feature.AUTO_DETECT_FIELDS, false);
  22.                 mapper.configure(Feature.AUTO_DETECT_GETTERS, false);
  23.                 mapper.configure(Feature.FAIL_ON_EMPTY_BEANS, false);
  24.                
  25.                 try {
  26.                         String result = mapper.writeValueAsString(myClass);
  27.                         System.out.println(result);
  28.                 } catch (JsonGenerationException e) {
  29.                         e.printStackTrace();
  30.                 } catch (JsonMappingException e) {
  31.                         e.printStackTrace();
  32.                 } catch (IOException e) {
  33.                         e.printStackTrace();
  34.                 }
  35.         }
  36.  
  37. }