mrniko/netty-socketio

Jackson Cannot Deserialize Object

lukaslist opened this issue · 1 comments

Error:
[nioEventLoopGroup-3-7] ERROR com.corundumstudio.socketio.JsonSupportWrapper - Can't read value: ["create_expense","{"id":0,"userId":1,"name":"test","amount":123123.0,"type":"EXPENSE","description":"123","date":"2024-04-10"}"] for type: class com.corundumstudio.socketio.protocol.Event
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of dev.lukaslist.models.ExpenseDTO (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"id":0,"userId":1,"name":"test","amount":123123.0,"type":"EXPENSE","description":"123","date":"2024-04-10"}')
at [Source: REDACTED (StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION disabled); line: 1, column: 19]

Class:

@Data
@NoArgsConstructor
public class ExpenseDTO {
    private long id;
    private long userId;
    private String name;
    private double amount;
    private String type;
    private String description;
    private LocalDate date;

    @JsonCreator
    public ExpenseDTO(@JsonProperty("id") long id,
                      @JsonProperty("userId") long userId,
                      @JsonProperty("name") String name,
                      @JsonProperty("amount") double amount,
                      @JsonProperty("type") String type,
                      @JsonProperty("description") String description,
                      @JsonProperty("date") LocalDate date) {
        this.id = id;
        this.userId = userId;
        this.name = name;
        this.amount = amount;
        this.type = type;
        this.description = description;
        this.date = date;
    }

    public ExpenseDTO(Expense expense) {
        this.id = expense.getId();
        this.userId = expense.getUser().getId();
        this.name = expense.getName();
        this.amount = expense.getAmount();
        this.type = expense.getType().toString();
        this.description = expense.getDescription();
        this.date = expense.getDate();
    }

    public static List<ExpenseDTO> turnExpenseListToExpenseDTOList(List<Expense> expenses) {
        List<ExpenseDTO> expenseDTOs = new ArrayList<>();
        for(Expense expense : expenses) {
            expenseDTOs.add(new ExpenseDTO(expense));
        }
        return expenseDTOs;
    }
}

Seems like this error appeared because sender sent stringified object, so the actual value should be replaced:
"{"id":0,"userId":1,"name":"test","amount":123123.0,"type":"EXPENSE","description":"123","date":"2024-04-10"}" ->
{"id":0,"userId":1,"name":"test","amount":123123.0,"type":"EXPENSE","description":"123","date":"2024-04-10"}