@@ -20,7 +20,6 @@ licensed under Creative Commons Zero (CC0):
2020// for ReadJSON, FromJSONString, FromJSONBytes
2121// TODO: In Java version add overloads for Class<T> in overloads
2222// that take java.lang.reflect.getType()
23- // TODO: Add TryGetValue method
2423
2524 /**
2625 * <p>Represents an object in Concise Binary Object Representation (CBOR) and
@@ -166,6 +165,7 @@ private static CBORObject ConstructIntegerValue(int v) {
166165 private static final int CBORObjectTypeSimpleValue = 7 ;
167166 private static final int CBORObjectTypeDouble = 8 ;
168167 private static final int CBORObjectTypeTextStringUtf8 = 9 ;
168+ private static final int CBORObjectTypeTextStringAscii = 10 ;
169169
170170 private static final int StreamedStringBufferLength = 4096 ;
171171
@@ -480,6 +480,7 @@ public final CBORType getType() {
480480 return CBORType .ByteString ;
481481 case CBORObjectTypeTextString :
482482 case CBORObjectTypeTextStringUtf8 :
483+ case CBORObjectTypeTextStringAscii :
483484 return CBORType .TextString ;
484485 default : throw new IllegalStateException ("Unexpected data type" );
485486 }
@@ -645,7 +646,7 @@ public CBORObject GetOrDefault(Object key, CBORObject defaultValue) {
645646 if (this .getType () == CBORType .Map ) {
646647 Map <CBORObject , CBORObject > map = this .AsMap ();
647648 CBORObject ckey = CBORObject .FromObject (key );
648- return (! map . containsKey ( ckey )) ? defaultValue : map . get ( ckey );
649+ return PropertyMap . GetOrDefault ( map , ckey , defaultValue );
649650 }
650651 return defaultValue ;
651652 }
@@ -1932,6 +1933,11 @@ private long CalcEncodedSize(int depth) {
19321933 size = (size + IntegerByteLength (bytes .length ));
19331934 return size + bytes .length ;
19341935 }
1936+ if (cbor .getItemType () == CBORObjectTypeTextStringAscii ) {
1937+ String str = (String )this .getThisItem ();
1938+ size = (size + IntegerByteLength (str .length ()));
1939+ return size + str .length ();
1940+ }
19351941 switch (cbor .getType ()) {
19361942 case Integer : {
19371943 if (cbor .CanValueFitInInt64 ()) {
@@ -2200,11 +2206,14 @@ public static CBORObject FromObject(String strValue) {
22002206 if (strValue .length () == 0 ) {
22012207 return GetFixedObject (0x60 );
22022208 }
2203- if (DataUtilities .GetUtf8Length (strValue , false ) < 0 ) {
2209+ long utf8Length = DataUtilities .GetUtf8Length (strValue , false );
2210+ if (utf8Length < 0 ) {
22042211 throw new IllegalArgumentException ("String contains an unpaired " +
22052212 "surrogate code point." );
22062213 }
2207- return new CBORObject (CBORObjectTypeTextString , strValue );
2214+ return new CBORObject (
2215+ strValue .length () == utf8Length ? CBORObjectTypeTextStringAscii : CBORObjectTypeTextString ,
2216+ strValue );
22082217 }
22092218
22102219 /**
@@ -4592,7 +4601,8 @@ public float AsSingle() {
45924601 public String AsString () {
45934602 int type = this .getItemType ();
45944603 switch (type ) {
4595- case CBORObjectTypeTextString : {
4604+ case CBORObjectTypeTextString :
4605+ case CBORObjectTypeTextStringAscii : {
45964606 return (String )this .getThisItem ();
45974607 }
45984608 case CBORObjectTypeTextStringUtf8 : {
@@ -4801,6 +4811,15 @@ public int compareTo(CBORObject other) {
48014811 (byte [])objB );
48024812 break ;
48034813 }
4814+ case CBORObjectTypeTextStringAscii : {
4815+ String strA = (String )objA ;
4816+ String strB = (String )objB ;
4817+ int alen = strA .length ();
4818+ int blen = strB .length ();
4819+ cmp = (alen < blen ) ? (-1 ) : ((alen > blen ) ? 1 :
4820+ strA .compareTo (strB ));
4821+ break ;
4822+ }
48044823 case CBORObjectTypeTextString : {
48054824 String strA = (String )objA ;
48064825 String strB = (String )objB ;
@@ -4847,20 +4866,37 @@ public int compareTo(CBORObject other) {
48474866 cmp = CBORUtilities .ByteArrayCompare (
48484867 this .EncodeToBytes (),
48494868 other .EncodeToBytes ());
4850- } else if (typeB == CBORObjectTypeTextString && typeA ==
4869+ } else if ((typeB == CBORObjectTypeTextString || typeB ==
4870+ CBORObjectTypeTextStringAscii ) && typeA ==
48514871 CBORObjectTypeTextStringUtf8 ) {
48524872 cmp = -CBORUtilities .CompareUtf16Utf8LengthFirst (
48534873 (String )objB ,
48544874 (byte [])objA );
4855- } else if (typeA == CBORObjectTypeTextString && typeB ==
4875+ } else if ((typeA == CBORObjectTypeTextString || typeA ==
4876+ CBORObjectTypeTextStringAscii ) && typeB ==
4877+ CBORObjectTypeTextStringUtf8 ) {
4878+ cmp = CBORUtilities .CompareUtf16Utf8LengthFirst (
4879+ (String )objA ,
4880+ (byte [])objB );
4881+ } else if ((typeA == CBORObjectTypeTextString && typeB ==
4882+ CBORObjectTypeTextStringAscii ) ||
4883+ (typeB == CBORObjectTypeTextString && typeA ==
4884+ CBORObjectTypeTextStringAscii )) {
4885+ cmp = -CBORUtilities .CompareStringsAsUtf8LengthFirst (
4886+ (String )objB ,
4887+ (String )objA );
4888+ } else if ((typeA == CBORObjectTypeTextString || typeA ==
4889+ CBORObjectTypeTextStringAscii ) && typeB ==
48564890 CBORObjectTypeTextStringUtf8 ) {
48574891 cmp = CBORUtilities .CompareUtf16Utf8LengthFirst (
48584892 (String )objA ,
48594893 (byte [])objB );
48604894 } else {
4861- int ta = (typeA == CBORObjectTypeTextStringUtf8 ) ?
4895+ int ta = (typeA == CBORObjectTypeTextStringUtf8 || typeA ==
4896+ CBORObjectTypeTextStringAscii ) ?
48624897 CBORObjectTypeTextString : typeA ;
4863- int tb = (typeB == CBORObjectTypeTextStringUtf8 ) ?
4898+ int tb = (typeB == CBORObjectTypeTextStringUtf8 || typeB ==
4899+ CBORObjectTypeTextStringAscii ) ?
48644900 CBORObjectTypeTextString : typeB ;
48654901 /* NOTE: itemtypeValue numbers are ordered such that they
48664902 // correspond to the lexicographical order of their CBOR encodings
@@ -5021,7 +5057,8 @@ public byte[] EncodeToBytes(CBOREncodeOptions options) {
50215057 }
50225058 if (!hasComplexTag ) {
50235059 switch (this .getItemType ()) {
5024- case CBORObjectTypeTextString : {
5060+ case CBORObjectTypeTextString :
5061+ case CBORObjectTypeTextStringAscii : {
50255062 byte [] ret = GetOptimizedBytesIfShortAscii (
50265063 this .AsString (), tagged ? (((int )tagbyte ) & 0xff ) : -1 );
50275064 if (ret != null ) {
@@ -5150,13 +5187,15 @@ public boolean equals(CBORObject other) {
51505187 if (this == otherValue ) {
51515188 return true ;
51525189 }
5153- if (this .itemtypeValue == CBORObjectTypeTextString &&
5190+ if ((this .itemtypeValue == CBORObjectTypeTextString ||
5191+ this .itemtypeValue == CBORObjectTypeTextStringAscii ) &&
51545192 otherValue .itemtypeValue == CBORObjectTypeTextStringUtf8 ) {
51555193 return CBORUtilities .StringEqualsUtf8 (
51565194 (String )this .itemValue ,
51575195 (byte [])otherValue .itemValue );
51585196 }
5159- if (otherValue .itemtypeValue == CBORObjectTypeTextString &&
5197+ if ((otherValue .itemtypeValue == CBORObjectTypeTextString ||
5198+ otherValue .itemtypeValue == CBORObjectTypeTextStringAscii ) &&
51605199 this .itemtypeValue == CBORObjectTypeTextStringUtf8 ) {
51615200 return CBORUtilities .StringEqualsUtf8 (
51625201 (String )otherValue .itemValue ,
@@ -5234,6 +5273,7 @@ public byte[] GetByteString() {
52345273 itemHashCode = CBORArrayHashCode (this .AsList ());
52355274 break ;
52365275 case CBORObjectTypeTextString :
5276+ case CBORObjectTypeTextStringAscii :
52375277 itemHashCode = CBORUtilities .StringHashCode (
52385278 (String )this .itemValue );
52395279 break ;
@@ -6555,7 +6595,8 @@ public void WriteTo(OutputStream stream, CBOREncodeOptions options) throws java.
65556595 stream .write (arr , 0 , arr .length );
65566596 break ;
65576597 }
6558- case CBORObjectTypeTextString : {
6598+ case CBORObjectTypeTextString :
6599+ case CBORObjectTypeTextStringAscii : {
65596600 Write ((String )this .getThisItem (), stream , options );
65606601 break ;
65616602 }
0 commit comments