Skip to content

Commit c0cba51

Browse files
committed
Add explicit AtomicLongDeserializer as well
1 parent 70238fa commit c0cba51

File tree

5 files changed

+94
-49
lines changed

5 files changed

+94
-49
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/std/AtomicIntegerDeserializer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public AtomicInteger deserialize(JsonParser p, DeserializationContext ctxt) thro
2828
}
2929

3030
@Override
31-
public LogicalType logicalType() { return LogicalType.Boolean; }
31+
public LogicalType logicalType() { return LogicalType.Integer; }
3232

3333
@Override // @since 2.12
3434
public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.fasterxml.jackson.databind.deser.std;
2+
3+
import java.io.IOException;
4+
import java.util.concurrent.atomic.AtomicLong;
5+
6+
import com.fasterxml.jackson.core.JsonParser;
7+
8+
import com.fasterxml.jackson.databind.DeserializationContext;
9+
import com.fasterxml.jackson.databind.JsonMappingException;
10+
import com.fasterxml.jackson.databind.type.LogicalType;
11+
12+
// @since 2.12
13+
public class AtomicLongDeserializer extends StdScalarDeserializer<AtomicLong>
14+
{
15+
private static final long serialVersionUID = 1L;
16+
17+
public AtomicLongDeserializer() { super(AtomicLong.class); }
18+
19+
@Override
20+
public AtomicLong deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
21+
if (p.isExpectedNumberIntToken()) {
22+
return new AtomicLong(p.getLongValue());
23+
}
24+
// 12-Jun-2020, tatu: May look convoluted, but need to work correctly with
25+
// CoercionConfig
26+
Long L = _parseLong(p, ctxt, AtomicLong.class);
27+
return (L == null) ? null : new AtomicLong(L.intValue());
28+
}
29+
30+
@Override
31+
public LogicalType logicalType() { return LogicalType.Integer; }
32+
33+
@Override // @since 2.12
34+
public Object getEmptyValue(DeserializationContext ctxt) throws JsonMappingException {
35+
return new AtomicLong();
36+
}
37+
}

src/main/java/com/fasterxml/jackson/databind/deser/std/JdkDeserializers.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.*;
55
import java.util.concurrent.atomic.AtomicBoolean;
66
import java.util.concurrent.atomic.AtomicInteger;
7+
import java.util.concurrent.atomic.AtomicLong;
78

89
import com.fasterxml.jackson.databind.*;
910

@@ -20,6 +21,7 @@ public class JdkDeserializers
2021
UUID.class,
2122
AtomicBoolean.class,
2223
AtomicInteger.class,
24+
AtomicLong.class,
2325
StackTraceElement.class,
2426
ByteBuffer.class,
2527
Void.class
@@ -42,12 +44,14 @@ public static JsonDeserializer<?> find(Class<?> rawType, String clsName)
4244
return new StackTraceElementDeserializer();
4345
}
4446
if (rawType == AtomicBoolean.class) {
45-
// (note: AtomicInteger/Long work due to single-arg constructor. For now?
4647
return new AtomicBooleanDeserializer();
4748
}
4849
if (rawType == AtomicInteger.class) {
4950
return new AtomicIntegerDeserializer();
5051
}
52+
if (rawType == AtomicLong.class) {
53+
return new AtomicLongDeserializer();
54+
}
5155
if (rawType == ByteBuffer.class) {
5256
return new ByteBufferDeserializer();
5357
}

src/main/java/com/fasterxml/jackson/databind/deser/std/NumberDeserializers.java

+1-47
Original file line numberDiff line numberDiff line change
@@ -570,53 +570,7 @@ public Long deserialize(JsonParser p, DeserializationContext ctxt) throws IOExce
570570
if (_primitive) {
571571
return _parseLongPrimitive(p, ctxt);
572572
}
573-
return _parseLong(p, ctxt);
574-
}
575-
576-
protected final Long _parseLong(JsonParser p, DeserializationContext ctxt)
577-
throws IOException
578-
{
579-
String text;
580-
switch (p.currentTokenId()) {
581-
case JsonTokenId.ID_STRING:
582-
text = p.getText();
583-
break;
584-
case JsonTokenId.ID_NUMBER_FLOAT:
585-
final CoercionAction act = _checkFloatToIntCoercion(p, ctxt, _valueClass);
586-
if (act == CoercionAction.AsNull) {
587-
return (Long) getNullValue(ctxt);
588-
}
589-
if (act == CoercionAction.AsEmpty) {
590-
return (Long) getEmptyValue(ctxt);
591-
}
592-
return p.getValueAsLong();
593-
case JsonTokenId.ID_NULL: // null fine for non-primitive
594-
return (Long) getNullValue(ctxt);
595-
case JsonTokenId.ID_NUMBER_INT:
596-
return p.getLongValue();
597-
// 29-Jun-2020, tatu: New! "Scalar from Object" (mostly for XML)
598-
case JsonTokenId.ID_START_OBJECT:
599-
text = ctxt.extractScalarFromObject(p, this, _valueClass);
600-
break;
601-
case JsonTokenId.ID_START_ARRAY:
602-
return (Long) _deserializeFromArray(p, ctxt);
603-
default:
604-
return (Long) ctxt.handleUnexpectedToken(getValueType(ctxt), p);
605-
}
606-
607-
final CoercionAction act = _checkFromStringCoercion(ctxt, text);
608-
if (act == CoercionAction.AsNull) {
609-
return (Long) getNullValue(ctxt);
610-
}
611-
if (act == CoercionAction.AsEmpty) {
612-
return (Long) getEmptyValue(ctxt);
613-
}
614-
text = text.trim();
615-
if (_checkTextualNull(ctxt, text)) {
616-
return (Long) getNullValue(ctxt);
617-
}
618-
// let's allow Strings to be converted too
619-
return _parseLongPrimitive(ctxt, text);
573+
return _parseLong(p, ctxt, Long.class);
620574
}
621575
}
622576

src/main/java/com/fasterxml/jackson/databind/deser/std/StdDeserializer.java

+50
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,56 @@ protected final long _parseLongPrimitive(DeserializationContext ctxt, String tex
879879
}
880880
}
881881

882+
/**
883+
* @since 2.12
884+
*/
885+
protected final Long _parseLong(JsonParser p, DeserializationContext ctxt,
886+
Class<?> targetType)
887+
throws IOException
888+
{
889+
String text;
890+
switch (p.currentTokenId()) {
891+
case JsonTokenId.ID_STRING:
892+
text = p.getText();
893+
break;
894+
case JsonTokenId.ID_NUMBER_FLOAT:
895+
final CoercionAction act = _checkFloatToIntCoercion(p, ctxt, targetType);
896+
if (act == CoercionAction.AsNull) {
897+
return (Long) getNullValue(ctxt);
898+
}
899+
if (act == CoercionAction.AsEmpty) {
900+
return (Long) getEmptyValue(ctxt);
901+
}
902+
return p.getValueAsLong();
903+
case JsonTokenId.ID_NULL: // null fine for non-primitive
904+
return (Long) getNullValue(ctxt);
905+
case JsonTokenId.ID_NUMBER_INT:
906+
return p.getLongValue();
907+
// 29-Jun-2020, tatu: New! "Scalar from Object" (mostly for XML)
908+
case JsonTokenId.ID_START_OBJECT:
909+
text = ctxt.extractScalarFromObject(p, this, targetType);
910+
break;
911+
case JsonTokenId.ID_START_ARRAY:
912+
return (Long) _deserializeFromArray(p, ctxt);
913+
default:
914+
return (Long) ctxt.handleUnexpectedToken(getValueType(ctxt), p);
915+
}
916+
917+
final CoercionAction act = _checkFromStringCoercion(ctxt, text);
918+
if (act == CoercionAction.AsNull) {
919+
return (Long) getNullValue(ctxt);
920+
}
921+
if (act == CoercionAction.AsEmpty) {
922+
return (Long) getEmptyValue(ctxt);
923+
}
924+
text = text.trim();
925+
if (_checkTextualNull(ctxt, text)) {
926+
return (Long) getNullValue(ctxt);
927+
}
928+
// let's allow Strings to be converted too
929+
return _parseLongPrimitive(ctxt, text);
930+
}
931+
882932
protected final float _parseFloatPrimitive(JsonParser p, DeserializationContext ctxt)
883933
throws IOException
884934
{

0 commit comments

Comments
 (0)