如何使用默认键入NON_FINAL使用jackson ArrayList子列表进行序列化?

时间:2015-11-09 14:36:29

标签: java json serialization arraylist jackson

我遇到问题DefaultTyping.NON_FINAL。当我尝试序列化子列表时,我希望它被序列化为

Sub test_user5472539()
Dim Ws1 As Worksheet, _
    Ws2 As Worksheet, _
    LastRow As Long, _
    FindString As String, _
    FirstAddress As String, _
    cF As Range

Set Ws1 = ActiveWorkbook.Sheets("Sheet1")
Set Ws2 = ActiveWorkbook.Sheets("Sheet2")
LastRow = Ws1.Range("A" & Ws1.Rows.Count).End(xlUp).Row

For i = 1 To LastRow
    FindString = Ws1.Range("A" & i)
    If Trim(FindString) <> "" Then
        Ws2.Range("A1").Activate
        With Ws2.Range("A:A")
            'First, define properly the Find method
            Set cF = .Find(What:=FindString, _
                        After:=.Cells(1, 1), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False, _
                        SearchFormat:=False)

            'If there is a result, keep looking with FindNext method
            If Not cF Is Nothing Then
                FirstAddress = cF.Address
                Do
                    cF.EntireRow.Delete
                    Set cF = .FindNext(cF)
                'Look until you find again the first result
                Loop While Not cF Is Nothing And cF.Address <> FirstAddress
            Else
                MsgBox "Not Found"
            End If
        End With
    Else
    End If
Next i

End Sub

["java.util.ArrayList",[0,1]] 

但它被序列化为

[0,1]

这个问题是类java.util.ArrayList $ SubList无法实例化,当我尝试读取没有[collection type;的默认构造函数]时抛出错误class java.util.ArrayList $ SubList

以下示例代码重现错误:

["java.util.ArrayList$SubList",[0,1]]

一个简单的解决方案是将列表包装到一个新列表中,但我尝试使用ObjectMapper的配置以更一般的方式解决这个问题。

这是主要方法的输出:

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonCassandraSerializer {

    public static void main(String[] args) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        List<Integer> list = new ArrayList<Integer>();
        list.add(0);
        list.add(1);
        list.add(2);
        list.add(3);
        String serialized = mapper.writeValueAsString(list.subList(0, 2));
        System.out.println(serialized);
        // in the next line an exception is thrown:
        list = mapper.readValue(serialized, new TypeReference<List<Integer>>() {});
    }
}

0 个答案:

没有答案