Search in sources :

Example 1 with CBORNumber

use of com.upokecenter.cbor.CBORNumber in project leshan by eclipse.

the class SenMLCborPackSerDes method deserializeFromCbor.

public SenMLPack deserializeFromCbor(Collection<CBORObject> objects) throws SenMLException {
    try {
        SenMLPack senMLPack = new SenMLPack();
        for (CBORObject o : objects) {
            SenMLRecord record = new SenMLRecord();
            CBORObject bn = o.get(-2);
            if (bn != null && bn.getType() == CBORType.TextString)
                record.setBaseName(bn.AsString());
            CBORObject bt = o.get(-3);
            if (bt != null && bt.isNumber())
                record.setBaseTime(bt.AsNumber().ToInt64Checked());
            CBORObject n = o.get(0);
            if (n != null && n.getType() == CBORType.TextString)
                record.setName(n.AsString());
            CBORObject t = o.get(6);
            if (t != null && t.isNumber())
                record.setTime(t.AsNumber().ToInt64Checked());
            CBORObject v = o.get(2);
            boolean hasValue = false;
            if (v != null && v.isNumber()) {
                CBORNumber number = v.AsNumber();
                switch(number.getKind()) {
                    case Integer:
                    case EInteger:
                        if (number.IsNegative()) {
                            if (number.CanFitInInt64()) {
                                record.setNumberValue(number.ToInt64Unchecked());
                            } else {
                                record.setNumberValue((BigInteger) v.ToObject(BigInteger.class));
                            }
                        } else {
                            if (number.CanFitInInt64()) {
                                record.setNumberValue(number.ToInt64Unchecked());
                            } else if (number.ToEIntegerIfExact().GetSignedBitLengthAsInt64() == 64) {
                                record.setNumberValue(ULong.valueOf(number.ToInt64Unchecked()));
                            } else {
                                record.setNumberValue((BigInteger) v.ToObject(BigInteger.class));
                            }
                        }
                        break;
                    case Double:
                    case EFloat:
                    case EDecimal:
                        if (v.AsNumber().CanFitInDouble()) {
                            record.setNumberValue(v.AsDoubleValue());
                        } else {
                            record.setNumberValue((BigDecimal) v.ToObject(BigDecimal.class));
                        }
                        break;
                    default:
                        throw new SenMLException("Invalid SenML record : unexpected kind of number %s is not supported in %s", number.getKind(), o);
                }
                hasValue = true;
            }
            CBORObject vb = o.get(4);
            if (vb != null && vb.getType() == CBORType.Boolean) {
                record.setBooleanValue(vb.AsBoolean());
                hasValue = true;
            }
            CBORObject vs = o.get(3);
            if (vs != null && vs.getType() == CBORType.TextString) {
                record.setStringValue(vs.AsString());
                hasValue = true;
            }
            CBORObject vlo = o.get("vlo");
            if (vlo != null && vlo.getType() == CBORType.TextString) {
                record.setObjectLinkValue(vlo.AsString());
                hasValue = true;
            }
            CBORObject vd = o.get(8);
            if (vd != null && vd.getType() == CBORType.ByteString) {
                record.setOpaqueValue(vd.GetByteString());
                hasValue = true;
            }
            if (!allowNoValue && !hasValue)
                throw new SenMLException("Invalid SenML record : record must have a value (v,vb,vlo,vd,vs) : %s", o);
            senMLPack.addRecord(record);
        }
        return senMLPack;
    } catch (IllegalArgumentException | IllegalStateException e) {
        throw new SenMLException(e, "Unable to serialize SenML in CBOR");
    }
}
Also used : BigInteger(java.math.BigInteger) SenMLPack(org.eclipse.leshan.senml.SenMLPack) SenMLRecord(org.eclipse.leshan.senml.SenMLRecord) SenMLException(org.eclipse.leshan.senml.SenMLException) CBORNumber(com.upokecenter.cbor.CBORNumber) CBORObject(com.upokecenter.cbor.CBORObject)

Aggregations

CBORNumber (com.upokecenter.cbor.CBORNumber)1 CBORObject (com.upokecenter.cbor.CBORObject)1 BigInteger (java.math.BigInteger)1 SenMLException (org.eclipse.leshan.senml.SenMLException)1 SenMLPack (org.eclipse.leshan.senml.SenMLPack)1 SenMLRecord (org.eclipse.leshan.senml.SenMLRecord)1