0

I have Dto with field of byte array and json string, serialized with JSON.stringify and service, that should parse this string.

@Data
@NoArgsConstructor
@AllArgsConstructor
public class CompanyDto {

    private UUID id;
    private String name;
    private boolean isVerified;
    private String description;
    private byte[] fileData;
    private UUID accountId;
}
    @Override
    public CompanyDto parseCompanyDto(String s) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new Hibernate5Module());
        try {
            return objectMapper.readValue(s, CompanyDto.class);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
            return null;
        }
    }

If fileData in input string is null, this service works well, but it can't deserialize byte array from string, serialized on frontend. It fails with exeption:

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `[B` out of START_OBJECT token
 at [Source: (String)"{"name":"testCompany","description":"test test test","fileData":{"0":255,"1":216,"2":255,"3":224,"4":0,"5":16,"6":74,"7":70,"8":73,"9":70,"10":0,"11":1,"12":1,"13":2,"14":0,"15":56,"16":0,"17":56,"18":0,"19":0,"20":255,"21":219,"22":0,"23":67,"24":0,"25":2,"26":2,"27":2,"28":2,"29":2,"30":1,"31":2,"32":2,"33":2,"34":2,"35":3,"36":2,"37":2,"38":3,"39":3,"40":6,"41":4,"42":3,"43":3,"44":3,"45":3,"46":7,"47":5,"48":5,"49":4,"50":6,"51":8,"52":7,"53":9,"54":8,"55":8,"56":7,"57":8,"58":8,"59":9,"60":"[truncated 25008 chars]; line: 1, column: 65] (through reference chain: edu.netcracker.jobdealer.dto.CompanyDto["fileData"])
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1442)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1216)
    at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1126)
    at com.fasterxml.jackson.databind.deser.std.PrimitiveArrayDeserializers.handleNonArray(PrimitiveArrayDeserializers.java:240)
    at com.fasterxml.jackson.databind.deser.std.PrimitiveArrayDeserializers$ByteDeser.deserialize(PrimitiveArrayDeserializers.java:498)
    at com.fasterxml.jackson.databind.deser.std.PrimitiveArrayDeserializers$ByteDeser.deserialize(PrimitiveArrayDeserializers.java:446)
    at com.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:129)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.vanillaDeserialize(BeanDeserializer.java:288)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:151)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4202)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3205)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3173)
    at service.impl.JsonServiceImpl.parseCompanyDto(JsonServiceImpl.java:51)

I tried to send fileData separately using URlSearchParams.append and it worked well, but it sounds like a dirty hack.

Can you say, what instance of [B means and how to parse it correctly?

4
  • 1
    Could you show example JSON payload you want to deserialise? Commented Dec 2, 2019 at 7:44
  • 2
    [B is the type name for byte[] and is what you want. Why are you serializing as a bunch of quoted int values instead of as Base64? Commented Dec 2, 2019 at 7:44
  • Possible duplicate of how to deserialize / serialize byte array using Jackson and wrapper object, maybe you can try the answer. Commented Dec 2, 2019 at 8:20
  • Michal, example of json in second line of stack trace. It can't print all, cause length of this json is near 17k symbols Commented Dec 2, 2019 at 11:17

1 Answer 1

3

Java can't serialize byte array. Use base64 or you can implement your own serializer/deserializer

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for an answer, i've changed format of receiving data to base64 and now it works well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.