diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index afe4fe30..7f0d344d 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -33,3 +33,6 @@ jobs: # Optional: Uploads the full dependency graph to GitHub to improve the quality of Dependabot alerts this repository can receive - name: Update dependency graph uses: advanced-security/maven-dependency-submission-action@571e99aab1055c2e71a1e2309b9691de18d6b7d6 + +permissions: + contents: read diff --git a/README.md b/README.md index 4069c37a..23766177 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ --- -A Java implementation of Concise Binary Object Representation, a general-purpose binary data format defined in RFC 7049. According to that RFC, CBOR's data model "is an extended version of the JSON data model", supporting many more types of data than JSON. "CBOR was inspired by MessagePack", but "is not intended as a version of or replacement for MessagePack." +A Java implementation of Concise Binary Object Representation, a general-purpose binary data format defined in RFC 8949. According to that RFC, CBOR's data model "is an extended version of the JSON data model", supporting many more types of data than JSON. "CBOR was inspired by MessagePack", but "is not intended as a version of or replacement for MessagePack." This implementation was written by Peter O. and is released to the Public Domain under the [CC0 Declaration](https://creativecommons.org/publicdomain/zero/1.0/). diff --git a/src/main/java/com/upokecenter/cbor/CBORObject.java b/src/main/java/com/upokecenter/cbor/CBORObject.java index 178928b6..f9dce268 100644 --- a/src/main/java/com/upokecenter/cbor/CBORObject.java +++ b/src/main/java/com/upokecenter/cbor/CBORObject.java @@ -2106,6 +2106,14 @@ public static CBORObject FromInt32(int value) { FromInt64((long)value); } + /** + * Generates a CBOR object from a java.util.UUID. + * @param value The parameter {@code value} is a java.util.UUID. + * @return A CBOR object. + */ + public static CBORObject FromGuid(java.util.UUID value) { return new +CBORUuidConverter().ToCBORObject(value); } + /** * Generates a CBOR object from a 32-bit signed integer. * @param value The parameter {@code value} is a 32-bit signed integer. @@ -2898,6 +2906,22 @@ public static CBORObject NewMap() { new TreeMap()); } + /** + * Creates a new CBOR map that stores its keys in an undefined order. + * @param keysAndValues A sequence of key-value pairs. + * @return A new CBOR map. + */ + public static CBORObject FromMap(Iterable> keysAndValues) { + TreeMap sd = new TreeMap(); + foreach (Tuple kv in keysAndValues) { + sd.put(kv.getItem1(), kv.getItem2()); + } + 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. @@ -2909,6 +2933,22 @@ public static CBORObject NewOrderedMap() { PropertyMap.NewOrderedDict()); } + /** + * Creates a new CBOR map that ensures that keys are stored in order. + * @param keysAndValues A sequence of key-value pairs. + * @return A new CBOR map. + */ + public static CBORObject FromOrderedMap(Iterable> keysAndValues) { + PropertyMap oDict = PropertyMap.NewOrderedDict(); + foreach (Tuple kv in keysAndValues) { + oDict.Add(kv.getItem1(), kv.getItem2()); + } + 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 the end of the stream is @@ -4271,6 +4311,16 @@ public float AsSingle() { return cn.GetNumberInterface().AsSingle(cn.GetValue()); } + /** + * Converts this object to a java.util.UUID. + * @return A java.util.UUID. + * @throws IllegalStateException This object does not represent a java.util.UUID. + * @throws CBORException This object does not have the expected tag. + */ + public java.util.UUID AsGuid() { + return new CBORUuidConverter().FromCBORObject(this); + } + /** *

Gets the value of this object as a text string.

This method is not * the "reverse" of the {@code FromString} method in the sense that FromString diff --git a/src/main/java/com/upokecenter/cbor/CBORUuidConverter.java b/src/main/java/com/upokecenter/cbor/CBORUuidConverter.java index 7d51e61b..84d6172b 100644 --- a/src/main/java/com/upokecenter/cbor/CBORUuidConverter.java +++ b/src/main/java/com/upokecenter/cbor/CBORUuidConverter.java @@ -37,18 +37,11 @@ public java.util.UUID FromCBORObject(CBORObject obj) { throw new CBORException("Must have outermost tag 37"); } ValidateObject(obj); - byte[] bytes = obj.GetByteString(); - char[] guidChars = new char[36]; - String hex = "0123456789abcdef"; - int index = 0; - for (int i = 0; i < 16; ++i) { - if (i == 4 || i == 6 || i == 8 || i == 10) { - guidChars[index++] = '-'; - } - guidChars[index++] = hex.charAt((bytes[i] >> 4) & 15); - guidChars[index++] = hex.charAt(bytes[i] & 15); - } - String guidString = new String(guidChars); - return java.util.UUID.fromString(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 java.util.UUID.fromString(bytes); } } diff --git a/src/main/java/com/upokecenter/cbor/PropertyMap.java b/src/main/java/com/upokecenter/cbor/PropertyMap.java index 924e2409..b77191fd 100644 --- a/src/main/java/com/upokecenter/cbor/PropertyMap.java +++ b/src/main/java/com/upokecenter/cbor/PropertyMap.java @@ -675,6 +675,34 @@ public static byte[] UUIDToBytes(java.util.UUID obj){ return bytes2; } + public static java.util.UUID UUIDFromDotNetBytes(byte[] bytes) { + char[] guidChars=new char[36]; + String hex = "0123456789abcdef"; + int index = 0; + guidChars[index++]=bytes[3]; + guidChars[index++]=bytes[2]; + guidChars[index++]=bytes[1]; + guidChars[index++]=bytes[0]; + guidChars[index++]='-'; + guidChars[index++]=bytes[5]; + guidChars[index++]=bytes[4]; + guidChars[index++]='-'; + guidChars[index++]=bytes[7]; + guidChars[index++]=bytes[6]; + guidChars[index++]='-'; + guidChars[index++]=bytes[8]; + guidChars[index++]=bytes[9]; + guidChars[index++]='-'; + guidChars[index++]=bytes[10]; + guidChars[index++]=bytes[11]; + guidChars[index++]=bytes[12]; + guidChars[index++]=bytes[13]; + guidChars[index++]=bytes[14]; + guidChars[index++]=bytes[15]; + String guidString = new String(guidChars); + return java.util.UUID.fromString(guidString); + } + public static CBORObject FromObjectOther(Object obj) { if (obj instanceof BigDecimal) { // TODO: Avoid going through EDecimal diff --git a/src/test/java/com/upokecenter/test/CBORSupplementTest.java b/src/test/java/com/upokecenter/test/CBORSupplementTest.java index b5114b02..1100d6cd 100644 --- a/src/test/java/com/upokecenter/test/CBORSupplementTest.java +++ b/src/test/java/com/upokecenter/test/CBORSupplementTest.java @@ -11,6 +11,7 @@ licensed under Creative Commons Zero (CC0): import java.io.*; import org.junit.Assert; import org.junit.Test; +import com.upokecenter.util.*; import com.upokecenter.cbor.*; import com.upokecenter.numbers.*; @@ -705,6 +706,19 @@ public void TestUUID() { Assert.assertEquals((byte)0xdd, bytes[13]); Assert.assertEquals((byte)0xee, bytes[14]); Assert.assertEquals((byte)0xff, bytes[15]); + bytes = new byte[] { + 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, + (byte)0x88, (byte)0x99, (byte)0xaa, (byte)0xbb, (byte)0xcc, (byte)0xdd, (byte)0xee, (byte)0xff, + }; + obj = CBORObject.FromCBORObjectAndTag( + CBORObject.FromByteArray(bytes), + 37); + { + String stringTemp = com.upokecenter.util.DataUtilities.ToLowerCaseAscii(obj.AsGuid().toString()); + Assert.assertEquals( + "00112233-4455-6677-8899-aabbccddeeff", + stringTemp); +} } // @Test