@Test public void serialize(@Mock Attribute source, @Mock JsonGenerator jgen, @Mock SerializerProvider sp) throws IOException { AttributeSerializer serializer = new AttributeSerializer(); when(source.getValue()).thenReturn(null); serializer.serialize(source, jgen, sp); verify(jgen).writeStartObject(); verify(jgen).writeFieldName("value"); verify(jgen).writeNull(); when(source.getValue()).thenAnswer(ic -> List.of(new GuardedString())); serializer.serialize(source, jgen, sp); verify(jgen).writeObject(any(GuardedString.class)); when(source.getValue()).thenAnswer(ic -> List.of(9000)); serializer.serialize(source, jgen, sp); verify(jgen).writeNumber(anyInt()); when(source.getValue()).thenAnswer(ic -> List.of(9000L)); serializer.serialize(source, jgen, sp); verify(jgen).writeNumber(anyLong()); when(source.getValue()).thenAnswer(ic -> List.of(9000.1)); serializer.serialize(source, jgen, sp); verify(jgen).writeNumber(anyDouble()); when(source.getValue()).thenAnswer(ic -> List.of(Boolean.TRUE)); serializer.serialize(source, jgen, sp); verify(jgen).writeBoolean(anyBoolean()); when(source.getValue()).thenAnswer(ic -> List.of(new byte[] { 9, 0, 0, 0 })); serializer.serialize(source, jgen, sp); verify(jgen).writeString(anyString()); when(source.getValue()).thenAnswer(ic -> List.of("test")); serializer.serialize(source, jgen, sp); verify(jgen).writeString(eq("test")); }
@Override public void serialize(final Attribute source, final JsonGenerator jgen, final SerializerProvider sp) throws IOException { jgen.writeStartObject(); jgen.writeStringField("name", source.getName()); jgen.writeFieldName("value"); if (source.getValue() == null) { jgen.writeNull(); } else { jgen.writeStartArray(); for (Object value : source.getValue()) { if (value == null) { jgen.writeNull(); } else if (value instanceof GuardedString) { jgen.writeObject(value); } else if (value instanceof Integer) { jgen.writeNumber((Integer) value); } else if (value instanceof Long) { jgen.writeNumber((Long) value); } else if (value instanceof Double) { jgen.writeNumber((Double) value); } else if (value instanceof Boolean) { jgen.writeBoolean((Boolean) value); } else if (value instanceof byte[]) { jgen.writeString( BYTE_ARRAY_PREFIX + Base64.getEncoder().encodeToString((byte[]) value) + BYTE_ARRAY_SUFFIX); } else { jgen.writeString(value.toString()); } } jgen.writeEndArray(); } jgen.writeEndObject(); }
AttributeSerializer extends JsonSerializer<Attribute> { @Override public void serialize(final Attribute source, final JsonGenerator jgen, final SerializerProvider sp) throws IOException { jgen.writeStartObject(); jgen.writeStringField("name", source.getName()); jgen.writeFieldName("value"); if (source.getValue() == null) { jgen.writeNull(); } else { jgen.writeStartArray(); for (Object value : source.getValue()) { if (value == null) { jgen.writeNull(); } else if (value instanceof GuardedString) { jgen.writeObject(value); } else if (value instanceof Integer) { jgen.writeNumber((Integer) value); } else if (value instanceof Long) { jgen.writeNumber((Long) value); } else if (value instanceof Double) { jgen.writeNumber((Double) value); } else if (value instanceof Boolean) { jgen.writeBoolean((Boolean) value); } else if (value instanceof byte[]) { jgen.writeString( BYTE_ARRAY_PREFIX + Base64.getEncoder().encodeToString((byte[]) value) + BYTE_ARRAY_SUFFIX); } else { jgen.writeString(value.toString()); } } jgen.writeEndArray(); } jgen.writeEndObject(); } }
AttributeSerializer extends JsonSerializer<Attribute> { @Override public void serialize(final Attribute source, final JsonGenerator jgen, final SerializerProvider sp) throws IOException { jgen.writeStartObject(); jgen.writeStringField("name", source.getName()); jgen.writeFieldName("value"); if (source.getValue() == null) { jgen.writeNull(); } else { jgen.writeStartArray(); for (Object value : source.getValue()) { if (value == null) { jgen.writeNull(); } else if (value instanceof GuardedString) { jgen.writeObject(value); } else if (value instanceof Integer) { jgen.writeNumber((Integer) value); } else if (value instanceof Long) { jgen.writeNumber((Long) value); } else if (value instanceof Double) { jgen.writeNumber((Double) value); } else if (value instanceof Boolean) { jgen.writeBoolean((Boolean) value); } else if (value instanceof byte[]) { jgen.writeString( BYTE_ARRAY_PREFIX + Base64.getEncoder().encodeToString((byte[]) value) + BYTE_ARRAY_SUFFIX); } else { jgen.writeString(value.toString()); } } jgen.writeEndArray(); } jgen.writeEndObject(); }  }
AttributeSerializer extends JsonSerializer<Attribute> { @Override public void serialize(final Attribute source, final JsonGenerator jgen, final SerializerProvider sp) throws IOException { jgen.writeStartObject(); jgen.writeStringField("name", source.getName()); jgen.writeFieldName("value"); if (source.getValue() == null) { jgen.writeNull(); } else { jgen.writeStartArray(); for (Object value : source.getValue()) { if (value == null) { jgen.writeNull(); } else if (value instanceof GuardedString) { jgen.writeObject(value); } else if (value instanceof Integer) { jgen.writeNumber((Integer) value); } else if (value instanceof Long) { jgen.writeNumber((Long) value); } else if (value instanceof Double) { jgen.writeNumber((Double) value); } else if (value instanceof Boolean) { jgen.writeBoolean((Boolean) value); } else if (value instanceof byte[]) { jgen.writeString( BYTE_ARRAY_PREFIX + Base64.getEncoder().encodeToString((byte[]) value) + BYTE_ARRAY_SUFFIX); } else { jgen.writeString(value.toString()); } } jgen.writeEndArray(); } jgen.writeEndObject(); }  @Override void serialize(final Attribute source, final JsonGenerator jgen, final SerializerProvider sp); }
AttributeSerializer extends JsonSerializer<Attribute> { @Override public void serialize(final Attribute source, final JsonGenerator jgen, final SerializerProvider sp) throws IOException { jgen.writeStartObject(); jgen.writeStringField("name", source.getName()); jgen.writeFieldName("value"); if (source.getValue() == null) { jgen.writeNull(); } else { jgen.writeStartArray(); for (Object value : source.getValue()) { if (value == null) { jgen.writeNull(); } else if (value instanceof GuardedString) { jgen.writeObject(value); } else if (value instanceof Integer) { jgen.writeNumber((Integer) value); } else if (value instanceof Long) { jgen.writeNumber((Long) value); } else if (value instanceof Double) { jgen.writeNumber((Double) value); } else if (value instanceof Boolean) { jgen.writeBoolean((Boolean) value); } else if (value instanceof byte[]) { jgen.writeString( BYTE_ARRAY_PREFIX + Base64.getEncoder().encodeToString((byte[]) value) + BYTE_ARRAY_SUFFIX); } else { jgen.writeString(value.toString()); } } jgen.writeEndArray(); } jgen.writeEndObject(); }  @Override void serialize(final Attribute source, final JsonGenerator jgen, final SerializerProvider sp); static final String BYTE_ARRAY_PREFIX; static final String BYTE_ARRAY_SUFFIX; }