Flex Null Integer

2019-01-28 21:22发布

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

4条回答
欢心
2楼-- · 2019-01-28 21:55

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楼-- · 2019-01-28 21:57

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楼-- · 2019-01-28 22:09

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

查看更多
Summer. ? 凉城
5楼-- · 2019-01-28 22:09

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.

查看更多
登录 后发表回答