@Test public void serialize() throws IOException { StringWriter outputWriter = new StringWriter(); JsonGenerator jsonGenerator = new JsonFactory().createGenerator(outputWriter); jsonGenerator.useDefaultPrettyPrinter(); PollOption pollOption = mock(PollOption.class); when(pollOption.getID()).thenReturn(77); when(pollOption.getPosition()) .thenReturn(new Position(13.37, 42.00)); Calendar timeStart = new GregorianCalendar(); Calendar timeEnd = new GregorianCalendar(); timeStart.set(2018,Calendar.FEBRUARY,1,0,0,0); timeEnd.set(2018,Calendar.MARCH,4,5,6,7); when(pollOption.getTimeStart()) .thenReturn(timeStart.getTime()); when(pollOption.getTimeEnd()) .thenReturn(timeEnd.getTime()); Map<Integer, Account> voters = new HashMap<>(); doReturn(voters).when(pollOption).getVoters(); Account voter = mock(Account.class); when(voter.getID()).thenReturn(42); voters.put(42, voter); PollOptionCompleteSerializer pollOptionCompleteSerializer = new PollOptionCompleteSerializer(); pollOptionCompleteSerializer.serialize(pollOption, jsonGenerator, null); jsonGenerator.close(); System.out.println(outputWriter.toString()); }
@Override public void serialize(PollOption pollOption, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); Position position = pollOption.getPosition(); gen.writeStringField("type", "polloption"); gen.writeNumberField("id", pollOption.getID()); gen.writeNumberField("latitude", position.getLatitude()); gen.writeNumberField("longitude", position.getLatitude()); gen.writeNumberField("time-start", pollOption.getTimeStart().getTime()); gen.writeNumberField("time-end", pollOption.getTimeEnd().getTime()); gen.writeArrayFieldStart("supporters"); for (int voterID : pollOption.getVoters().keySet()) gen.writeNumber(voterID); gen.writeEndArray(); gen.writeEndObject(); }
PollOptionCompleteSerializer extends JsonSerializer<PollOption> { @Override public void serialize(PollOption pollOption, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); Position position = pollOption.getPosition(); gen.writeStringField("type", "polloption"); gen.writeNumberField("id", pollOption.getID()); gen.writeNumberField("latitude", position.getLatitude()); gen.writeNumberField("longitude", position.getLatitude()); gen.writeNumberField("time-start", pollOption.getTimeStart().getTime()); gen.writeNumberField("time-end", pollOption.getTimeEnd().getTime()); gen.writeArrayFieldStart("supporters"); for (int voterID : pollOption.getVoters().keySet()) gen.writeNumber(voterID); gen.writeEndArray(); gen.writeEndObject(); } }
PollOptionCompleteSerializer extends JsonSerializer<PollOption> { @Override public void serialize(PollOption pollOption, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); Position position = pollOption.getPosition(); gen.writeStringField("type", "polloption"); gen.writeNumberField("id", pollOption.getID()); gen.writeNumberField("latitude", position.getLatitude()); gen.writeNumberField("longitude", position.getLatitude()); gen.writeNumberField("time-start", pollOption.getTimeStart().getTime()); gen.writeNumberField("time-end", pollOption.getTimeEnd().getTime()); gen.writeArrayFieldStart("supporters"); for (int voterID : pollOption.getVoters().keySet()) gen.writeNumber(voterID); gen.writeEndArray(); gen.writeEndObject(); }  }
PollOptionCompleteSerializer extends JsonSerializer<PollOption> { @Override public void serialize(PollOption pollOption, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); Position position = pollOption.getPosition(); gen.writeStringField("type", "polloption"); gen.writeNumberField("id", pollOption.getID()); gen.writeNumberField("latitude", position.getLatitude()); gen.writeNumberField("longitude", position.getLatitude()); gen.writeNumberField("time-start", pollOption.getTimeStart().getTime()); gen.writeNumberField("time-end", pollOption.getTimeEnd().getTime()); gen.writeArrayFieldStart("supporters"); for (int voterID : pollOption.getVoters().keySet()) gen.writeNumber(voterID); gen.writeEndArray(); gen.writeEndObject(); }  @Override void serialize(PollOption pollOption, JsonGenerator gen,
                          SerializerProvider serializers); }
PollOptionCompleteSerializer extends JsonSerializer<PollOption> { @Override public void serialize(PollOption pollOption, JsonGenerator gen, SerializerProvider serializers) throws IOException { gen.writeStartObject(); Position position = pollOption.getPosition(); gen.writeStringField("type", "polloption"); gen.writeNumberField("id", pollOption.getID()); gen.writeNumberField("latitude", position.getLatitude()); gen.writeNumberField("longitude", position.getLatitude()); gen.writeNumberField("time-start", pollOption.getTimeStart().getTime()); gen.writeNumberField("time-end", pollOption.getTimeEnd().getTime()); gen.writeArrayFieldStart("supporters"); for (int voterID : pollOption.getVoters().keySet()) gen.writeNumber(voterID); gen.writeEndArray(); gen.writeEndObject(); }  @Override void serialize(PollOption pollOption, JsonGenerator gen,
                          SerializerProvider serializers);  }