@Override
    public Object deserialize(Asn1ObjectInputStream in, Class<? extends Object> type,
                                      Asn1ObjectMapper mapper) {
        final Asn1Entity entity = type.getAnnotation(Asn1Entity.class);
        final Object instance = ObjectUtils.newInstance(type);

        final Map<String, List<Asn1Field>> fields = fieldsMap(mapper.getFields(type));
        while (!in.atEnd()) {
            try (final Asn1ObjectInputStream seq = in.next()) {
                if (seq.tagNo != getNestedTagNo(type)) {
                    throw new Asn1Exception("Expected tag %x, got %x", getNestedTagNo(type), seq.tagNo);
                }

                final String id = readIdentifier(seq);
                final List<Asn1Field> fieldsOfId = fields.remove(id);
                if (fieldsOfId == null) {
                    if (!entity.partial()) throw new Asn1Exception("Found unknown identifier " + id);
                    seq.advanceToEnd();
                    continue;
                }

                final FieldSequence fieldsSeq = new FieldSequence(false, fieldsOfId);
                readFields(mapper, seq, fieldsSeq, instance);
                while (!seq.atEnd()) {
                    try (final Asn1ObjectInputStream obj = seq.next()) {
                        final Asn1Field field = fieldsSeq.get(obj.tagNo);
                        final Object attr = mapper.readValue(obj, field.converter(), field.type());
                        ObjectUtils.setProperty(field.pd, instance, attr);
                    }
                }
                if (!fieldsSeq.done()) {
                    throw new Asn1Exception("At end of data, but still non optional fields");
                }
            }
        }
        return instance;
    }
@Test
    public void shouldDeserializeWithOptional() {
        assertEquals(new Set(1, 2, 3, 4), deserialize(
            new SetOfIdentifiedConverter(), Set.class, new byte[] {
                0x30, 6, 0x06, 1, 44, 0x02, 1, 4, 0x30, 6, 0x06, 1, 83, 0x02, 1, 3,
                0x30,9, 0x06, 1, 84, 0x02, 1, 1, 0x02, 1, 2,
            }
        ));
    }