Esempio n. 1
0
 /// <summary>Creates a CBOR number object from a CBOR object
 /// representing a number (that is, one for which the IsNumber property
 /// in.NET or the isNumber() method in Java returns true).</summary>
 /// <param name='o'>The parameter is a CBOR object representing a
 /// number.</param>
 /// <returns>A CBOR number object, or null if the given CBOR object is
 /// null or does not represent a number.</returns>
 public static CBORNumber FromCBORObject(CBORObject o)
 {
     if (o == null)
     {
         return(null);
     }
     if (IsUntaggedInteger(o))
     {
         if (o.CanValueFitInInt64())
         {
             return(new CBORNumber(Kind.Integer, o.AsInt64Value()));
         }
         else
         {
             return(new CBORNumber(Kind.EInteger, o.AsEIntegerValue()));
         }
     }
     else if (!o.IsTagged && o.Type == CBORType.FloatingPoint)
     {
         return(CBORNumber.FromObject(o.AsDoubleValue()));
     }
     if (o.HasOneTag(2) || o.HasOneTag(3))
     {
         return(BignumToNumber(o));
     }
     else if (o.HasOneTag(4) ||
              o.HasOneTag(5) ||
              o.HasOneTag(264) ||
              o.HasOneTag(265) ||
              o.HasOneTag(268) ||
              o.HasOneTag(269))
     {
         return(BigFracToNumber(o,
                                o.MostOuterTag.ToInt32Checked()));
     }
     else if (o.HasOneTag(30) ||
              o.HasOneTag(270))
     {
         return(RationalToNumber(o,
                                 o.MostOuterTag.ToInt32Checked()));
     }
     else
     {
         return(null);
     }
 }
Esempio n. 2
0
        internal static string ToStringHelper(CBORObject obj, int depth)
        {
            StringBuilder sb       = null;
            string        simvalue = null;
            CBORType      type     = obj.Type;
            CBORObject    curobject;

            if (obj.IsTagged)
            {
                if (sb == null)
                {
                    if (type == CBORType.TextString)
                    {
                        // The default capacity of StringBuilder may be too small
                        // for many strings, so set a suggested capacity
                        // explicitly
                        string str = obj.AsString();
                        sb = new StringBuilder(Math.Min(str.Length, 4096) + 16);
                    }
                    else
                    {
                        sb = new StringBuilder();
                    }
                }
                // Append opening tags if needed
                curobject = obj;
                while (curobject.IsTagged)
                {
                    EInteger ei = curobject.MostOuterTag;
                    sb.Append(ei.ToString());
                    sb.Append('(');
                    curobject = curobject.UntagOne();
                }
            }
            switch (type)
            {
            case CBORType.SimpleValue:
                sb = sb ?? new StringBuilder();
                if (obj.IsUndefined)
                {
                    sb.Append("undefined");
                }
                else if (obj.IsNull)
                {
                    sb.Append("null");
                }
                else
                {
                    sb.Append("simple(");
                    int  thisItemInt = obj.SimpleValue;
                    char c;
                    if (thisItemInt >= 100)
                    {
                        // NOTE: '0'-'9' have ASCII code 0x30-0x39
                        c = (char)(0x30 + ((thisItemInt / 100) % 10));
                        sb.Append(c);
                    }
                    if (thisItemInt >= 10)
                    {
                        c = (char)(0x30 + ((thisItemInt / 10) % 10));
                        sb.Append(c);
                        c = (char)(0x30 + (thisItemInt % 10));
                    }
                    else
                    {
                        c = (char)(0x30 + thisItemInt);
                    }
                    sb.Append(c);
                    sb.Append(")");
                }
                break;

            case CBORType.Boolean:
            case CBORType.Integer:
                simvalue = obj.Untag().ToJSONString();
                if (sb == null)
                {
                    return(simvalue);
                }
                sb.Append(simvalue);
                break;

            case CBORType.FloatingPoint: {
                double f = obj.AsDoubleValue();
                simvalue = Double.IsNegativeInfinity(f) ? "-Infinity" :
                           (Double.IsPositiveInfinity(f) ? "Infinity" : (Double.IsNaN(f) ?

                                                                         "NaN" : obj.Untag().ToJSONString()));
                if (sb == null)
                {
                    return(simvalue);
                }
                sb.Append(simvalue);
                break;
            }

            case CBORType.ByteString: {
                sb = sb ?? new StringBuilder();
                sb.Append("h'");
                byte[] data   = obj.GetByteString();
                int    length = data.Length;
                for (var i = 0; i < length; ++i)
                {
                    sb.Append(HexAlphabet[(data[i] >> 4) & 15]);
                    sb.Append(HexAlphabet[data[i] & 15]);
                }
                sb.Append("'");
                break;
            }

            case CBORType.TextString: {
                if (sb == null)
                {
                    return("\"" + obj.AsString() + "\"");
                }
                sb.Append('\"');
                sb.Append(obj.AsString());
                sb.Append('\"');
                break;
            }

            case CBORType.Array: {
                sb = sb ?? new StringBuilder();
                var first = true;
                sb.Append("[");
                if (depth >= 50)
                {
                    sb.Append("...");
                }
                else
                {
                    for (var i = 0; i < obj.Count; ++i)
                    {
                        if (!first)
                        {
                            sb.Append(", ");
                        }
                        sb.Append(ToStringHelper(obj[i], depth + 1));
                        first = false;
                    }
                }
                sb.Append("]");
                break;
            }

            case CBORType.Map: {
                sb = sb ?? new StringBuilder();
                var first = true;
                sb.Append("{");
                if (depth >= 50)
                {
                    sb.Append("...");
                }
                else
                {
                    ICollection <KeyValuePair <CBORObject, CBORObject> > entries =
                        obj.Entries;
                    foreach (KeyValuePair <CBORObject, CBORObject> entry
                             in entries)
                    {
                        CBORObject key   = entry.Key;
                        CBORObject value = entry.Value;
                        if (!first)
                        {
                            sb.Append(", ");
                        }
                        sb.Append(ToStringHelper(key, depth + 1));
                        sb.Append(": ");
                        sb.Append(ToStringHelper(value, depth + 1));
                        first = false;
                    }
                }
                sb.Append("}");
                break;
            }

            default: {
                sb = sb ?? new StringBuilder();
                sb.Append("???");
                break;
            }
            }
            // Append closing tags if needed
            curobject = obj;
            while (curobject.IsTagged)
            {
                sb.Append(')');
                curobject = curobject.UntagOne();
            }
            return(sb.ToString());
        }