示例#1
0
#pragma warning disable CS0618
        public static void TestNumber(CBORObject o)
        {
            if (o.Type != CBORType.Number)
            {
                return;
            }
            if (o.IsPositiveInfinity() || o.IsNegativeInfinity() ||
                o.IsNaN())
            {
                try {
                    o.AsByte();
                    Assert.Fail("Should have failed");
                } catch (OverflowException) {
                    // NOTE: Intentionally empty
                } catch (Exception ex) {
                    Assert.Fail("Object: " + o + ", " + ex);
                    throw new InvalidOperationException(String.Empty, ex);
                }
                try {
                    o.AsInt16();
                    Assert.Fail("Should have failed");
                } catch (OverflowException) {
                    // NOTE: Intentionally empty
                } catch (Exception ex) {
                    Assert.Fail("Object: " + o + ", " + ex);
                    throw new InvalidOperationException(String.Empty, ex);
                }
                try {
                    o.AsInt32();
                    Assert.Fail("Should have failed");
                } catch (OverflowException) {
                    // NOTE: Intentionally empty
                } catch (Exception ex) {
                    Assert.Fail("Object: " + o + ", " + ex);
                    throw new InvalidOperationException(String.Empty, ex);
                }
                try {
                    o.AsInt64();
                    Assert.Fail("Should have failed");
                } catch (OverflowException) {
                    // NOTE: Intentionally empty
                } catch (Exception ex) {
                    Assert.Fail("Object: " + o + ", " + ex);
                    throw new InvalidOperationException(String.Empty, ex);
                }
                try {
                    o.AsSingle();
                } catch (Exception ex) {
                    Assert.Fail(ex.ToString());
                    throw new InvalidOperationException(String.Empty, ex);
                }
                try {
                    o.AsDouble();
                } catch (Exception ex) {
                    Assert.Fail(ex.ToString());
                    throw new InvalidOperationException(String.Empty, ex);
                }
                try {
                    o.AsEInteger();
                    Assert.Fail("Should have failed");
                } catch (OverflowException) {
                    // NOTE: Intentionally empty
                } catch (Exception ex) {
                    Assert.Fail("Object: " + o + ", " + ex);
                    throw new InvalidOperationException(String.Empty, ex);
                }
                return;
            }
            try {
                o.AsSingle();
            } catch (Exception ex) {
                Assert.Fail("Object: " + o + ", " + ex);
                throw new InvalidOperationException(String.Empty, ex);
            }
            try {
                o.AsDouble();
            } catch (Exception ex) {
                Assert.Fail("Object: " + o + ", " + ex);
                throw new InvalidOperationException(String.Empty, ex);
            }
        }
示例#2
0
 public static void Write(CBORObject obj, Stream stream)
 {
     if (obj.Type == CBORType.Number)
     {
         stream.WriteByte(unchecked ((byte)((byte)'i')));
         writeUtf8(obj.AsEInteger().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(LongToString(length), stream);
         stream.WriteByte(unchecked ((byte)((byte)':')));
         writeUtf8(s, stream);
     }
     else if (obj.Type == CBORType.Map)
     {
         var hasNonStringKeys = false;
         foreach (CBORObject key in obj.Keys)
         {
             if (key.Type != CBORType.TextString)
             {
                 hasNonStringKeys = true;
                 break;
             }
         }
         if (hasNonStringKeys)
         {
             var valueSMap = 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();
                 valueSMap[str] = value;
             }
             stream.WriteByte(unchecked ((byte)((byte)'d')));
             foreach (KeyValuePair <string, CBORObject> entry in valueSMap)
             {
                 string     key    = entry.Key;
                 CBORObject value  = entry.Value;
                 long       length = DataUtilities.GetUtf8Length(key, false);
                 if (length < 0)
                 {
                     throw new CBORException("invalid string");
                 }
                 writeUtf8(
                     LongToString(length),
                     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(LongToString(length), 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 (var 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(LongToString(length), stream);
         stream.WriteByte(unchecked ((byte)((byte)':')));
         writeUtf8(str, stream);
     }
 }
示例#3
0
 public static void Write(CBORObject obj, Stream stream)
 {
     if (obj.Type == CBORType.Number) {
     stream.WriteByte(unchecked((byte)((byte)'i')));
     writeUtf8(obj.AsEInteger().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(LongToString(length), stream);
     stream.WriteByte(unchecked((byte)((byte)':')));
     writeUtf8(s, stream);
       } else if (obj.Type == CBORType.Map) {
     var hasNonStringKeys = false;
     foreach (CBORObject key in obj.Keys) {
       if (key.Type != CBORType.TextString) {
     hasNonStringKeys = true;
     break;
       }
     }
     if (hasNonStringKeys) {
       var valueSMap = 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();
     valueSMap[str] = value;
       }
       stream.WriteByte(unchecked((byte)((byte)'d')));
       foreach (KeyValuePair<string, CBORObject> entry in valueSMap) {
     string key = entry.Key;
     CBORObject value = entry.Value;
     long length = DataUtilities.GetUtf8Length(key, false);
     if (length < 0) {
       throw new CBORException("invalid string");
     }
     writeUtf8(
       LongToString(length),
       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(LongToString(length), 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 (var 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(LongToString(length), stream);
     stream.WriteByte(unchecked((byte)((byte)':')));
     writeUtf8(str, stream);
       }
 }
示例#4
0
 public static void TestNumber(CBORObject o)
 {
     if (o.Type != CBORType.Number) {
     return;
       }
       if (o.IsPositiveInfinity() || o.IsNegativeInfinity() ||
       o.IsNaN()) {
     try {
       o.AsByte();
       Assert.Fail("Should have failed");
     } catch (OverflowException) {
       new Object();
     } catch (Exception ex) {
       Assert.Fail("Object: " + o + ", " + ex); throw new
     InvalidOperationException(String.Empty, ex);
     }
     try {
       o.AsInt16();
       Assert.Fail("Should have failed");
     } catch (OverflowException) {
       new Object();
     } catch (Exception ex) {
       Assert.Fail("Object: " + o + ", " + ex); throw new
     InvalidOperationException(String.Empty, ex);
     }
     try {
       o.AsInt32();
       Assert.Fail("Should have failed");
     } catch (OverflowException) {
       new Object();
     } catch (Exception ex) {
       Assert.Fail("Object: " + o + ", " + ex); throw new
     InvalidOperationException(String.Empty, ex);
     }
     try {
       o.AsInt64();
       Assert.Fail("Should have failed");
     } catch (OverflowException) {
       new Object();
     } catch (Exception ex) {
       Assert.Fail("Object: " + o + ", " + ex); throw new
     InvalidOperationException(String.Empty, ex);
     }
     try {
       o.AsSingle();
     } catch (Exception ex) {
       Assert.Fail(ex.ToString());
       throw new InvalidOperationException(String.Empty, ex);
     }
     try {
       o.AsDouble();
     } catch (Exception ex) {
       Assert.Fail(ex.ToString());
       throw new InvalidOperationException(String.Empty, ex);
     }
     try {
       o.AsEInteger();
       Assert.Fail("Should have failed");
     } catch (OverflowException) {
       new Object();
     } catch (Exception ex) {
       Assert.Fail("Object: " + o + ", " + ex); throw new
     InvalidOperationException(String.Empty, ex);
     }
     return;
       }
       try {
     o.AsSingle();
       } catch (Exception ex) {
     Assert.Fail("Object: " + o + ", " + ex); throw new
       InvalidOperationException(String.Empty, ex);
       }
       try {
     o.AsDouble();
       } catch (Exception ex) {
     Assert.Fail("Object: " + o + ", " + ex); throw new
       InvalidOperationException(String.Empty, ex);
       }
 }