简体   繁体   English

Flex Null Integer

[英]Flex Null Integer

I take data from Java to Flex by AMF (BlazeDS) 我通过AMF(BlazeDS)将数据从Java传递到Flex

In java side object has Integer field. 在java方面,对象有Integer字段。 So it can be null. 所以它可以为null。

In Flex side object is int. 在Flex中,对象是int。 So null values are deserialized as 0. 因此空值反序列化为0。

This is not what I want, I want to see whether it is 0 or null. 这不是我想要的,我想看看它是0还是null。

Is there wrapper like (Integer in Java) for Flex? Flex中是否存在类似(Java中的Integer)的包装器? Thanks 谢谢

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 如果构造函数的参数为null ,您可以编写一个将NaN分配给内部int的程序

We solved this by creating a BeanProxy class which overrides setValue and getValue. 我们通过创建一个覆盖setValue和getValue的BeanProxy类来解决这个问题。 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. 在那里,如果值是Number并且为null,则返回NaN到flex侧,如果它是Double和NaN,我们将返回null到Java端。 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);
}

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. Amarghosh有正确的答案,但是当你继续完成你的项目时,你会发现当你应用“一切都是字符串”规则时,amf世界的生活会变得如此简单。 Simply a suggestion that might help a lot in the long run. 从长远来看,这个建议可能会有很大帮助。

Best of luck, Jeremy 祝你好运,杰里米

As Amarghosh answered, there is no such wrapper. 正如Amarghosh回答的那样,没有这样的包装。 As a workaround, we check the int value for -1 which equals an unassigned int value in our domain. 作为一种解决方法,我们检查-1的int值,它等于我们域中的未分配的int值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM