diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index 2546ec5c..4d685168 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -2332,6 +2332,12 @@ public static CBORObject FromInt32(int value) { FromInt64((long)value); } + /// Generates a CBOR object from a Guid. + /// The parameter is a + /// Guid. + /// A CBOR object. + public static CBORObject FromGuid(Guid value) => new CBORUuidConverter().ToCBORObject(value); + /// Generates a CBOR object from a 32-bit signed /// integer. /// The parameter is a @@ -3230,6 +3236,20 @@ public static CBORObject NewMap() { new SortedDictionary()); } + /// Creates a new CBOR map that stores its keys in an + /// undefined order. + /// A sequence of key-value pairs. + /// A new CBOR map. + public static CBORObject FromMap(IEnumerable> keysAndValues) { + var sd = new SortedDictionary(); + foreach (Tuple kv in keysAndValues) { + sd.Add(kv.Item1, kv.Item2); + } + return new CBORObject( + CBORObjectTypeMap, + sd); + } + /// Creates a new empty CBOR map that ensures that keys are /// stored in the order in which they are first inserted. /// A new CBOR map. @@ -3239,6 +3259,20 @@ public static CBORObject NewOrderedMap() { PropertyMap.NewOrderedDict()); } + /// Creates a new CBOR map that ensures that keys are + /// stored in order. + /// A sequence of key-value pairs. + /// A new CBOR map. + public static CBORObject FromOrderedMap(IEnumerable> keysAndValues) { + var oDict = PropertyMap.NewOrderedDict(); + foreach (Tuple kv in keysAndValues) { + oDict.Add(kv.Item1, kv.Item2); + } + return new CBORObject( + CBORObjectTypeMap, + oDict); + } + /// /// Reads a sequence of objects in CBOR format from a data /// stream. This method will read CBOR objects from the stream until @@ -4661,6 +4695,15 @@ public float AsSingle() { return cn.GetNumberInterface().AsSingle(cn.GetValue()); } + /// Converts this object to a Guid. + /// A Guid. + /// This object does + /// not represent a Guid. + /// This object does not have the expected tag. + public Guid AsGuid() { + return new CBORUuidConverter().FromCBORObject(this); + } + /// Gets the value of this object as a text string. /// Gets this object's string. /// This object's type is diff --git a/CBOR/PeterO/Cbor/CBORUuidConverter.cs b/CBOR/PeterO/Cbor/CBORUuidConverter.cs index 55eb9668..8f02b10c 100644 --- a/CBOR/PeterO/Cbor/CBORUuidConverter.cs +++ b/CBOR/PeterO/Cbor/CBORUuidConverter.cs @@ -34,19 +34,9 @@ public Guid FromCBORObject(CBORObject obj) { throw new CBORException("Must have outermost tag 37"); } _ = ValidateObject(obj); - byte[] bytes = obj.GetByteString(); - var guidChars = new char[36]; - string hex = "0123456789abcdef"; - var index = 0; - for (int i = 0; i < 16; ++i) { - if (i == 4 || i == 6 || i == 8 || i == 10) { - guidChars[index++] = '-'; - } - guidChars[index++] = hex[(bytes[i] >> 4) & 15]; - guidChars[index++] = hex[bytes[i] & 15]; - } - var guidString = new String(guidChars); - return new Guid(guidString); + byte[] b2 = obj.GetByteString(); + byte[] bytes = { b2[3], b2[2], b2[1], b2[0], b2[5], b2[4], b2[7], b2[6], b2[8], b2[9], b2[10], b2[11], b2[12], b2[13], b2[14], b2[15] }; + return new Guid(bytes); } } }