Skip to content

Commit

Permalink
add option to deserialize attribute/custom field numbers as double
Browse files Browse the repository at this point in the history
add support in customfieldsaccessor for JsonNodes
  • Loading branch information
jenschude committed Nov 7, 2023
1 parent 3b6de5c commit e8626a1
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,26 @@ public ApiModule(ModuleOptions options) {
boolean customFieldAsJsonNode = Boolean.parseBoolean(
Optional.ofNullable(options.getOption(ApiModuleOptions.DESERIALIZE_CUSTOM_FIELD_AS_JSON_NODE))
.orElse(System.getProperty(ApiModuleOptions.DESERIALIZE_CUSTOM_FIELD_AS_JSON_NODE)));
boolean attributeNumberAsDouble = Boolean.parseBoolean(
Optional.ofNullable(options.getOption(ApiModuleOptions.DESERIALIZE_ATTRIBUTE_NUMBER_AS_DOUBLE))
.orElse(System.getProperty(ApiModuleOptions.DESERIALIZE_ATTRIBUTE_NUMBER_AS_DOUBLE)));
boolean customFieldNumberAsDouble = Boolean.parseBoolean(
Optional.ofNullable(options.getOption(ApiModuleOptions.DESERIALIZE_CUSTOM_FIELD_NUMBER_AS_DOUBLE))
.orElse(System.getProperty(ApiModuleOptions.DESERIALIZE_CUSTOM_FIELD_NUMBER_AS_DOUBLE)));

if (attributeAsJsonNode) {
setMixInAnnotation(AttributeImpl.class, AttributeJsonNodeMixin.class);
}
else {
addDeserializer(AttributeImpl.class, new AtrributeDeserializer(attributeAsDateString));
addDeserializer(AttributeImpl.class,
new AtrributeDeserializer(attributeAsDateString, attributeNumberAsDouble));
}
if (customFieldAsJsonNode) {
addDeserializer(FieldContainerImpl.class, new CustomFieldJsonNodeDeserializer());
}
else {
addDeserializer(FieldContainerImpl.class, new CustomFieldDeserializer(customFieldAsDateString));
addDeserializer(FieldContainerImpl.class,
new CustomFieldDeserializer(customFieldAsDateString, customFieldNumberAsDouble));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,37 @@ public class ApiModuleOptions implements ModuleOptions {

public static final String DESERIALIZE_CUSTOM_FIELD_AS_JSON_NODE = "commercetools.deserializeCustomFieldAsJsonNode";

public static final String DESERIALIZE_CUSTOM_FIELD_NUMBER_AS_DOUBLE = "commercetools.deserializeCustomFieldNumberAsDouble";

public static final String DESERIALIZE_ATTRIBUTE_NUMBER_AS_DOUBLE = "commercetools.deserializeAttributeNumberAsDouble";

private final Boolean dateAttributeAsString;
private final Boolean dateCustomFieldAsString;

private final Boolean attributeAsJsonNode;
private final Boolean customFieldAsJsonNode;

private final Boolean attributeNumberAsDouble;
private final Boolean customFieldNumberAsDouble;

private ApiModuleOptions() {
this.dateAttributeAsString = false;
this.dateCustomFieldAsString = false;
this.attributeAsJsonNode = false;
this.customFieldAsJsonNode = false;
this.attributeNumberAsDouble = false;
this.customFieldNumberAsDouble = false;
}

private ApiModuleOptions(final Boolean dateAttributeAsString, final Boolean dateCustomFieldAsString,
final Boolean attributeAsJsonNode, final Boolean customFieldAsJsonNode) {
final Boolean attributeAsJsonNode, final Boolean customFieldAsJsonNode,
final Boolean attributeNumberAsDouble, final Boolean customFieldNumberAsDouble) {
this.dateAttributeAsString = dateAttributeAsString;
this.dateCustomFieldAsString = dateCustomFieldAsString;
this.attributeAsJsonNode = attributeAsJsonNode;
this.customFieldAsJsonNode = customFieldAsJsonNode;
this.attributeNumberAsDouble = attributeNumberAsDouble;
this.customFieldNumberAsDouble = customFieldNumberAsDouble;
}

public static ApiModuleOptions of() {
Expand All @@ -52,24 +64,42 @@ public Boolean getCustomFieldAsJsonNode() {
return customFieldAsJsonNode;
}

public Boolean getAttributeNumberAsDouble() {
return attributeNumberAsDouble;
}

public Boolean getCustomFieldNumberAsDouble() {
return customFieldNumberAsDouble;
}

public ApiModuleOptions withDateCustomFieldAsString(Boolean dateCustomFieldAsString) {
return new ApiModuleOptions(dateAttributeAsString, dateCustomFieldAsString, attributeAsJsonNode,
customFieldAsJsonNode);
customFieldAsJsonNode, attributeNumberAsDouble, customFieldNumberAsDouble);
}

public ApiModuleOptions withDateAttributeAsString(Boolean dateAttributeAsString) {
return new ApiModuleOptions(dateAttributeAsString, dateCustomFieldAsString, attributeAsJsonNode,
customFieldAsJsonNode);
customFieldAsJsonNode, attributeNumberAsDouble, customFieldNumberAsDouble);
}

public ApiModuleOptions withAttributeAsJsonNode(Boolean attributeAsJsonNode) {
return new ApiModuleOptions(dateAttributeAsString, dateCustomFieldAsString, attributeAsJsonNode,
customFieldAsJsonNode);
customFieldAsJsonNode, attributeNumberAsDouble, customFieldNumberAsDouble);
}

public ApiModuleOptions withCustomFieldAsJsonNode(Boolean customFieldAsJsonNode) {
return new ApiModuleOptions(dateAttributeAsString, dateCustomFieldAsString, attributeAsJsonNode,
customFieldAsJsonNode);
customFieldAsJsonNode, attributeNumberAsDouble, customFieldNumberAsDouble);
}

public ApiModuleOptions withCustomFieldNumberAsDouble(Boolean customFieldNumberAsDouble) {
return new ApiModuleOptions(dateAttributeAsString, dateCustomFieldAsString, attributeAsJsonNode,
customFieldAsJsonNode, attributeNumberAsDouble, customFieldNumberAsDouble);
}

public ApiModuleOptions withAttributeNumberAsDouble(Boolean attributeNumberAsDouble) {
return new ApiModuleOptions(dateAttributeAsString, dateCustomFieldAsString, attributeAsJsonNode,
customFieldAsJsonNode, attributeNumberAsDouble, customFieldNumberAsDouble);
}

@Override
Expand All @@ -86,6 +116,12 @@ public String getOption(String name) {
if (name.equals(DESERIALIZE_CUSTOM_FIELD_AS_JSON_NODE)) {
return customFieldAsJsonNode.toString();
}
if (name.equals(DESERIALIZE_ATTRIBUTE_NUMBER_AS_DOUBLE)) {
return attributeNumberAsDouble.toString();
}
if (name.equals(DESERIALIZE_CUSTOM_FIELD_NUMBER_AS_DOUBLE)) {
return customFieldNumberAsDouble.toString();
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,21 @@ public class AtrributeDeserializer extends JsonDeserializer<AttributeImpl> {

private final boolean deserializeAsDate;

private final boolean deserializeNumberAsDouble;

public AtrributeDeserializer(boolean deserializeAsDateString) {
this.deserializeAsDate = !deserializeAsDateString;
this.deserializeNumberAsDouble = false;
}

public AtrributeDeserializer(boolean deserializeAsDateString, boolean deserializeNumberAsDouble) {
this.deserializeAsDate = !deserializeAsDateString;
this.deserializeNumberAsDouble = deserializeNumberAsDouble;
}

public AtrributeDeserializer() {
this.deserializeAsDate = true;
this.deserializeNumberAsDouble = false;
}

@Override
Expand All @@ -59,7 +68,7 @@ private TypeReference<?> typeRef(JsonNode valueNode) {
return new TypeReference<Boolean>() {
};
case NUMBER:
if (valueNode.isInt() || valueNode.isLong()) {
if (!deserializeNumberAsDouble && (valueNode.isInt() || valueNode.isLong())) {
return new TypeReference<Long>() {
};
}
Expand Down Expand Up @@ -189,7 +198,7 @@ private ElemType elemType(JsonNode valueNode) {
}
return ElemType.LOCALIZED_STRING;
case NUMBER:
if (valueNode.isInt() || valueNode.isLong()) {
if (!deserializeNumberAsDouble && (valueNode.isInt() || valueNode.isLong())) {
return ElemType.LONG;
}
return ElemType.NUMBER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,21 @@ public class CustomFieldDeserializer extends JsonDeserializer<FieldContainerImpl
private static Pattern time = Pattern.compile("^[0-9]{2}:[0-9]{2}:[0-9]{2}([.][0-9]{1,6})?");

private final boolean deserializeAsDate;
private final boolean deserializeNumberAsDouble;

public CustomFieldDeserializer(boolean deserializeAsDateString) {
this.deserializeAsDate = !deserializeAsDateString;
this.deserializeNumberAsDouble = false;
}

public CustomFieldDeserializer(boolean deserializeAsDateString, boolean deserializeNumberAsDouble) {
this.deserializeAsDate = !deserializeAsDateString;
this.deserializeNumberAsDouble = deserializeNumberAsDouble;
}

public CustomFieldDeserializer() {
this.deserializeAsDate = true;
this.deserializeNumberAsDouble = false;
}

@Override
Expand Down Expand Up @@ -66,7 +74,7 @@ private TypeReference<?> typeRef(JsonNode valueNode) {
return new TypeReference<Boolean>() {
};
case NUMBER:
if (valueNode.isInt() || valueNode.isLong()) {
if (!deserializeNumberAsDouble && (valueNode.isInt() || valueNode.isLong())) {
return new TypeReference<Long>() {
};
}
Expand Down Expand Up @@ -183,7 +191,7 @@ private ElemType elemType(JsonNode valueNode) {
}
return ElemType.LOCALIZED_STRING;
case NUMBER:
if (valueNode.isInt() || valueNode.isLong()) {
if (!deserializeNumberAsDouble && (valueNode.isInt() || valueNode.isLong())) {
return ElemType.LONG;
}
return ElemType.NUMBER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ public static Double asDouble(final Attribute attribute) {
if (attribute.getValue() instanceof Double) {
return (Double) attribute.getValue();
}
if (attribute.getValue() instanceof Number) {
return ((Number) attribute.getValue()).doubleValue();
}
if (attribute.getValue() instanceof JsonNode) {
return ((JsonNode) attribute.getValue()).asDouble();
}
Expand Down Expand Up @@ -148,6 +151,9 @@ public static Long asLong(final Attribute attribute) {
if (attribute.getValue() instanceof Long) {
return (Long) attribute.getValue();
}
if (attribute.getValue() instanceof Number) {
return ((Number) attribute.getValue()).longValue();
}
if (attribute.getValue() instanceof JsonNode) {
return ((JsonNode) attribute.getValue()).asLong();
}
Expand Down
Loading

0 comments on commit e8626a1

Please sign in to comment.