@Test public void serialize() throws IOException { StringWriter outputWriter = new StringWriter(); JsonGenerator jsonGenerator = new JsonFactory().createGenerator(outputWriter); jsonGenerator.useDefaultPrettyPrinter(); Poll poll = mock(Poll.class); when(poll.getID()).thenReturn(4); when(poll.getQuestion()).thenReturn("the-question"); when(poll.isMultiChoice()).thenReturn(true); Calendar timeVoteClose = new GregorianCalendar(); timeVoteClose.set(2018,Calendar.JANUARY,1,13,33,37); when(poll.getTimeVoteClose()) .thenReturn(timeVoteClose.getTime()); Account creator = mock(Account.class); when(poll.getCreator()).thenReturn(creator); when(creator.getID()).thenReturn(1337); Map<Integer, PollOption> pollOptions = new HashMap<>(); pollOptions.put(0, null); pollOptions.put(1, null); pollOptions.put(2, null); doReturn(pollOptions).when(poll).getPollOptions(); PollCompleteSerializer pollCompleteSerializer = new PollCompleteSerializer(); pollCompleteSerializer.serialize(poll, jsonGenerator,null); jsonGenerator.close(); System.out.println(outputWriter.toString()); }
@Override public void serialize(Poll poll, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); gen.writeStringField("type", "poll"); gen.writeNumberField("id", poll.getID()); gen.writeNumberField("creator", poll.getCreator().getID()); gen.writeStringField("question", poll.getQuestion()); gen.writeBooleanField("multi-choice", poll.isMultiChoice()); gen.writeNumberField("time-close", poll.getTimeVoteClose().getTime()); SerializerUtil.writeIDArray(poll.getPollOptions().keySet(), "options", gen); gen.writeEndObject(); }
PollCompleteSerializer extends JsonSerializer<Poll> { @Override public void serialize(Poll poll, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); gen.writeStringField("type", "poll"); gen.writeNumberField("id", poll.getID()); gen.writeNumberField("creator", poll.getCreator().getID()); gen.writeStringField("question", poll.getQuestion()); gen.writeBooleanField("multi-choice", poll.isMultiChoice()); gen.writeNumberField("time-close", poll.getTimeVoteClose().getTime()); SerializerUtil.writeIDArray(poll.getPollOptions().keySet(), "options", gen); gen.writeEndObject(); } }
PollCompleteSerializer extends JsonSerializer<Poll> { @Override public void serialize(Poll poll, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); gen.writeStringField("type", "poll"); gen.writeNumberField("id", poll.getID()); gen.writeNumberField("creator", poll.getCreator().getID()); gen.writeStringField("question", poll.getQuestion()); gen.writeBooleanField("multi-choice", poll.isMultiChoice()); gen.writeNumberField("time-close", poll.getTimeVoteClose().getTime()); SerializerUtil.writeIDArray(poll.getPollOptions().keySet(), "options", gen); gen.writeEndObject(); }  }
PollCompleteSerializer extends JsonSerializer<Poll> { @Override public void serialize(Poll poll, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); gen.writeStringField("type", "poll"); gen.writeNumberField("id", poll.getID()); gen.writeNumberField("creator", poll.getCreator().getID()); gen.writeStringField("question", poll.getQuestion()); gen.writeBooleanField("multi-choice", poll.isMultiChoice()); gen.writeNumberField("time-close", poll.getTimeVoteClose().getTime()); SerializerUtil.writeIDArray(poll.getPollOptions().keySet(), "options", gen); gen.writeEndObject(); }  @Override void serialize(Poll poll, JsonGenerator gen, SerializerProvider
            serializers); }
PollCompleteSerializer extends JsonSerializer<Poll> { @Override public void serialize(Poll poll, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); gen.writeStringField("type", "poll"); gen.writeNumberField("id", poll.getID()); gen.writeNumberField("creator", poll.getCreator().getID()); gen.writeStringField("question", poll.getQuestion()); gen.writeBooleanField("multi-choice", poll.isMultiChoice()); gen.writeNumberField("time-close", poll.getTimeVoteClose().getTime()); SerializerUtil.writeIDArray(poll.getPollOptions().keySet(), "options", gen); gen.writeEndObject(); }  @Override void serialize(Poll poll, JsonGenerator gen, SerializerProvider
            serializers);  }