Contains methods useful for reading and writing strings. It is designed to have no dependencies other than the basic runtime class library.
Ejemplo n.º 1
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);
     }
 }