private static String DecodeTextString(byte[] bytes) { if (bytes ==
- null) { throw new ArgumentNullException(nameof(mapObj));}
- if
- (bytes.Length == 0 || bytes[0]<0x60 || bytes[0]>0x7f) {throw new
- CBORException();} return CBORObject.DecodeFromBytes(bytes,
- CBOREncodeOptions.Default).AsString(); }
- .
- Type arrayListString = new ParameterizedType() { public Type[]
- getActualTypeArguments() { /* Contains one type parameter,
- String*/
- return new Type[] { String.class }; }
- public Type getRawType() { /* Raw type is
- ArrayList */ return ArrayList.class; }
- public Type getOwnerType() {
- return null; } };
- ArrayList<String> array = (ArrayList<String>)
- cborArray.ToObject(arrayListString);
- var array = (List<String>)cborArray.ToObject(
- typeof(List<String>));
- .
- var conv = new CBORTypeMapper().AddConverter(typeof(DateTime),
- CBORDateConverter.UntaggedNumber);
- var obj = CBORObject.FromObject().ToObject<DateTime>(conv);
- Type arrayListString = new ParameterizedType() { public Type[]
- getActualTypeArguments() { /* Contains one type parameter,
- String*/
- return new Type[] { String.class }; }
- public Type getRawType() { /* Raw type is
- ArrayList */ return ArrayList.class; } public Type getOwnerType() {
- return null; } }; ArrayList<String> array =
- (ArrayList<String>) cborArray.ToObject(arrayListString);
- var array = (List<String>)cborArray.ToObject(
- typeof(List<String>));
- .
- var conv = new CBORTypeMapper().AddConverter(typeof(DateTime),
- CBORDateConverter.TaggedNumber);
- CBORObject obj = CBORObject.FromObject(DateTime.Now, conv);
- long x = -40L; /* Example 64-bit value treated as 2^64-40.*/
- CBORObject obj = CBORObject.FromObject(
- v < 0 ? EInteger.FromInt32(1).ShiftLeft(64).Add(v) :
- EInteger.FromInt64(v));
- long x = -40L; /* Example 64-bit value treated as 2^64-40.*/
- CBORObject obj = CBORObject.FromObject(
- v < 0 ? BigInteger.valueOf(1).shiftLeft(64).add(BigInteger.valueOf(v)) :
- BigInteger.valueOf(v));
- CBORObject obj = CBORObject.NewArray() .Add(CBORObject.False)
- .Add(CBORObject.FromObject(5)) .Add(CBORObject.FromObject("text
- string")) .Add(CBORObject.FromObjectAndTag(9999, 1));
- .
- CBORObject obj = CBORObject.NewArray() .Add(CBORObject.False) .Add(5)
- .Add("text string") .Add(CBORObject.FromObjectAndTag(9999, 1));
- .
- CBORObject obj = CBORObject.FromInt32(99999);
- if (obj.CanValueFitInInt32()) { /* Not an Int32;
- handle the error */ Console.WriteLine("Not a 32-bit integer."); } else {
- Console.WriteLine("The value is " + obj.AsInt32Value()); }
- .
- CBORObject obj = CBORObject.FromInt64(99999);
- if (obj.CanValueFitInInt64()) {
- /* Not an Int64; handle the error*/
- Console.WriteLine("Not a 64-bit integer."); } else {
- Console.WriteLine("The value is " + obj.AsInt64Value()); }
- .
- CBORObject obj = CBORObject.FromInt32(99999);
- if (obj.AsNumber().CanTruncatedIntFitInInt32()) {
- /* Not an Int32; handle the error */
- Console.WriteLine("Not a 32-bit integer."); } else {
- Console.WriteLine("The value is " + obj.AsInt32()); }
- .
- CBORObject obj = CBORObject.FromInt64(99999);
- if (obj.IsIntegral && obj.AsNumber().CanFitInInt64()) {
- /* Not an Int64; handle the error */
- Console.WriteLine("Not a 64-bit integer."); } else {
- Console.WriteLine("The value is " + obj.AsInt64()); }
- .
- '/' KEY '/' KEY [...]where KEY represents a key into - the JSON object or its sub-objects in the hierarchy. For example, -
/foo/2/barmeans the same as -
obj['foo'][2]['bar']in JavaScript. If "~" and/or "/" - occurs in a key, it must be escaped with "~0" or "~1", - respectively, in a JSON pointer. JSON pointers also support the - special key "-" (as in "/foo/-") to indicate the end of an array, - but this method treats this key as an error since it refers to a - nonexistent item. Indices to arrays (such as 2 in the example) must - contain only basic digits 0 to 9 and no leading zeros. (Note that - RFC 6901 was published before JSON was extended to support - top-level values other than arrays and key-value - dictionaries.).
/* Generates a JSON string of 'mapObj' whose keys are in the order
- given
- in 'keys' . Only keys found in 'keys' will be written if they exist in
- 'mapObj'. */ private static string KeysToJSONMap(CBORObject mapObj,
- IList<CBORObject> keys) { if (mapObj == null) { throw new
- ArgumentNullException)nameof(mapObj));}
- if (keys == null) { throw new
- ArgumentNullException)nameof(keys));}
- if (obj.Type != CBORType.Map) {
- throw new ArgumentException("'obj' is not a map."); } StringBuilder
- builder = new StringBuilder(); var first = true; builder.Append("{");
- for (CBORObject key in keys) { if (mapObj.ContainsKey(key)) { if
- (!first) {builder.Append(", ");} var keyString=(key.CBORType ==
- CBORType.String) ? key.AsString() : key.ToJSONString();
- builder.Append(CBORObject.FromObject(keyString) .ToJSONString())
- .Append(":").Append(mapObj[key].ToJSONString()); first=false; } } return
- builder.Append("}").ToString(); }
- .
-
- stream.WriteByte(0x1e); /* RS */
- cborObject.WriteJSONTo(stream); /* JSON */
- stream.WriteByte(0x0a); /* LF */
-
-
- /* maximum supported JSON size in bytes*/
- var maxSize = 20000;
- using (var ms = new LimitedMemoryStream(maxSize)) {
- cborObject.WriteJSONTo(ms);
- var bytes = ms.ToArray();
- }
-
-
- /* maximum supported JSON size in bytes*/
- final int maxSize = 20000;
- ByteArrayOutputStream ba = new ByteArrayOutputStream();
- /* throws UnsupportedOperationException if too big*/
- cborObject.WriteJSONTo(new FilterOutputStream(ba) {
- private int size = 0;
- public void write(byte[] b, int off, int len) throws IOException {
- if (len>(maxSize-size)) {
- throw new UnsupportedOperationException();
- }
- size+=len; out.write(b, off, len);
- }
- public void write(byte b) throws IOException {
- if (size >= maxSize) {
- throw new UnsupportedOperationException();
- }
- size++; out.write(b);
- }
- });
- byte[] bytes = ba.toByteArray();
-
-
- var backing = new byte[20000]; /* maximum supported JSON size in
- bytes*/
- byte[] bytes1, bytes2;
- using (var ms = new MemoryStream(backing)) {
- /* throws NotSupportedException if too big*/
- cborObject.WriteJSONTo(ms);
- bytes1 = new byte[ms.Position];
- /* Copy serialized data if successful*/
- System.ArrayCopy(backing, 0, bytes1, 0, (int)ms.Position);
- /* Reset memory stream*/
- ms.Position = 0;
- cborObject2.WriteJSONTo(ms);
- bytes2 = new byte[ms.Position];
- /* Copy serialized data if successful*/
- System.ArrayCopy(backing, 0, bytes2, 0, (int)ms.Position);
- }
-
- /* array, length 3*/
- CBORObject.WriteValue(stream, 4, 3);
- /* item 1 */
- CBORObject.Write("hello world", stream);
- CBORObject.Write(25, stream); /* item 2*/
- CBORObject.Write(false, stream); /* item 3*/
- CBORObject.WriteValue(stream, 5, 2); /* map, 2
- pairs*/
- CBORObject.Write("number", stream); /* key 1 */
- CBORObject.Write(25, stream); /* value 1 */
- CBORObject.Write("string", stream); /* key 2*/
- CBORObject.Write("hello", stream); /* value 2*/
- string str = "hello world"; byte[] bytes =
- DataUtilities.GetUtf8Bytes(str, true); CBORObject.WriteValue(stream, 4,
- bytes.Length); stream.Write(bytes, 0, bytes.Length);
- .
- private static void WriteKeysToMap(CBORObject mapObj,
- IList<CBORObject> keys, Stream outputStream) {
- if (mapObj == null) {
- throw new ArgumentNullException(nameof(mapObj));}
- if (keys == null)
- {throw new ArgumentNullException(nameof(keys));}
- if (outputStream ==
- null) {throw new ArgumentNullException(nameof(outputStream));}
- if
- (obj.Type!=CBORType.Map) { throw new ArgumentException("'obj' is not a
- map."); } int keyCount = 0; for (CBORObject key in keys) { if
- (mapObj.ContainsKey(key)) { keyCount++; } }
- CBORObject.WriteValue(outputStream, 5, keyCount); for (CBORObject key in
- keys) { if (mapObj.ContainsKey(key)) { key.WriteTo(outputStream);
- mapObj[key].WriteTo(outputStream); } } }
- private static void WriteKeysToIndefMap(CBORObject mapObj,
- IList<CBORObject> keys, Stream outputStream) { if (mapObj == null)
- { throw new ArgumentNullException(nameof(mapObj));}
- if (keys == null)
- {throw new ArgumentNullException(nameof(keys));}
- if (outputStream ==
- null) {throw new ArgumentNullException(nameof(outputStream));}
- if
- (obj.Type!=CBORType.Map) { throw new ArgumentException("'obj' is not a
- map."); } outputStream.WriteByte((byte)0xBF); for (CBORObject key in
- keys) { if (mapObj.ContainsKey(key)) { key.WriteTo(outputStream);
- mapObj[key].WriteTo(outputStream); } }
- outputStream.WriteByte((byte)0xff); }
- private static void WriteToIndefArray(IList<object> list,
- Stream
- outputStream) { if (list == null) { throw new
- ArgumentNullException(nameof(list));}
- if (outputStream == null) {throw
- new ArgumentNullException(nameof(outputStream));}
- outputStream.WriteByte((byte)0x9f); for (object item in list) { new
- CBORObject(item).WriteTo(outputStream); }
- outputStream.WriteByte((byte)0xff); }
-
- /* maximum supported CBOR size in bytes*/
- var maxSize = 20000;
- using (var ms = new LimitedMemoryStream(maxSize)) {
- cborObject.WriteTo(ms);
- var bytes = ms.ToArray();
- }
-
-
- /* maximum supported CBOR size in bytes*/
- final int maxSize = 20000;
- ByteArrayOutputStream ba = new ByteArrayOutputStream();
- /* throws UnsupportedOperationException if too big*/
- cborObject.WriteTo(new FilterOutputStream(ba) {
- private int size = 0;
- public void write(byte[] b, int off, int len) throws IOException {
- if (len>(maxSize-size)) {
- throw new UnsupportedOperationException();
- }
- size+=len; out.write(b, off, len);
- }
- public void write(byte b) throws IOException {
- if (size >= maxSize) {
- throw new UnsupportedOperationException();
- }
- size++; out.write(b);
- }
- });
- byte[] bytes = ba.toByteArray();
-
-
- var backing = new byte[20000]; /* maximum supported CBOR size in
- bytes*/
- byte[] bytes1, bytes2;
- using (var ms = new MemoryStream(backing)) {
- /* throws NotSupportedException if too big*/
- cborObject.WriteTo(ms);
- bytes1 = new byte[ms.Position];
- /* Copy serialized data if successful*/
- System.ArrayCopy(backing, 0, bytes1, 0, (int)ms.Position);
- /* Reset memory stream*/
- ms.Position = 0;
- cborObject2.WriteTo(ms);
- bytes2 = new byte[ms.Position];
- /* Copy serialized data if successful*/
- System.ArrayCopy(backing, 0, bytes2, 0, (int)ms.Position);
- }
-
- [{"key":"value1","foo":"foovalue"},
- {"key":"value2","bar":"barvalue"}, {"baz":"bazvalue"}] If
- getPointersToKey is called on this object with a keyToFind called
- "key", we get the following Map as the return value:
- { "/0" => "value1", // "/0" points to {"foo":"foovalue"} "/1"
- => "value2" /* "/1" points to {"bar":"barvalue"} */ } and the
- JSON object will change to the following:
- [{"foo":"foovalue"}, {"bar":"barvalue"},
- {"baz","bazvalue"}].[{"key":"value1","foo":"foovalue"},
- {"key":"value2","bar":"barvalue"}, {"baz":"bazvalue"}] If
- getPointersToKey is called on this object with a keyToFind called
- "key", we get the following Map as the return value:
- { "/0" => "value1", // "/0" points to
- {"key":"value1","foo":"foovalue"} "/1" => "value2" // "/1" points
- to {"key":"value2","bar":"barvalue"} } and the JSON object
- will remain unchanged.
- for (var i = 0;i<str.Length; ++i) { int codePoint =
- DataUtilities.CodePointAt(str, i, 2); if (codePoint < 0) { break; /*
- Unpaired surrogate */ } Console.WriteLine("codePoint:"+codePoint); if
- (codePoint >= 0x10000) { i++; /* Supplementary code point */ } }
- .
- |C|BBB...BBB|AAAAAA...AAAAAA|
- /* After performing arithmetic operations, adjust
- /* the number to 5*/*/
- /**/
- digits after the decimal point number = number.Quantize(
- EInteger.FromInt32(-5), /* five digits after the decimal
- point*/
- EContext.ForPrecision(25) /* 25-digit
- precision);*/
- /* After performing arithmetic operations, adjust the number to 5
- digits
- after the decimal point */ number = number.Quantize(-5, /* five digits
- after the decimal point */EContext.ForPrecision(25) /* 25-digit
- precision*/);
- EInteger result = EInteger.FromString("5").Multiply(200);
- .
- EInteger result = EInteger.FromString("5").Multiply(200L);
- .
- /* After performing arithmetic operations, adjust
- /* the number to 5 /*
- */*/*/
- digits after the radix point number = number.Quantize(
- EInteger.FromInt32(-5), /* five digits after the radix
- point*/
- EContext.ForPrecision(25) /* 25-digit
- precision);*/
- /* After performing arithmetic operations, adjust
- /* the number to 5*/*/
- digits after the radix point number = number.Quantize(-5, /* five
- digits*/
- after the radix point EContext.ForPrecision(25) /* 25-digit
- precision);*/
- String str = efloat.ToShortestString(EContext.Binary64);
- EInteger result = EInteger.FromString("5").Multiply(200);
- .
- xx-x:mm example:/ww
- The following cases return false:
- x@y:/z /x/y/z example.xyz
- .
- http://example/z xx-x:mm example:/ww
- The following cases return false:
- x@y:/z /x/y/z example.xyz
- .
-