2 changed files with 66 additions and 0 deletions
-
63src/main/java/com/spring/common/jackson/FlexibleLongListDeserializer.java
-
3src/main/java/com/spring/modules/quote/entity/Quote.java
@ -0,0 +1,63 @@ |
|||
package com.spring.common.jackson; |
|||
|
|||
import com.fasterxml.jackson.core.JsonParser; |
|||
import com.fasterxml.jackson.core.JsonToken; |
|||
import com.fasterxml.jackson.databind.DeserializationContext; |
|||
import com.fasterxml.jackson.databind.JsonDeserializer; |
|||
|
|||
import java.io.IOException; |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* 兼容 ids 以 JSON 数组、单个数字或逗号分隔字符串传入。 |
|||
*/ |
|||
public class FlexibleLongListDeserializer extends JsonDeserializer<List<Long>> { |
|||
|
|||
@Override |
|||
public List<Long> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { |
|||
List<Long> result = new ArrayList<>(); |
|||
JsonToken token = p.getCurrentToken(); |
|||
if (token == JsonToken.VALUE_NULL) { |
|||
return null; |
|||
} |
|||
if (token == JsonToken.VALUE_STRING) { |
|||
addAll(result, p.getText()); |
|||
return result; |
|||
} |
|||
if (token == JsonToken.VALUE_NUMBER_INT) { |
|||
result.add(p.getLongValue()); |
|||
return result; |
|||
} |
|||
if (token == JsonToken.START_ARRAY) { |
|||
while (p.nextToken() != JsonToken.END_ARRAY) { |
|||
JsonToken item = p.getCurrentToken(); |
|||
if (item == JsonToken.VALUE_NUMBER_INT) { |
|||
result.add(p.getLongValue()); |
|||
} else if (item == JsonToken.VALUE_STRING) { |
|||
addAll(result, p.getText()); |
|||
} else if (item == JsonToken.VALUE_NULL) { |
|||
continue; |
|||
} |
|||
} |
|||
return result; |
|||
} |
|||
return result; |
|||
} |
|||
|
|||
private void addAll(List<Long> result, String text) { |
|||
if (text == null) { |
|||
return; |
|||
} |
|||
String value = text.trim(); |
|||
if (value.isEmpty()) { |
|||
return; |
|||
} |
|||
for (String part : value.split("[,\\s]+")) { |
|||
if (part == null || part.trim().isEmpty()) { |
|||
continue; |
|||
} |
|||
result.add(Long.valueOf(part.trim())); |
|||
} |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue