Flex Null Integer

2019-01-28 21:45发布

问题:

I take data from Java to Flex by AMF (BlazeDS)

In java side object has Integer field. So it can be null.

In Flex side object is int. So null values are deserialized as 0.

This is not what I want, I want to see whether it is 0 or null.

Is there wrapper like (Integer in Java) for Flex? Thanks

回答1:

As far as I can tell, there is no such wrapper. You can write one that assigns NaN to the internal int if the argument to the constructor is null



回答2:

We solved this by creating a BeanProxy class which overrides setValue and getValue. In there we return NaN to the flex side if the value is a Number and null, and we return null to the Java side if it's a Double and NaN. Job done:

@Override
public void setValue(Object instance, String propertyName, Object value) {
    if ((value instanceof Double)) {
        Double doubleValue = (Double) value;
        if (doubleValue != null && doubleValue.isNaN()) {
            super.setValue(instance, propertyName, null);
        }
    }else{
          super.setValue(instance, propertyName, value);
        }
}

@Override
public Object getValue(Object obj, String propertyName) {
    final Class classType = super.getType(obj, propertyName);
    if (isNumber(classType) && super.getValue(obj, propertyName) == null) {
        return Double.NaN;
    }
    return super.getValue(obj, propertyName);
}


回答3:

Amarghosh has the correct answer, but you'll find as you continue through your project that life is so much easier in the amf world when you apply the "everything is a string" rule. Simply a suggestion that might help a lot in the long run.

Best of luck, Jeremy



回答4:

As Amarghosh answered, there is no such wrapper. As a workaround, we check the int value for -1 which equals an unassigned int value in our domain.