public static void TestNumber(CBORObject o) { if(o.Type!= CBORType.Number){ return; } if(o.IsPositiveInfinity() || o.IsNegativeInfinity() || o.IsNaN()){ try { o.AsByte(); } catch(OverflowException){ } catch(Exception ex){ Assert.Fail("Object: "+o+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsInt16(); } catch(OverflowException){ } catch(Exception ex){ Assert.Fail("Object: "+o+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsInt32(); } catch(OverflowException){ } catch(Exception ex){ Assert.Fail("Object: "+o+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsInt64(); } catch(OverflowException){ } catch(Exception ex){ Assert.Fail("Object: "+o+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsSingle(); } catch(OverflowException){ } catch(Exception ex){ Assert.Fail("Object: "+o+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsDouble(); } catch(OverflowException){ } catch(Exception ex){ Assert.Fail("Object: "+o+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsBigInteger(); } catch(OverflowException){ } catch(Exception ex){ Assert.Fail("Object: "+o+", "+ex.ToString()); throw new InvalidOperationException("",ex); } return; } BigInteger df=o.AsExtendedDecimal().ToBigInteger(); try { o.AsBigInteger(); } catch(Exception ex){ Assert.Fail("Object: "+o+", int: "+df+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsSingle(); } catch(Exception ex){ Assert.Fail("Object: "+o+", int: "+df+", "+ex.ToString()); throw new InvalidOperationException("",ex); } try { o.AsDouble(); } catch(Exception ex){ Assert.Fail("Object: "+o+", int: "+df+", "+ex.ToString()); throw new InvalidOperationException("",ex); } }
public static void Write(CBORObject obj, Stream stream) { if (obj.Type == CBORType.Number) { stream.WriteByte(unchecked((byte)((byte)'i'))); writeUtf8(obj.AsBigInteger().ToString(), stream); stream.WriteByte(unchecked((byte)((byte)'e'))); } else if (obj.Type == CBORType.TextString) { string s = obj.AsString(); long length = DataUtilities.GetUtf8Length(s, false); if (length < 0) throw new CBORException("invalid string"); writeUtf8(Convert.ToString((long)length, CultureInfo.InvariantCulture), stream); stream.WriteByte(unchecked((byte)((byte)':'))); writeUtf8(s, stream); } else if (obj.Type == CBORType.Map) { bool hasNonStringKeys = false; foreach (CBORObject key in obj.Keys) { if (key.Type != CBORType.TextString) { hasNonStringKeys = true; break; } } if (hasNonStringKeys) { var sMap = new Dictionary<String, CBORObject>(); // Copy to a map with String keys, since // some keys could be duplicates // when serialized to strings foreach (CBORObject key in obj.Keys) { CBORObject value = obj[key]; string str = (key.Type == CBORType.TextString) ? key.AsString() : key.ToJSONString(); sMap[str] = value; } stream.WriteByte(unchecked((byte)((byte)'d'))); foreach (KeyValuePair<string, CBORObject> entry in sMap) { string key = entry.Key; CBORObject value = entry.Value; long length = DataUtilities.GetUtf8Length(key, false); if (length < 0) throw new CBORException("invalid string"); writeUtf8(Convert.ToString((long)length, CultureInfo.InvariantCulture), stream); stream.WriteByte(unchecked((byte)((byte)':'))); writeUtf8(key, stream); Write(value, stream); } stream.WriteByte(unchecked((byte)((byte)'e'))); } else { stream.WriteByte(unchecked((byte)((byte)'d'))); foreach (CBORObject key in obj.Keys) { string str = key.AsString(); long length = DataUtilities.GetUtf8Length(str, false); if (length < 0) throw new CBORException("invalid string"); writeUtf8(Convert.ToString((long)length, CultureInfo.InvariantCulture), stream); stream.WriteByte(unchecked((byte)((byte)':'))); writeUtf8(str, stream); Write(obj[key], stream); } stream.WriteByte(unchecked((byte)((byte)'e'))); } } else if (obj.Type == CBORType.Array) { stream.WriteByte(unchecked((byte)((byte)'l'))); for (int i = 0; i < obj.Count; i++) { Write(obj[i], stream); } stream.WriteByte(unchecked((byte)((byte)'e'))); } else { string str = obj.ToJSONString(); long length = DataUtilities.GetUtf8Length(str, false); if (length < 0) throw new CBORException("invalid string"); writeUtf8(Convert.ToString((long)length, CultureInfo.InvariantCulture), stream); stream.WriteByte(unchecked((byte)((byte)':'))); writeUtf8(str, stream); } }