Skip to content

Commit

Permalink
update README
Browse files Browse the repository at this point in the history
  • Loading branch information
peteroupc committed Jul 21, 2024
1 parent 40d8578 commit e9ba37e
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/).

Expand Down
50 changes: 50 additions & 0 deletions src/main/java/com/upokecenter/cbor/CBORObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -2898,6 +2906,22 @@ public static CBORObject NewMap() {
new TreeMap<CBORObject, CBORObject>());
}

/**
* 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<Tuple<CBORObject,
CBORObject >> keysAndValues) {
TreeMap<CBORObject, CBORObject> sd = new TreeMap<CBORObject, CBORObject>();
foreach (Tuple<CBORObject, CBORObject> 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.
Expand All @@ -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<Tuple<CBORObject,
CBORObject >> keysAndValues) {
PropertyMap oDict = PropertyMap.NewOrderedDict();
foreach (Tuple<CBORObject, CBORObject> 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
Expand Down Expand Up @@ -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);
}

/**
* <p>Gets the value of this object as a text string.</p> <p>This method is not
* the "reverse" of the {@code FromString} method in the sense that FromString
Expand Down
19 changes: 6 additions & 13 deletions src/main/java/com/upokecenter/cbor/CBORUuidConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
28 changes: 28 additions & 0 deletions src/main/java/com/upokecenter/cbor/PropertyMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/com/upokecenter/test/CBORSupplementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e9ba37e

Please sign in to comment.