PeterO.Cbor.CBORObject
## PeterO.Cbor.CBORObject
public sealed class CBORObject : System.IComparable, System.IEquatable
Represents an object in Concise Binary Object Representation (CBOR) and contains methods for reading and writing CBOR data. CBOR is an Internet Standard and defined in RFC 8949.
Converting CBOR objects
There are many ways to get a CBOR object, including from bytes, objects, streams and JSON, as described below.
To and from byte arrays: The CBORObject.DecodeFromBytes method converts a byte array in CBOR format to a CBOR object. The EncodeToBytes method converts a CBOR object to its corresponding byte array in CBOR format.
To and from data streams: The CBORObject.Write methods write many kinds of objects to a data stream, including numbers, CBOR objects, strings, and arrays of numbers and strings. The CBORObject.Read method reads a CBOR object from a data stream.
To and from other objects: The CBORObject.FromObject
method converts many kinds of objects to a CBOR object, including numbers, strings, and arrays and maps of numbers and strings. Methods like AsNumber and AsString convert a CBOR object to different types of object. The CBORObject.ToObject
method converts a CBOR object to an object of a given type; for example, a CBOR array to a native List
(or ArrayList
in Java), or a CBOR integer to an int
or long
.
To and from JSON: This class also doubles as a reader and writer of JavaScript Object Notation (JSON). The CBORObject.FromJSONString method converts JSON in text string form to a CBOR object, and the ToJSONString method converts a CBOR object to a JSON string. (Note that the conversion from CBOR to JSON is not always without loss and may make it impossible to recover the original object when converting the JSON back to CBOR. See the ToJSONString documentation.) Likewise, ToJSONBytes and FromJSONBytes work with JSON in the form of byte arrays rather than text strings.
In addition, the CBORObject.WriteJSON method writes many kinds of objects as JSON to a data stream, including numbers, CBOR objects, strings, and arrays of numbers and strings. The CBORObject.Read method reads a CBOR object from a JSON data stream.
Comparison Considerations:
Instances of CBORObject should not be compared for equality using the “==” operator; it’s possible to create two CBOR objects with the same value but not the same reference. (The “==” operator might only check if each side of the operator is the same instance.)
This class’s natural ordering (under the CompareTo method) is consistent with the Equals method, meaning that two values that compare as equal under the CompareTo method are also equal under the Equals method; this is a change in version 4.0. Two otherwise equal objects with different tags are not treated as equal by both CompareTo and Equals. To strip the tags from a CBOR object before comparing, use the Untag
method.
Thread Safety:
Certain CBOR objects are immutable (their values can’t be changed), so they are inherently safe for use by multiple threads.
CBOR objects that are arrays, maps, and byte strings (whether or not they are tagged) are mutable, but this class doesn’t attempt to synchronize reads and writes to those objects by multiple threads, so those objects are not thread safe without such synchronization.
One kind of CBOR object is called a map, or a list of key-value pairs. Keys can be any kind of CBOR object, including numbers, strings, arrays, and maps. However, untagged text strings (which means GetTags returns an empty array and the Type property, or “getType()” in Java, returns TextString) are the most suitable to use as keys; other kinds of CBOR object are much better used as map values instead, keeping in mind that some of them are not thread safe without synchronizing reads and writes to them.
To find the type of a CBOR object, call its Type property (or “getType()” in Java). The return value can be Integer, FloatingPoint, Boolean, SimpleValue, or TextString for immutable CBOR objects, and Array, Map, or ByteString for mutable CBOR objects.
Nesting Depth:
The DecodeFromBytes and Read methods can only read objects with a limited maximum depth of arrays and maps nested within other arrays and maps. The code sets this maximum depth to 500 (allowing more than enough nesting for most purposes), but it’s possible that stack overflows in some runtimes might lower the effective maximum nesting depth. When the nesting depth goes above 500, the DecodeFromBytes and Read methods throw a CBORException.
The ReadJSON and FromJSONString methods currently have nesting depths of 1000.
Member Summary
[Abs()](#Abs)
- Deprecated: Instead, convert this object to a number (with .AsNumber()), and use that number’s .Abs() method.[Add(object)](#Add_object)
- Converts an object to a CBOR object and adds it to the end of this array.[Add(object, object)](#Add_object_object)
- Adds a new key and its value to this CBOR map, or adds the value if the key doesn’t exist.[Add(PeterO.Cbor.CBORObject)](#Add_PeterO_Cbor_CBORObject)
- Adds a new object to the end of this array.[Addition(PeterO.Cbor.CBORObject, PeterO.Cbor.CBORObject)](#Addition_PeterO_Cbor_CBORObject_PeterO_Cbor_CBORObject)
- Deprecated: Instead, convert both CBOR objects to numbers (with .AsNumber()), and use the first number’s .Add() method.[ApplyJSONPatch(PeterO.Cbor.CBORObject)](#ApplyJSONPatch_PeterO_Cbor_CBORObject)
- Returns a copy of this object after applying the operations in a JSON patch, in the form of a CBOR object.[AsBoolean()](#AsBoolean)
- Returns false if this object is a CBOR false, null, or undefined value (whether or not the object has tags); otherwise, true.[AsByte()](#AsByte)
- Deprecated: Instead, use .ToObject<byte>() in .NET or .ToObject(Byte.class) in Java.[AsDecimal()](#AsDecimal)
- Deprecated: Instead, use .ToObject<decimal>().[AsDouble()](#AsDouble)
- Converts this object to a 64-bit floating point number.[AsDoubleBits()](#AsDoubleBits)
- Converts this object to the bits of a 64-bit floating-point number if this CBOR object’s type is FloatingPoint.[AsDoubleValue()](#AsDoubleValue)
- Converts this object to a 64-bit floating-point number if this CBOR object’s type is FloatingPoint.[AsEDecimal()](#AsEDecimal)
- Deprecated: Instead, use .ToObject<PeterO.Numbers.EDecimal>() in .NET or .ToObject(com.upokecenter.numbers.EDecimal.class) in Java.[AsEFloat()](#AsEFloat)
- Deprecated: Instead, use .ToObject<PeterO.Numbers.EFloat>() in .NET or .ToObject(com.upokecenter.numbers.EFloat.class) in Java.[AsEInteger()](#AsEInteger)
- Deprecated: Instead, use .ToObject<PeterO.Numbers.EInteger>() in .NET or .ToObject(com.upokecenter.numbers.EInteger.class) in Java.[AsEIntegerValue()](#AsEIntegerValue)
- Converts this object to an arbitrary-precision integer if this CBOR object’s type is Integer.[AsERational()](#AsERational)
- Deprecated: Instead, use .ToObject<PeterO.Numbers.ERational>() in .NET or .ToObject(com.upokecenter.numbers.ERational.class) in Java.[AsInt16()](#AsInt16)
- Deprecated: Instead, use the following: (cbor.AsNumber().ToInt16Checked()), or .ToObject<short>() in .NET.[AsInt32()](#AsInt32)
- Converts this object to a 32-bit signed integer.[AsInt32Value()](#AsInt32Value)
- Converts this object to a 32-bit signed integer if this CBOR object’s type is Integer.[AsInt64()](#AsInt64)
- Deprecated: Instead, use the following: (cbor.AsNumber().ToInt64Checked()), or .ToObject<long>() in .NET.[AsInt64Value()](#AsInt64Value)
- Converts this object to a 64-bit signed integer if this CBOR object’s type is Integer.[AsNumber()](#AsNumber)
- Converts this object to a CBOR number.[AsSByte()](#AsSByte)
- Deprecated: Instead, use the following: (cbor.AsNumber().ToSByteChecked()), or .ToObject<sbyte>().[AsSingle()](#AsSingle)
- Converts this object to a 32-bit floating point number.[AsString()](#AsString)
- Gets the value of this object as a text string.[AsUInt16()](#AsUInt16)
- Deprecated: Instead, use the following: (cbor.AsNumber().ToUInt16Checked()), or .ToObject<ushort>().[AsUInt32()](#AsUInt32)
- Deprecated: Instead, use the following: (cbor.AsNumber().ToUInt32Checked()), or .ToObject<uint>().[AsUInt64()](#AsUInt64)
- Deprecated: Instead, use the following: (cbor.AsNumber().ToUInt64Checked()), or .ToObject<ulong>().[AtJSONPointer(string)](#AtJSONPointer_string)
- Gets the CBOR object referred to by a JSON Pointer according to RFC6901.[AtJSONPointer(string, PeterO.Cbor.CBORObject)](#AtJSONPointer_string_PeterO_Cbor_CBORObject)
- Gets the CBOR object referred to by a JSON Pointer according to RFC6901, or a default value if the operation fails.[CalcEncodedSize()](#CalcEncodedSize)
- Calculates the number of bytes this CBOR object takes when serialized as a byte array using the EncodeToBytes() method.[CanFitInDouble()](#CanFitInDouble)
- Deprecated: Instead, use the following: (cbor.IsNumber && cbor.AsNumber().CanFitInDouble()).[CanFitInInt32()](#CanFitInInt32)
- Deprecated: Instead, use .CanValueFitInInt32(), if the application allows only CBOR integers, or (cbor.IsNumber &&cbor.AsNumber().CanFitInInt32()), if the application allows any CBOR object convertible to an integer.[CanFitInInt64()](#CanFitInInt64)
- Deprecated: Instead, use CanValueFitInInt64(), if the application allows only CBOR integers, or (cbor.IsNumber &&cbor.AsNumber().CanFitInInt64()), if the application allows any CBOR object convertible to an integer.[CanFitInSingle()](#CanFitInSingle)
- Deprecated: Instead, use the following: (cbor.IsNumber && cbor.AsNumber().CanFitInSingle()).[CanTruncatedIntFitInInt32()](#CanTruncatedIntFitInInt32)
- Deprecated: Instead, use the following: (cbor.CanValueFitInInt32() if only integers of any tag are allowed, or (cbor.IsNumber && cbor.AsNumber().CanTruncatedIntFitInInt32()).[CanTruncatedIntFitInInt64()](#CanTruncatedIntFitInInt64)
- Deprecated: Instead, use the following: (cbor.CanValueFitInInt64() if only integers of any tag are allowed, or (cbor.IsNumber && cbor.AsNumber().CanTruncatedIntFitInInt64()).[CanValueFitInInt32()](#CanValueFitInInt32)
- Returns whether this CBOR object stores an integer (CBORType.[CanValueFitInInt64()](#CanValueFitInInt64)
- Returns whether this CBOR object stores an integer (CBORType.[Clear()](#Clear)
- Removes all items from this CBOR array or all keys and values from this CBOR map.[CompareTo(PeterO.Cbor.CBORObject)](#CompareTo_PeterO_Cbor_CBORObject)
- Compares two CBOR objects.[CompareToIgnoreTags(PeterO.Cbor.CBORObject)](#CompareToIgnoreTags_PeterO_Cbor_CBORObject)
- Compares this object and another CBOR object, ignoring the tags they have, if any.[ContainsKey(object)](#ContainsKey_object)
- Determines whether a value of the given key exists in this object.[ContainsKey(PeterO.Cbor.CBORObject)](#ContainsKey_PeterO_Cbor_CBORObject)
- Determines whether a value of the given key exists in this object.[ContainsKey(string)](#ContainsKey_string)
- Determines whether a value of the given key exists in this object.[Count](#Count)
- Gets the number of keys in this map, or the number of items in this array, or 0 if this item is neither an array nor a map.[DecodeFromBytes(byte[])](#DecodeFromBytes_byte)
- Generates a CBOR object from an array of CBOR-encoded bytes.[DecodeFromBytes(byte[], PeterO.Cbor.CBOREncodeOptions)](#DecodeFromBytes_byte_PeterO_Cbor_CBOREncodeOptions)
- Generates a CBOR object from an array of CBOR-encoded bytes, using the given CBOREncodeOptions object to control the decoding process.[DecodeObjectFromBytes(byte[], PeterO.Cbor.CBOREncodeOptions, System.Type)](#DecodeObjectFromBytes_byte_PeterO_Cbor_CBOREncodeOptions_System_Type)
- Generates an object of an arbitrary type from an array of CBOR-encoded bytes, using the given CBOREncodeOptions object to control the decoding process.[DecodeObjectFromBytes(byte[], PeterO.Cbor.CBOREncodeOptions, System.Type, PeterO.Cbor.CBORTypeMapper, PeterO.Cbor.PODOptions)](#DecodeObjectFromBytes_byte_PeterO_Cbor_CBOREncodeOptions_System_Type_PeterO_Cbor_CBORTypeMapper_PeterO_Cbor_PODOptions)
- Generates an object of an arbitrary type from an array of CBOR-encoded bytes, using the given CBOREncodeOptions object to control the decoding process.[DecodeObjectFromBytes(byte[], System.Type)](#DecodeObjectFromBytes_byte_System_Type)
- Generates an object of an arbitrary type from an array of CBOR-encoded bytes.[DecodeObjectFromBytes(byte[], System.Type, PeterO.Cbor.CBORTypeMapper, PeterO.Cbor.PODOptions)](#DecodeObjectFromBytes_byte_System_Type_PeterO_Cbor_CBORTypeMapper_PeterO_Cbor_PODOptions)
- Generates an object of an arbitrary type from an array of CBOR-encoded bytes.[DecodeObjectFromBytes<T>(byte[])](#DecodeObjectFromBytes_T_byte)
- Generates an object of an arbitrary type from an array of CBOR-encoded bytes.[DecodeObjectFromBytes<T>(byte[], PeterO.Cbor.CBOREncodeOptions)](#DecodeObjectFromBytes_T_byte_PeterO_Cbor_CBOREncodeOptions)
- Generates an object of an arbitrary type from an array of CBOR-encoded bytes, using the given CBOREncodeOptions object to control the decoding process.[DecodeObjectFromBytes<T>(byte[], PeterO.Cbor.CBOREncodeOptions, PeterO.Cbor.CBORTypeMapper, PeterO.Cbor.PODOptions)](#DecodeObjectFromBytes_T_byte_PeterO_Cbor_CBOREncodeOptions_PeterO_Cbor_CBORTypeMapper_PeterO_Cbor_PODOptions)
- Generates an object of an arbitrary type from an array of CBOR-encoded bytes, using the given CBOREncodeOptions object to control the decoding process.[DecodeObjectFromBytes<T>(byte[], PeterO.Cbor.CBORTypeMapper, PeterO.Cbor.PODOptions)](#DecodeObjectFromBytes_T_byte_PeterO_Cbor_CBORTypeMapper_PeterO_Cbor_PODOptions)
- Generates an object of an arbitrary type from an array of CBOR-encoded bytes.[DecodeSequenceFromBytes(byte[])](#DecodeSequenceFromBytes_byte)
- Generates a sequence of CBOR objects from an array of CBOR-encoded bytes.[DecodeSequenceFromBytes(byte[], PeterO.Cbor.CBOREncodeOptions)](#DecodeSequenceFromBytes_byte_PeterO_Cbor_CBOREncodeOptions)
- Generates a sequence of CBOR objects from an array of CBOR-encoded bytes.[Divide(PeterO.Cbor.CBORObject, PeterO.Cbor.CBORObject)](#Divide_PeterO_Cbor_CBORObject_PeterO_Cbor_CBORObject)
- Deprecated: Instead, convert both CBOR objects to numbers (with .AsNumber()), and use the first number’s .Divide() method.[EncodeToBytes()](#EncodeToBytes)
- Writes the binary representation of this CBOR object and returns a byte array of that representation.[EncodeToBytes(PeterO.Cbor.CBOREncodeOptions)](#EncodeToBytes_PeterO_Cbor_CBOREncodeOptions)
- Writes the binary representation of this CBOR object and returns a byte array of that representation, using the specified options for encoding the object to CBOR format.[Entries](#Entries)
- Gets a collection of the key/value pairs stored in this CBOR object, if it’s a map.[Equals(object)](#Equals_object)
- Determines whether this object and another object are equal and have the same type.[Equals(PeterO.Cbor.CBORObject)](#Equals_PeterO_Cbor_CBORObject)
- Compares the equality of two CBOR objects.[public static readonly PeterO.Cbor.CBORObject False;](#False)
- Represents the value false.[FromFloatingPointBits(long, int)](#FromFloatingPointBits_long_int)
- Generates a CBOR object from a floating-point number represented by its bits.[FromJSONBytes(byte[])](#FromJSONBytes_byte)
- Generates a CBOR object from a byte array in JavaScript Object Notation (JSON) format.[FromJSONBytes(byte[], int, int)](#FromJSONBytes_byte_int_int)
- Generates a CBOR object from a byte array in JavaScript Object Notation (JSON) format.[FromJSONBytes(byte[], int, int, PeterO.Cbor.JSONOptions)](#FromJSONBytes_byte_int_int_PeterO_Cbor_JSONOptions)
- Generates a CBOR object from a byte array in JavaScript Object Notation (JSON) format, using the specified options to control the decoding process.[FromJSONBytes(byte[], PeterO.Cbor.JSONOptions)](#FromJSONBytes_byte_PeterO_Cbor_JSONOptions)
- Generates a CBOR object from a byte array in JavaScript Object Notation (JSON) format, using the specified options to control the decoding process.[FromJSONSequenceBytes(byte[])](#FromJSONSequenceBytes_byte)
- Generates a list of CBOR objects from an array of bytes in JavaScript Object Notation (JSON) text sequence format (RFC 7464).[FromJSONSequenceBytes(byte[], PeterO.Cbor.JSONOptions)](#FromJSONSequenceBytes_byte_PeterO_Cbor_JSONOptions)
- Generates a list of CBOR objects from an array of bytes in JavaScript Object Notation (JSON) text sequence format (RFC 7464), using the specified options to control the decoding process.[FromJSONString(string)](#FromJSONString_string)
- Generates a CBOR object from a text string in JavaScript Object Notation (JSON) format.[FromJSONString(string, int, int)](#FromJSONString_string_int_int)
- Generates a CBOR object from a text string in JavaScript Object Notation (JSON) format.[FromJSONString(string, int, int, PeterO.Cbor.JSONOptions)](#FromJSONString_string_int_int_PeterO_Cbor_JSONOptions)
- Generates a CBOR object from a text string in JavaScript Object Notation (JSON) format, using the specified options to control the decoding process.[FromJSONString(string, PeterO.Cbor.CBOREncodeOptions)](#FromJSONString_string_PeterO_Cbor_CBOREncodeOptions)
- Deprecated: Instead, use .FromJSONString(str, new JSONOptions("allowduplicatekeys=true")) or .FromJSONString(str, new JSONOptions("allowduplicatekeys=false")), as appropriate.[FromJSONString(string, PeterO.Cbor.JSONOptions)](#FromJSONString_string_PeterO_Cbor_JSONOptions)
- Generates a CBOR object from a text string in JavaScript Object Notation (JSON) format, using the specified options to control the decoding process.[FromObject(bool)](#FromObject_bool)
- Returns the CBOR true value or false value, depending on “value”.[FromObject(byte[])](#FromObject_byte)
- Generates a CBOR object from a byte (0 to 255). Generates a CBOR object from an array of 8-bit bytes; the byte array is copied to a new byte array in this process.[FromObject(decimal)](#FromObject_decimal)
- Converts a.[FromObject(double)](#FromObject_double)
- Generates a CBOR object from a 64-bit floating-point number.[FromObject(float)](#FromObject_float)
- Generates a CBOR object from a 32-bit floating-point number.[FromObject(int[])](#FromObject_int)
- Generates a CBOR object from a 32-bit signed integer. Generates a CBOR object from an array of 32-bit integers.[FromObject(long[])](#FromObject_long)
- Generates a CBOR object from a 64-bit signed integer. Generates a CBOR object from an array of 64-bit integers.[FromObject(object)](#FromObject_object)
- Generates a CBORObject from an arbitrary object.[FromObject(object, PeterO.Cbor.CBORTypeMapper)](#FromObject_object_PeterO_Cbor_CBORTypeMapper)
- Generates a CBORObject from an arbitrary object.[FromObject(object, PeterO.Cbor.CBORTypeMapper, PeterO.Cbor.PODOptions)](#FromObject_object_PeterO_Cbor_CBORTypeMapper_PeterO_Cbor_PODOptions)
- Generates a CBORObject from an arbitrary object, using the given options to control how certain objects are converted to CBOR objects.[FromObject(object, PeterO.Cbor.PODOptions)](#FromObject_object_PeterO_Cbor_PODOptions)
- Generates a CBORObject from an arbitrary object.[FromObject(PeterO.Cbor.CBORObject[])](#FromObject_PeterO_Cbor_CBORObject)
- Generates a CBOR object from a CBOR object. Generates a CBOR object from an array of CBOR objects.[FromObject(PeterO.Numbers.EDecimal)](#FromObject_PeterO_Numbers_EDecimal)
- Generates a CBOR object from a decimal number.[FromObject(PeterO.Numbers.EFloat)](#FromObject_PeterO_Numbers_EFloat)
- Generates a CBOR object from an arbitrary-precision binary floating-point number.[FromObject(PeterO.Numbers.EInteger)](#FromObject_PeterO_Numbers_EInteger)
- Generates a CBOR object from an arbitrary-precision integer.[FromObject(PeterO.Numbers.ERational)](#FromObject_PeterO_Numbers_ERational)
- Generates a CBOR object from an arbitrary-precision rational number.[FromObject(sbyte)](#FromObject_sbyte)
- Converts a signed 8-bit integer to a CBOR object.[FromObject(short)](#FromObject_short)
- Generates a CBOR object from a 16-bit signed integer.[FromObject(string)](#FromObject_string)
- Generates a CBOR object from a text string.[FromObject(uint)](#FromObject_uint)
- Converts a 32-bit unsigned integer to a CBOR object.[FromObject(ulong)](#FromObject_ulong)
- Converts a 64-bit unsigned integer to a CBOR object.[FromObject(ushort)](#FromObject_ushort)
- Converts a 16-bit unsigned integer to a CBOR object.[FromObjectAndTag(object, int)](#FromObjectAndTag_object_int)
- Generates a CBOR object from an arbitrary object and gives the resulting object a tag in addition to its existing tags (the new tag is made the outermost tag).[FromObjectAndTag(object, PeterO.Numbers.EInteger)](#FromObjectAndTag_object_PeterO_Numbers_EInteger)
- Generates a CBOR object from an arbitrary object and gives the resulting object a tag in addition to its existing tags (the new tag is made the outermost tag).[FromObjectAndTag(object, ulong)](#FromObjectAndTag_object_ulong)
- Generates a CBOR object from an arbitrary object and gives the resulting object a tag.[FromSimpleValue(int)](#FromSimpleValue_int)
- Creates a CBOR object from a simple value number.[GetAllTags()](#GetAllTags)
- Gets a list of all tags, from outermost to innermost.[GetByteString()](#GetByteString)
- Gets the backing byte array used in this CBOR object, if this object is a byte string, without copying the data to a new byte array.[GetHashCode()](#GetHashCode)
- Calculates the hash code of this object.[GetOrDefault(object, PeterO.Cbor.CBORObject)](#GetOrDefault_object_PeterO_Cbor_CBORObject)
- Gets the value of a CBOR object by integer index in this array or by CBOR object key in this map, or a default value if that value is not found.[HasMostInnerTag(int)](#HasMostInnerTag_int)
- Returns whether this object has an innermost tag and that tag is of the given number.[HasMostInnerTag(PeterO.Numbers.EInteger)](#HasMostInnerTag_PeterO_Numbers_EInteger)
- Returns whether this object has an innermost tag and that tag is of the given number, expressed as an arbitrary-precision number.[HasMostOuterTag(int)](#HasMostOuterTag_int)
- Returns whether this object has an outermost tag and that tag is of the given number.[HasMostOuterTag(PeterO.Numbers.EInteger)](#HasMostOuterTag_PeterO_Numbers_EInteger)
- Returns whether this object has an outermost tag and that tag is of the given number.[HasOneTag()](#HasOneTag)
- Returns whether this object has only one tag.[HasOneTag(int)](#HasOneTag_int)
- Returns whether this object has only one tag and that tag is the given number.[HasOneTag(PeterO.Numbers.EInteger)](#HasOneTag_PeterO_Numbers_EInteger)
- Returns whether this object has only one tag and that tag is the given number, expressed as an arbitrary-precision integer.[HasTag(int)](#HasTag_int)
- Returns whether this object has a tag of the given number.[HasTag(PeterO.Numbers.EInteger)](#HasTag_PeterO_Numbers_EInteger)
- Returns whether this object has a tag of the given number.[Insert(int, object)](#Insert_int_object)
- Inserts an object at the specified position in this CBOR array.[IsFalse](#IsFalse)
- Gets a value indicating whether this value is a CBOR false value, whether tagged or not.[IsFinite](#IsFinite)
- Deprecated: Instead, use the following: (cbor.IsNumber && cbor.AsNumber().IsFinite()).[IsInfinity()](#IsInfinity)
- Deprecated: Instead, use the following: (cbor.IsNumber && cbor.AsNumber().IsInfinity()).[IsIntegral](#IsIntegral)
- Deprecated: Instead, use the following: (cbor.IsNumber && cbor.AsNumber().IsInteger()).[IsNaN()](#IsNaN)
- Deprecated: Instead, use the following: (cbor.IsNumber && cbor.AsNumber().IsNaN()).[IsNegative](#IsNegative)
- Deprecated: Instead, use (cbor.IsNumber() && cbor.AsNumber().IsNegative()).[IsNegativeInfinity()](#IsNegativeInfinity)
- Deprecated: Instead, use the following: (cbor.IsNumber && cbor.AsNumber().IsNegativeInfinity()).[IsNull](#IsNull)
- Gets a value indicating whether this CBOR object is a CBOR null value, whether tagged or not.[IsNumber](#IsNumber)
- Gets a value indicating whether this CBOR object stores a number (including infinity or a not-a-number or NaN value).[IsPositiveInfinity()](#IsPositiveInfinity)
- Deprecated: Instead, use the following: (cbor.IsNumber && cbor.AsNumber().IsPositiveInfinity()).[IsTagged](#IsTagged)
- Gets a value indicating whether this data item has at least one tag.[IsTrue](#IsTrue)
- Gets a value indicating whether this value is a CBOR true value, whether tagged or not.[IsUndefined](#IsUndefined)
- Gets a value indicating whether this value is a CBOR undefined value, whether tagged or not.[IsZero](#IsZero)
- Deprecated: Instead, use the following: (cbor.IsNumber && cbor.AsNumber().IsZero()).[Keys](#Keys)
- Gets a collection of the keys of this CBOR object.[MostInnerTag](#MostInnerTag)
- Gets the last defined tag for this CBOR data item, or -1 if the item is untagged.[MostOuterTag](#MostOuterTag)
- Gets the outermost tag for this CBOR data item, or -1 if the item is untagged.[Multiply(PeterO.Cbor.CBORObject, PeterO.Cbor.CBORObject)](#Multiply_PeterO_Cbor_CBORObject_PeterO_Cbor_CBORObject)
- Deprecated: Instead, convert both CBOR objects to numbers (with .AsNumber()), and use the first number’s .Multiply() method.[public static readonly PeterO.Cbor.CBORObject NaN;](#NaN)
- A not-a-number value.[Negate()](#Negate)
- Deprecated: Instead, convert this object to a number (with .AsNumber()), and use that number’s .Negate() method.[public static readonly PeterO.Cbor.CBORObject NegativeInfinity;](#NegativeInfinity)
- The value negative infinity.[NewArray()](#NewArray)
- Creates a new empty CBOR array.[NewMap()](#NewMap)
- Creates a new empty CBOR map that stores its keys in an undefined order.[NewOrderedMap()](#NewOrderedMap)
- Creates a new empty CBOR map that ensures that keys are stored in the order in which they are first inserted.[public static readonly PeterO.Cbor.CBORObject Null;](#Null)
- Represents the value null.[PeterO.Cbor.CBORObject operator +(PeterO.Cbor.CBORObject, PeterO.Cbor.CBORObject)](#op_Addition)
- Deprecated: May be removed in the next major version. Consider converting the objects to CBOR numbers and performing the operation there.[PeterO.Cbor.CBORObject operator /(PeterO.Cbor.CBORObject, PeterO.Cbor.CBORObject)](#op_Division)
- Deprecated: May be removed in the next major version. Consider converting the objects to CBOR numbers and performing the operation there.[bool operator >(PeterO.Cbor.CBORObject, PeterO.Cbor.CBORObject)](#op_GreaterThan)
- Returns whether one object’s value is greater than another’s.[bool operator >=(PeterO.Cbor.CBORObject, PeterO.Cbor.CBORObject)](#op_GreaterThanOrEqual)
- Returns whether one object’s value is at least another’s.[bool operator <(PeterO.Cbor.CBORObject, PeterO.Cbor.CBORObject)](#op_LessThan)
- Returns whether one object’s value is less than another’s.[bool operator <=(PeterO.Cbor.CBORObject, PeterO.Cbor.CBORObject)](#op_LessThanOrEqual)
- Returns whether one object’s value is up to another’s.[PeterO.Cbor.CBORObject operator %(PeterO.Cbor.CBORObject, PeterO.Cbor.CBORObject)](#op_Modulus)
- Deprecated: May be removed in the next major version. Consider converting the objects to CBOR numbers and performing the operation there.[PeterO.Cbor.CBORObject operator *(PeterO.Cbor.CBORObject, PeterO.Cbor.CBORObject)](#op_Multiply)
- Deprecated: May be removed in the next major version. Consider converting the objects to CBOR numbers and performing the operation there.[PeterO.Cbor.CBORObject operator -(PeterO.Cbor.CBORObject, PeterO.Cbor.CBORObject)](#op_Subtraction)
- Deprecated: May be removed in the next major version. Consider converting the objects to CBOR numbers and performing the operation there.[public static readonly PeterO.Cbor.CBORObject PositiveInfinity;](#PositiveInfinity)
- The value positive infinity.[Read(System.IO.Stream)](#Read_System_IO_Stream)
- Reads an object in CBOR format from a data stream.[Read(System.IO.Stream, PeterO.Cbor.CBOREncodeOptions)](#Read_System_IO_Stream_PeterO_Cbor_CBOREncodeOptions)
- Reads an object in CBOR format from a data stream, using the specified options to control the decoding process.[ReadJSON(System.IO.Stream)](#ReadJSON_System_IO_Stream)
- Generates a CBOR object from a data stream in JavaScript Object Notation (JSON) format.[ReadJSON(System.IO.Stream, PeterO.Cbor.CBOREncodeOptions)](#ReadJSON_System_IO_Stream_PeterO_Cbor_CBOREncodeOptions)
- Deprecated: Instead, use .ReadJSON(stream, new JSONOptions("allowduplicatekeys=true")) or .ReadJSON(stream, new JSONOptions("allowduplicatekeys=false")), as appropriate.[ReadJSON(System.IO.Stream, PeterO.Cbor.JSONOptions)](#ReadJSON_System_IO_Stream_PeterO_Cbor_JSONOptions)
- Generates a CBOR object from a data stream in JavaScript Object Notation (JSON) format, using the specified options to control the decoding process.[ReadJSONSequence(System.IO.Stream)](#ReadJSONSequence_System_IO_Stream)
- Generates a list of CBOR objects from a data stream in JavaScript Object Notation (JSON) text sequence format (RFC 7464).[ReadJSONSequence(System.IO.Stream, PeterO.Cbor.JSONOptions)](#ReadJSONSequence_System_IO_Stream_PeterO_Cbor_JSONOptions)
- Generates a list of CBOR objects from a data stream in JavaScript Object Notation (JSON) text sequence format (RFC 7464).[ReadSequence(System.IO.Stream)](#ReadSequence_System_IO_Stream)
- Reads a sequence of objects in CBOR format from a data stream.[ReadSequence(System.IO.Stream, PeterO.Cbor.CBOREncodeOptions)](#ReadSequence_System_IO_Stream_PeterO_Cbor_CBOREncodeOptions)
- Reads a sequence of objects in CBOR format from a data stream.[Remainder(PeterO.Cbor.CBORObject, PeterO.Cbor.CBORObject)](#Remainder_PeterO_Cbor_CBORObject_PeterO_Cbor_CBORObject)
- Deprecated: Instead, convert both CBOR objects to numbers (with .AsNumber()), and use the first number’s .Remainder() method.[Remove(object)](#Remove_object)
- If this object is an array, removes the first instance of the specified item (once converted to a CBOR object) from the array.[Remove(PeterO.Cbor.CBORObject)](#Remove_PeterO_Cbor_CBORObject)
- If this object is an array, removes the first instance of the specified item from the array.[RemoveAt(int)](#RemoveAt_int)
- Removes the item at the given index of this CBOR array.[Set(object, object)](#Set_object_object)
- Maps an object to a key in this CBOR map, or adds the value if the key doesn’t exist.[Sign](#Sign)
- Deprecated: Instead, convert this object to a number with .AsNumber(), and use the Sign property in .NET or the signum method in Java. Either will treat not-a-number (NaN) values differently than here.[SimpleValue](#SimpleValue)
- Gets the simple value ID of this CBOR object, or -1 if the object is not a simple value.[Subtract(PeterO.Cbor.CBORObject, PeterO.Cbor.CBORObject)](#Subtract_PeterO_Cbor_CBORObject_PeterO_Cbor_CBORObject)
- Deprecated: Instead, convert both CBOR objects to numbers (with .AsNumber()), and use the first number’s .Subtract() method.[TagCount](#TagCount)
- Gets the number of tags this object has.[this[int]](#this_int)
- Gets the value of a CBOR object by integer index in this array or by integer key in this map.[this[PeterO.Cbor.CBORObject]](#this_PeterO_Cbor_CBORObject)
- Gets the value of a CBOR object by integer index in this array or by CBOR object key in this map.[this[string]](#this_string)
- Gets the value of a CBOR object in this map, using a string as the key.[ToJSONBytes()](#ToJSONBytes)
- Converts this object to a byte array in JavaScript Object Notation (JSON) format.[ToJSONBytes(PeterO.Cbor.JSONOptions)](#ToJSONBytes_PeterO_Cbor_JSONOptions)
- Converts this object to a byte array in JavaScript Object Notation (JSON) format.[ToJSONString()](#ToJSONString)
- Converts this object to a text string in JavaScript Object Notation (JSON) format.[ToJSONString(PeterO.Cbor.JSONOptions)](#ToJSONString_PeterO_Cbor_JSONOptions)
- Converts this object to a text string in JavaScript Object Notation (JSON) format, using the specified options to control the encoding process.[ToObject(System.Type)](#ToObject_System_Type)
- Converts this CBOR object to an object of an arbitrary type.[ToObject(System.Type, PeterO.Cbor.CBORTypeMapper)](#ToObject_System_Type_PeterO_Cbor_CBORTypeMapper)
- Converts this CBOR object to an object of an arbitrary type.[ToObject(System.Type, PeterO.Cbor.CBORTypeMapper, PeterO.Cbor.PODOptions)](#ToObject_System_Type_PeterO_Cbor_CBORTypeMapper_PeterO_Cbor_PODOptions)
- Converts this CBOR object to an object of an arbitrary type.[ToObject(System.Type, PeterO.Cbor.PODOptions)](#ToObject_System_Type_PeterO_Cbor_PODOptions)
- Converts this CBOR object to an object of an arbitrary type.[ToObject<T>()](#ToObject_T)
- Converts this CBOR object to an object of an arbitrary type.[ToObject<T>(PeterO.Cbor.CBORTypeMapper)](#ToObject_T_PeterO_Cbor_CBORTypeMapper)
- Converts this CBOR object to an object of an arbitrary type.[ToObject<T>(PeterO.Cbor.CBORTypeMapper, PeterO.Cbor.PODOptions)](#ToObject_T_PeterO_Cbor_CBORTypeMapper_PeterO_Cbor_PODOptions)
- Converts this CBOR object to an object of an arbitrary type.[ToObject<T>(PeterO.Cbor.PODOptions)](#ToObject_T_PeterO_Cbor_PODOptions)
- Converts this CBOR object to an object of an arbitrary type.[ToString()](#ToString)
- Returns this CBOR object in a text form intended to be read by humans.[public static readonly PeterO.Cbor.CBORObject True;](#True)
- Represents the value true.[Type](#Type)
- Gets the general data type of this CBOR object.[public static readonly PeterO.Cbor.CBORObject Undefined;](#Undefined)
- Represents the value undefined.[Untag()](#Untag)
- Gets an object with the same value as this one but without the tags it has, if any.[UntagOne()](#UntagOne)
- Gets an object with the same value as this one but without this object’s outermost tag, if any.[Values](#Values)
- Gets a collection of the values of this CBOR object, if it’s a map or an array.[WithTag(int)](#WithTag_int)
- Generates a CBOR object from an arbitrary object and gives the resulting object a tag in addition to its existing tags (the new tag is made the outermost tag).[WithTag(PeterO.Numbers.EInteger)](#WithTag_PeterO_Numbers_EInteger)
- Generates a CBOR object from this one, but gives the resulting object a tag in addition to its existing tags (the new tag is made the outermost tag).[WithTag(ulong)](#WithTag_ulong)
- Generates a CBOR object from this one, but gives the resulting object a tag in addition to its existing tags (the new tag is made the outermost tag).[Write(bool, System.IO.Stream)](#Write_bool_System_IO_Stream)
- Writes a Boolean value in CBOR format to a data stream.[Write(byte, System.IO.Stream)](#Write_byte_System_IO_Stream)
- Writes a byte (0 to 255) in CBOR format to a data stream.[Write(double, System.IO.Stream)](#Write_double_System_IO_Stream)
- Writes a 64-bit floating-point number in CBOR format to a data stream.[Write(float, System.IO.Stream)](#Write_float_System_IO_Stream)
- Writes a 32-bit floating-point number in CBOR format to a data stream.[Write(int, System.IO.Stream)](#Write_int_System_IO_Stream)
- Writes a 32-bit signed integer in CBOR format to a data stream.[Write(long, System.IO.Stream)](#Write_long_System_IO_Stream)
- Writes a 64-bit signed integer in CBOR format to a data stream.[Write(object, System.IO.Stream)](#Write_object_System_IO_Stream)
- Writes a CBOR object to a CBOR data stream.[Write(object, System.IO.Stream, PeterO.Cbor.CBOREncodeOptions)](#Write_object_System_IO_Stream_PeterO_Cbor_CBOREncodeOptions)
- Writes an arbitrary object to a CBOR data stream, using the specified options for controlling how the object is encoded to CBOR data format.[Write(PeterO.Cbor.CBORObject, System.IO.Stream)](#Write_PeterO_Cbor_CBORObject_System_IO_Stream)
- Writes a CBOR object to a CBOR data stream.[Write(PeterO.Numbers.EDecimal, System.IO.Stream)](#Write_PeterO_Numbers_EDecimal_System_IO_Stream)
- Writes a decimal floating-point number in CBOR format to a data stream, as though it were converted to a CBOR object via CBORObject.[Write(PeterO.Numbers.EFloat, System.IO.Stream)](#Write_PeterO_Numbers_EFloat_System_IO_Stream)
- Writes a binary floating-point number in CBOR format to a data stream, as though it were converted to a CBOR object via CBORObject.[Write(PeterO.Numbers.EInteger, System.IO.Stream)](#Write_PeterO_Numbers_EInteger_System_IO_Stream)
- Writes a arbitrary-precision integer in CBOR format to a data stream.[Write(PeterO.Numbers.ERational, System.IO.Stream)](#Write_PeterO_Numbers_ERational_System_IO_Stream)
- Writes a rational number in CBOR format to a data stream, as though it were converted to a CBOR object via CBORObject.[Write(sbyte, System.IO.Stream)](#Write_sbyte_System_IO_Stream)
- Writes an 8-bit signed integer in CBOR format to a data stream.[Write(short, System.IO.Stream)](#Write_short_System_IO_Stream)
- Writes a 16-bit signed integer in CBOR format to a data stream.[Write(string, System.IO.Stream)](#Write_string_System_IO_Stream)
- Writes a text string in CBOR format to a data stream.[Write(string, System.IO.Stream, PeterO.Cbor.CBOREncodeOptions)](#Write_string_System_IO_Stream_PeterO_Cbor_CBOREncodeOptions)
- Writes a text string in CBOR format to a data stream, using the given options to control the encoding process.[Write(uint, System.IO.Stream)](#Write_uint_System_IO_Stream)
- Writes a 32-bit unsigned integer in CBOR format to a data stream.[Write(ulong, System.IO.Stream)](#Write_ulong_System_IO_Stream)
- Writes a 64-bit unsigned integer in CBOR format to a data stream.[Write(ushort, System.IO.Stream)](#Write_ushort_System_IO_Stream)
- Writes a 16-bit unsigned integer in CBOR format to a data stream.[WriteFloatingPointBits(System.IO.Stream, long, int)](#WriteFloatingPointBits_System_IO_Stream_long_int)
- Writes the bits of a floating-point number in CBOR format to a data stream.[WriteFloatingPointBits(System.IO.Stream, long, int, bool)](#WriteFloatingPointBits_System_IO_Stream_long_int_bool)
- Writes the bits of a floating-point number in CBOR format to a data stream.[WriteFloatingPointValue(System.IO.Stream, double, int)](#WriteFloatingPointValue_System_IO_Stream_double_int)
- Writes a 64-bit binary floating-point number in CBOR format to a data stream, either in its 64-bit form, or its rounded 32-bit or 16-bit equivalent.[WriteFloatingPointValue(System.IO.Stream, float, int)](#WriteFloatingPointValue_System_IO_Stream_float_int)
- Writes a 32-bit binary floating-point number in CBOR format to a data stream, either in its 64- or 32-bit form, or its rounded 16-bit equivalent.[WriteJSON(object, System.IO.Stream)](#WriteJSON_object_System_IO_Stream)
- Converts an arbitrary object to a text string in JavaScript Object Notation (JSON) format, as in the ToJSONString method, and writes that string to a data stream in UTF-8.[WriteJSONTo(System.IO.Stream)](#WriteJSONTo_System_IO_Stream)
- Converts this object to a text string in JavaScript Object Notation (JSON) format, as in the ToJSONString method, and writes that string to a data stream in UTF-8.[WriteJSONTo(System.IO.Stream, PeterO.Cbor.JSONOptions)](#WriteJSONTo_System_IO_Stream_PeterO_Cbor_JSONOptions)
- Converts this object to a text string in JavaScript Object Notation (JSON) format, as in the ToJSONString method, and writes that string to a data stream in UTF-8, using the given JSON options to control the encoding process.[WriteTo(System.IO.Stream)](#WriteTo_System_IO_Stream)
- Writes this CBOR object to a data stream.[WriteTo(System.IO.Stream, PeterO.Cbor.CBOREncodeOptions)](#WriteTo_System_IO_Stream_PeterO_Cbor_CBOREncodeOptions)
- Writes this CBOR object to a data stream, using the specified options for encoding the data to CBOR format.[WriteValue(System.IO.Stream, int, int)](#WriteValue_System_IO_Stream_int_int)
- Writes a CBOR major type number and an integer 0 or greater associated with it to a data stream, where that integer is passed to this method as a 32-bit signed integer.[WriteValue(System.IO.Stream, int, long)](#WriteValue_System_IO_Stream_int_long)
- Writes a CBOR major type number and an integer 0 or greater associated with it to a data stream, where that integer is passed to this method as a 64-bit signed integer.[WriteValue(System.IO.Stream, int, PeterO.Numbers.EInteger)](#WriteValue_System_IO_Stream_int_PeterO_Numbers_EInteger)
- Writes a CBOR major type number and an integer 0 or greater associated with it to a data stream, where that integer is passed to this method as an arbitrary-precision integer.[WriteValue(System.IO.Stream, int, uint)](#WriteValue_System_IO_Stream_int_uint)
- Writes a CBOR major type number and an integer 0 or greater associated with it to a data stream, where that integer is passed to this method as a 32-bit unsigned integer.[WriteValue(System.IO.Stream, int, ulong)](#WriteValue_System_IO_Stream_int_ulong)
- Writes a CBOR major type number and an integer 0 or greater associated with it to a data stream, where that integer is passed to this method as a 64-bit unsigned integer.[public static readonly PeterO.Cbor.CBORObject Zero;](#Zero)
- Gets a CBOR object for the number zero.
public static readonly PeterO.Cbor.CBORObject False;
Represents the value false.
public static readonly PeterO.Cbor.CBORObject NaN;
A not-a-number value.
public static readonly PeterO.Cbor.CBORObject NegativeInfinity;
The value negative infinity.
public static readonly PeterO.Cbor.CBORObject Null;
Represents the value null.
public static readonly PeterO.Cbor.CBORObject PositiveInfinity;
The value positive infinity.
public static readonly PeterO.Cbor.CBORObject True;
Represents the value true.
public static readonly PeterO.Cbor.CBORObject Undefined;
Represents the value undefined.
public static readonly PeterO.Cbor.CBORObject Zero;
Gets a CBOR object for the number zero.
public int Count { get; }
Gets the number of keys in this map, or the number of items in this array, or 0 if this item is neither an array nor a map.
Returns:
The number of keys in this map, or the number of items in this array, or 0 if this item is neither an array nor a map.
public System.Collections.Generic.ICollection Entries { get; }
Gets a collection of the key/value pairs stored in this CBOR object, if it’s a map. Returns one entry for each key/value pair in the map. In general, the order in which those entries occur is undefined unless this is a map created using the NewOrderedMap method.
Returns:
A collection of the key/value pairs stored in this CBOR map, as a read-only view of those pairs. To avoid potential problems, the calling code should not modify the CBOR map while iterating over the returned collection.
Exceptions:
- System.InvalidOperationException: This object is not a map.
public bool IsFalse { get; }
Gets a value indicating whether this value is a CBOR false value, whether tagged or not.
Returns:
true
if this value is a CBOR false value; otherwise, false
.
public bool IsFinite { get; }
Deprecated. Instead, use the following: (cbor.IsNumber && cbor.AsNumber().IsFinite()).
Gets a value indicating whether this CBOR object represents a finite number.
Returns:
true
if this CBOR object represents a finite number; otherwise, false
.
public bool IsIntegral { get; }
Deprecated. Instead, use the following: (cbor.IsNumber && cbor.AsNumber().IsInteger()).
Gets a value indicating whether this object represents an integer number, that is, a number without a fractional part. Infinity and not-a-number are not considered integers.
Returns:
true
if this object represents an integer number, that is, a number without a fractional part; otherwise, false
.
public bool IsNegative { get; }
Deprecated. Instead, use (cbor.IsNumber() && cbor.AsNumber().IsNegative()).
Gets a value indicating whether this object is a negative number.
Returns:
true
if this object is a negative number; otherwise, false
.
public bool IsNull { get; }
Gets a value indicating whether this CBOR object is a CBOR null value, whether tagged or not.
Returns:
true
if this value is a CBOR null value; otherwise, false
.
public bool IsNumber { get; }
Gets a value indicating whether this CBOR object stores a number (including infinity or a not-a-number or NaN value). Currently, this is true if this item is untagged and has a CBORType of Integer or FloatingPoint, or if this item has only one tag and that tag is 2, 3, 4, 5, 30, 264, 265, 268, 269, or 270 with the right data type.
Returns:
A value indicating whether this CBOR object stores a number.
public bool IsTagged { get; }
Gets a value indicating whether this data item has at least one tag.
Returns:
true
if this data item has at least one tag; otherwise, false
.
public bool IsTrue { get; }
Gets a value indicating whether this value is a CBOR true value, whether tagged or not.
Returns:
true
if this value is a CBOR true value; otherwise, false
.
public bool IsUndefined { get; }
Gets a value indicating whether this value is a CBOR undefined value, whether tagged or not.
Returns:
true
if this value is a CBOR undefined value; otherwise, false
.
public bool IsZero { get; }
Deprecated. Instead, use the following: (cbor.IsNumber && cbor.AsNumber().IsZero()).
Gets a value indicating whether this object’s value equals 0.
Returns:
true
if this object’s value equals 0; otherwise, false
.
public PeterO.Cbor.CBORObject this[string key] { get; set; }
Gets the value of a CBOR object in this map, using a string as the key.
Parameters:
- key: A key that points to the desired value.
Return Value:
The CBOR object referred to by key in this map. Returns null
if an item with the given key doesn’t exist.
Exceptions:
-
System.ArgumentNullException: The key is null.
-
System.InvalidOperationException: This object is not a map.
public System.Collections.Generic.ICollection Keys { get; }
Gets a collection of the keys of this CBOR object. In general, the order in which those keys occur is undefined unless this is a map created using the NewOrderedMap method.
Returns:
A collection of the keys of this CBOR object. To avoid potential problems, the calling code should not modify the CBOR map or the returned collection while iterating over the returned collection.
Exceptions:
- System.InvalidOperationException: This object is not a map.
public PeterO.Numbers.EInteger MostInnerTag { get; }
Gets the last defined tag for this CBOR data item, or -1 if the item is untagged.
Returns:
The last defined tag for this CBOR data item, or -1 if the item is untagged.
public PeterO.Numbers.EInteger MostOuterTag { get; }
Gets the outermost tag for this CBOR data item, or -1 if the item is untagged.
Returns:
The outermost tag for this CBOR data item, or -1 if the item is untagged.
public int Sign { get; }
Deprecated. Instead, convert this object to a number with .AsNumber(), and use the Sign property in .NET or the signum method in Java. Either will treat not-a-number (NaN) values differently than here.
Gets this value’s sign: -1 if negative; 1 if positive; 0 if zero. Throws an exception if this is a not-a-number value.
Returns:
This value’s sign: -1 if negative; 1 if positive; 0 if zero.
Exceptions:
- System.InvalidOperationException: This object does not represent a number, or this object is a not-a-number (NaN) value.
public int SimpleValue { get; }
Gets the simple value ID of this CBOR object, or -1 if the object is not a simple value. In this method, objects with a CBOR type of Boolean or SimpleValue are simple values, whether they are tagged or not.
Returns:
The simple value ID of this object if it’s a simple value, or -1 if this object is not a simple value.
public int TagCount { get; }
Gets the number of tags this object has.
Returns:
The number of tags this object has.
public PeterO.Cbor.CBORType Type { get; }
Gets the general data type of this CBOR object. This method disregards the tags this object has, if any.
Returns:
The general data type of this CBOR object.
public System.Collections.Generic.ICollection Values { get; }
Gets a collection of the values of this CBOR object, if it’s a map or an array. If this object is a map, returns one value for each key in the map; in general, the order in which those keys occur is undefined unless this is a map created using the NewOrderedMap method. If this is an array, returns all the values of the array in the order they are listed. (This method can’t be used to get the bytes in a CBOR byte string; for that, use the GetByteString method instead.).
Returns:
A collection of the values of this CBOR map or array. To avoid potential problems, the calling code should not modify the CBOR map or array or the returned collection while iterating over the returned collection.
Exceptions:
- System.InvalidOperationException: This object is not a map or an array.
public PeterO.Cbor.CBORObject Abs();
Deprecated. Instead, convert this object to a number (with .AsNumber()), and use that number’s .Abs() method.
Gets this object’s absolute value.
Return Value:
This object’s absolute without its negative sign.
Exceptions:
- System.InvalidOperationException: This object does not represent a number (for this purpose, infinities and not-a-number or NaN values, but not CBORObject.Null, are considered numbers).
public PeterO.Cbor.CBORObject Add( object key, object valueOb);
Adds a new key and its value to this CBOR map, or adds the value if the key doesn’t exist.
NOTE: This method can’t be used to add a tag to an existing CBOR object. To create a CBOR object with a given tag, call the CBORObject.FromObjectAndTag
method and pass the CBOR object and the desired tag number to that method.
Parameters:
-
key: An object representing the key, which will be converted to a CBORObject. Can be null, in which case this value is converted to CBORObject.Null.
-
valueOb: An object representing the value, which will be converted to a CBORObject. Can be null, in which case this value is converted to CBORObject.Null.
Return Value:
This instance.
Exceptions:
-
System.ArgumentException: The parameter key already exists in this map.
-
System.InvalidOperationException: This object is not a map.
-
System.ArgumentException: The parameter key or valueOb has an unsupported type.
public PeterO.Cbor.CBORObject Add( object obj);
Converts an object to a CBOR object and adds it to the end of this array.
NOTE: This method can’t be used to add a tag to an existing CBOR object. To create a CBOR object with a given tag, call the CBORObject.FromObjectAndTag
method and pass the CBOR object and the desired tag number to that method.
The following example creates a CBOR array and adds several CBOR objects, one of which has a custom CBOR tag, to that array. Note the chaining behavior made possible by this method.
CBORObject obj = CBORObject.NewArray() .Add(CBORObject.False) .Add(5) .Add("text string") .Add(CBORObject.FromObjectAndTag(9999, 1));
.
Parameters:
- obj: A CBOR object (or an object convertible to a CBOR object) to add to this CBOR array.
Return Value:
This instance.
Exceptions:
-
System.InvalidOperationException: This instance is not an array.
-
System.ArgumentException: The type of obj is not supported.
public PeterO.Cbor.CBORObject Add( PeterO.Cbor.CBORObject obj);
Adds a new object to the end of this array. (Used to throw ArgumentNullException on a null reference, but now converts the null reference to CBORObject.Null, for convenience with the Object overload of this method).
NOTE: This method can’t be used to add a tag to an existing CBOR object. To create a CBOR object with a given tag, call the CBORObject.FromObjectAndTag
method and pass the CBOR object and the desired tag number to that method.
The following example creates a CBOR array and adds several CBOR objects, one of which has a custom CBOR tag, to that array. Note the chaining behavior made possible by this method.
CBORObject obj = CBORObject.NewArray() .Add(CBORObject.False) .Add(CBORObject.FromObject(5)) .Add(CBORObject.FromObject("text string")) .Add(CBORObject.FromObjectAndTag(9999, 1));
.
Parameters:
- obj: The parameter obj is a CBOR object.
Return Value:
This instance.
Exceptions:
- System.InvalidOperationException: This object is not an array.
public static PeterO.Cbor.CBORObject Addition( PeterO.Cbor.CBORObject first, PeterO.Cbor.CBORObject second);
Deprecated. Instead, convert both CBOR objects to numbers (with .AsNumber()), and use the first number’s .Add() method.
Finds the sum of two CBOR numbers.
Parameters:
-
first: The parameter first is a CBOR object.
-
second: The parameter second is a CBOR object.
Return Value:
A CBOR object.
Exceptions:
-
System.ArgumentException: Either or both operands are not numbers (as opposed to Not-a-Number, NaN).
-
System.ArgumentNullException: The parameter first or second is null.
public PeterO.Cbor.CBORObject ApplyJSONPatch( PeterO.Cbor.CBORObject patch);
Returns a copy of this object after applying the operations in a JSON patch, in the form of a CBOR object. JSON patches are specified in RFC 6902 and their format is summarized in the remarks below.
Remarks: A JSON patch is an array with one or more maps. Each map has the following keys:
-
“op” - Required. This key’s value is the patch operation and must be “add”, “remove”, “move”, “copy”, “test”, or “replace”, in basic lower case letters and no other case combination.
-
“value” - Required if the operation is “add”, “replace”, or “test” and specifies the item to add (insert), or that will replace the existing item, or to check an existing item for equality, respectively. (For “test”, the operation fails if the existing item doesn’t match the specified value.)
-
“path” - Required for all operations. A JSON Pointer (RFC 6901) specifying the destination path in the CBOR object for the operation. For more information, see RFC 6901 or the documentation for AtJSONPointer(pointer, defaultValue).
-
“from” - Required if the operation is “move” or “copy”. A JSON Pointer (RFC 6901) specifying the path in the CBOR object where the source value is located.
Parameters:
- patch: A JSON patch in the form of a CBOR object; it has the form summarized in the remarks.
Return Value:
The result of the patch operation.
Exceptions:
- PeterO.Cbor.CBORException: The parameter patch is null or the patch operation failed.
public bool AsBoolean();
Returns false if this object is a CBOR false, null, or undefined value (whether or not the object has tags); otherwise, true.
Return Value:
False if this object is a CBOR false, null, or undefined value; otherwise, true.
public byte AsByte();
Deprecated. Instead, use .ToObject<byte>() in .NET or .ToObject(Byte.class) in Java.
Converts this object to a byte (0 to 255). Floating point values are converted to integers by discarding their fractional parts.
Return Value:
The closest byte-sized integer to this object.
Exceptions:
-
System.InvalidOperationException: This object does not represent a number (for this purpose, infinities and not-a-number or NaN values, but not CBORObject.Null, are considered numbers).
-
System.OverflowException: This object’s value exceeds the range of a byte (would be less than 0 or greater than 255 when converted to an integer by discarding its fractional part).
public decimal AsDecimal();
Deprecated. Instead, use .ToObject<decimal>().
Converts this object to a DotNet decimal.
Return Value:
The closest big integer to this object.
Exceptions:
-
System.InvalidOperationException: This object does not represent a number (for this purpose, infinities and not-a-number or NaN values, but not CBORObject.Null, are considered numbers).
-
System.OverflowException: This object’s value exceeds the range of a DotNet decimal.
public double AsDouble();
Converts this object to a 64-bit floating point number.
Return Value:
The closest 64-bit floating point number to this object. The return value can be positive infinity or negative infinity if this value exceeds the range of a 64-bit floating point number.
Exceptions:
- System.InvalidOperationException: This object does not represent a number (for this purpose, infinities and not-a-number or NaN values, but not CBORObject.Null, are considered numbers).
public long AsDoubleBits();
Converts this object to the bits of a 64-bit floating-point number if this CBOR object’s type is FloatingPoint. This method disregards the tags this object has, if any.
Return Value:
The bits of a 64-bit floating-point number stored by this object. The most significant bit is the sign (set means negative, clear means nonnegative); the next most significant 11 bits are the exponent area; and the remaining bits are the significand area. If all the bits of the exponent area are set and the significand area is 0, this indicates infinity. If all the bits of the exponent area are set and the significand area is other than 0, this indicates not-a-number (NaN).
Exceptions:
- System.InvalidOperationException:
This object’s type is not
CBORType.FloatingPoint
.
public double AsDoubleValue();
Converts this object to a 64-bit floating-point number if this CBOR object’s type is FloatingPoint. This method disregards the tags this object has, if any.
Return Value:
The 64-bit floating-point number stored by this object.
Exceptions:
- System.InvalidOperationException:
This object’s type is not
CBORType.FloatingPoint
.
public PeterO.Numbers.EDecimal AsEDecimal();
Deprecated. Instead, use .ToObject<PeterO.Numbers.EDecimal>() in .NET or .ToObject(com.upokecenter.numbers.EDecimal.class) in Java.
Converts this object to a decimal number.
Return Value:
A decimal number for this object’s value.
Exceptions:
- System.InvalidOperationException:
This object does not represent a number (for the purposes of this method, infinity and not-a-number values, but not
CBORObject.Null
, are considered numbers).
public PeterO.Numbers.EFloat AsEFloat();
Deprecated. Instead, use .ToObject<PeterO.Numbers.EFloat>() in .NET or .ToObject(com.upokecenter.numbers.EFloat.class) in Java.
Converts this object to an arbitrary-precision binary floating point number. See the ToObject overload taking a type for more information.
Return Value:
An arbitrary-precision binary floating-point number for this object’s value.
Exceptions:
- System.InvalidOperationException:
This object does not represent a number (for the purposes of this method, infinity and not-a-number values, but not
CBORObject.Null
, are considered numbers).
public PeterO.Numbers.EInteger AsEInteger();
Deprecated. Instead, use .ToObject<PeterO.Numbers.EInteger>() in .NET or .ToObject(com.upokecenter.numbers.EInteger.class) in Java.
Converts this object to an arbitrary-precision integer. See the ToObject overload taking a type for more information.
Return Value:
The closest arbitrary-precision integer to this object.
Exceptions:
-
System.InvalidOperationException: This object does not represent a number (for the purposes of this method, infinity and not-a-number values, but not
CBORObject.Null
, are considered numbers). -
System.OverflowException: This object’s value is infinity or not-a-number (NaN).
public PeterO.Numbers.EInteger AsEIntegerValue();
Converts this object to an arbitrary-precision integer if this CBOR object’s type is Integer. This method disregards the tags this object has, if any. (Note that CBOR stores untagged integers at least -(2^64) and less than 2^64.).
Return Value:
The integer stored by this object.
Exceptions:
- System.InvalidOperationException:
This object’s type is not
CBORType.Integer
.
public PeterO.Numbers.ERational AsERational();
Deprecated. Instead, use .ToObject<PeterO.Numbers.ERational>() in .NET or .ToObject(com.upokecenter.numbers.ERational.class) in Java.
Converts this object to a rational number. See the ToObject overload taking a type for more information.
Return Value:
A rational number for this object’s value.
Exceptions:
- System.InvalidOperationException:
This object does not represent a number (for the purposes of this method, infinity and not-a-number values, but not
CBORObject.Null
, are considered numbers).
public short AsInt16();
Deprecated. Instead, use the following: (cbor.AsNumber().ToInt16Checked()), or .ToObject<short>() in .NET.
Converts this object to a 16-bit signed integer. Floating point values are converted to integers by discarding their fractional parts.
Return Value:
The closest 16-bit signed integer to this object.
Exceptions:
-
System.InvalidOperationException: This object does not represent a number (for this purpose, infinities and not-a-number or NaN values, but not CBORObject.Null, are considered numbers).
-
System.OverflowException: This object’s value exceeds the range of a 16-bit signed integer.
public int AsInt32();
Converts this object to a 32-bit signed integer. Non-integer number values are converted to integers by discarding their fractional parts. (NOTE: To determine whether this method call can succeed, call AsNumber().CanTruncatedIntFitInInt32 before calling this method. See the example.).
The following example code (originally written in C# for the.NET Framework) shows a way to check whether a given CBOR object stores a 32-bit signed integer before getting its value.
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()); }
.
Return Value:
The closest 32-bit signed integer to this object.
Exceptions:
-
System.InvalidOperationException: This object does not represent a number (for this purpose, infinities and not-a-number or NaN values, but not CBORObject.Null, are considered numbers).
-
System.OverflowException: This object’s value exceeds the range of a 32-bit signed integer.
public int AsInt32Value();
Converts this object to a 32-bit signed integer if this CBOR object’s type is Integer. This method disregards the tags this object has, if any.
The following example code (originally written in C# for the.NET Framework) shows a way to check whether a given CBOR object stores a 32-bit signed integer before getting its value.
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()); }
.
Return Value:
The 32-bit signed integer stored by this object.
Exceptions:
-
System.InvalidOperationException: This object’s type is not
CBORType.Integer
. -
System.OverflowException: This object’s value exceeds the range of a 32-bit signed integer.
public long AsInt64();
Deprecated. Instead, use the following: (cbor.AsNumber().ToInt64Checked()), or .ToObject<long>() in .NET.
Converts this object to a 64-bit signed integer. Non-integer numbers are converted to integers by discarding their fractional parts. (NOTE: To determine whether this method call can succeed, call AsNumber().CanTruncatedIntFitInInt64 before calling this method. See the example.).
The following example code (originally written in C# for the.NET Framework) shows a way to check whether a given CBOR object stores a 64-bit signed integer before getting its value.
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()); }
.
Return Value:
The closest 64-bit signed integer to this object.
Exceptions:
-
System.InvalidOperationException: This object does not represent a number (for this purpose, infinities and not-a-number or NaN values, but not CBORObject.Null, are considered numbers).
-
System.OverflowException: This object’s value exceeds the range of a 64-bit signed integer.
public long AsInt64Value();
Converts this object to a 64-bit signed integer if this CBOR object’s type is Integer. This method disregards the tags this object has, if any.
The following example code (originally written in C# for the.NET Framework) shows a way to check whether a given CBOR object stores a 64-bit signed integer before getting its value.
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()); }
.
Return Value:
The 64-bit signed integer stored by this object.
Exceptions:
-
System.InvalidOperationException: This object’s type is not
CBORType.Integer
. -
System.OverflowException: This object’s value exceeds the range of a 64-bit signed integer.
public PeterO.Cbor.CBORNumber AsNumber();
Converts this object to a CBOR number. (NOTE: To determine whether this method call can succeed, call the IsNumber property (isNumber() method in Java) before calling this method.).
Return Value:
The number represented by this object.
Exceptions:
- System.InvalidOperationException: This object does not represent a number (for this purpose, infinities and not-a-number or NaN values, but not CBORObject.Null, are considered numbers).
public sbyte AsSByte();
Deprecated. Instead, use the following: (cbor.AsNumber().ToSByteChecked()), or .ToObject<sbyte>().
This API is not CLS-compliant.
Converts this object to an 8-bit signed integer.
Return Value:
An 8-bit signed integer.
public float AsSingle();
Converts this object to a 32-bit floating point number.
Return Value:
The closest 32-bit floating point number to this object. The return value can be positive infinity or negative infinity if this object’s value exceeds the range of a 32-bit floating point number.
Exceptions:
- System.InvalidOperationException: This object does not represent a number (for this purpose, infinities and not-a-number or NaN values, but not CBORObject.Null, are considered numbers).
public string AsString();
Gets the value of this object as a text string.
This method is not the “reverse” of the FromObject
method in the sense that FromObject can take either a text string or null
, but this method can accept only text strings. The ToObject
method is closer to a “reverse” version to FromObject
than the AsString
method: ToObject<String>(cbor)
in DotNet, or ToObject(String.class)
in Java, will convert a CBOR object to a DotNet or Java String if it represents a text string, or to null
if IsNull
returns true
for the CBOR object, and will fail in other cases.
Return Value:
Gets this object’s string.
Exceptions:
- System.InvalidOperationException:
This object’s type is not a text string (for the purposes of this method, infinity and not-a-number values, but not
CBORObject.Null
, are considered numbers). To check the CBOR object for null before conversion, use the following idiom (originally written in C# for the.NET version):(cbor == null || cbor.IsNull) ? null : cbor.AsString()
.
public ushort AsUInt16();
Deprecated. Instead, use the following: (cbor.AsNumber().ToUInt16Checked()), or .ToObject<ushort>().
This API is not CLS-compliant.
Converts this object to a 16-bit unsigned integer after discarding any fractional part, if any, from its value.
Return Value:
A 16-bit unsigned integer.
Exceptions:
-
System.InvalidOperationException: This object does not represent a number (for this purpose, infinities and not-a-number or NaN values, but not CBORObject.Null, are considered numbers).
-
System.OverflowException: This object’s value, if converted to an integer by discarding its fractional part, is outside the range of a 16-bit unsigned integer.
public uint AsUInt32();
Deprecated. Instead, use the following: (cbor.AsNumber().ToUInt32Checked()), or .ToObject<uint>().
This API is not CLS-compliant.
Converts this object to a 32-bit unsigned integer after discarding any fractional part, if any, from its value.
Return Value:
A 32-bit unsigned integer.
Exceptions:
-
System.InvalidOperationException: This object does not represent a number (for this purpose, infinities and not-a-number or NaN values, but not CBORObject.Null, are considered numbers).
-
System.OverflowException: This object’s value, if converted to an integer by discarding its fractional part, is outside the range of a 32-bit unsigned integer.
public ulong AsUInt64();
Deprecated. Instead, use the following: (cbor.AsNumber().ToUInt64Checked()), or .ToObject<ulong>().
This API is not CLS-compliant.
Converts this object to a 64-bit unsigned integer after discarding any fractional part, if any, from its value.
Return Value:
A 64-bit unsigned integer.
Exceptions:
-
System.InvalidOperationException: This object does not represent a number (for this purpose, infinities and not-a-number or NaN values, but not CBORObject.Null, are considered numbers).
-
System.OverflowException: This object’s value, if converted to an integer by discarding its fractional part, is outside the range of a 64-bit unsigned integer.
public PeterO.Cbor.CBORObject AtJSONPointer( string pointer);
Gets the CBOR object referred to by a JSON Pointer according to RFC6901. For more information, see the overload taking a default value parameter.
Parameters:
- pointer: A JSON pointer according to RFC 6901.
Return Value:
An object within this CBOR object. Returns this object if pointer is the empty string (even if this object has a CBOR type other than array or map).
Exceptions:
- PeterO.Cbor.CBORException: Thrown if the pointer is null, or if the pointer is invalid, or if there is no object at the given pointer, or the special key “-“ appears in the pointer in the context of an array (not a map), or if the pointer is non-empty and this object has a CBOR type other than array or map.
public PeterO.Cbor.CBORObject AtJSONPointer( string pointer, PeterO.Cbor.CBORObject defaultValue);
Gets the CBOR object referred to by a JSON Pointer according to RFC6901, or a default value if the operation fails. The syntax for a JSON Pointer is: ‘/’ KEY ‘/’ KEY […] where KEY represents a key into the JSON object or its sub-objects in the hierarchy. For example, /foo/2/bar means 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.).
Parameters:
-
pointer: A JSON pointer according to RFC 6901.
-
defaultValue: The parameter defaultValue is a Cbor.CBORObject object.
Return Value:
An object within the specified JSON object. Returns this object if pointer is the empty string (even if this object has a CBOR type other than array or map). Returns defaultValue if the pointer is null, or if the pointer is invalid, or if there is no object at the given pointer, or the special key “-“ appears in the pointer in the context of an array (not a map), or if the pointer is non-empty and this object has a CBOR type other than array or map.
public long CalcEncodedSize();
Calculates the number of bytes this CBOR object takes when serialized as a byte array using the EncodeToBytes()
method. This calculation assumes that integers, lengths of maps and arrays, lengths of text and byte strings, and tag numbers are encoded in their shortest form; that floating-point numbers are encoded in their shortest value-preserving form; and that no indefinite-length encodings are used.
Return Value:
The number of bytes this CBOR object takes when serialized as a byte array using the EncodeToBytes()
method.
Exceptions:
- PeterO.Cbor.CBORException: The CBOR object has an extremely deep level of nesting, including if the CBOR object is or has an array or map that includes itself.
public bool CanFitInDouble();
Deprecated. Instead, use the following: (cbor.IsNumber && cbor.AsNumber().CanFitInDouble()).
Returns whether this object’s value can be converted to a 64-bit floating point number without its value being rounded to another numerical value.
Return Value:
true
if this object’s value can be converted to a 64-bit floating point number without its value being rounded to another numerical value, or if this is a not-a-number value, even if the value’s diagnostic information can’t fit in a 64-bit floating point number; otherwise, false
.
public bool CanFitInInt32();
Deprecated. Instead, use .CanValueFitInInt32(), if the application allows only CBOR integers, or (cbor.IsNumber &&cbor.AsNumber().CanFitInInt32()), if the application allows any CBOR object convertible to an integer.
Returns whether this object’s numerical value is an integer, is -(2^31) or greater, and is less than 2^31.
Return Value:
true
if this object’s numerical value is an integer, is -(2^31) or greater, and is less than 2^31; otherwise, false
.
public bool CanFitInInt64();
Deprecated. Instead, use CanValueFitInInt64(), if the application allows only CBOR integers, or (cbor.IsNumber &&cbor.AsNumber().CanFitInInt64()), if the application allows any CBOR object convertible to an integer.
Returns whether this object’s numerical value is an integer, is -(2^63) or greater, and is less than 2^63.
Return Value:
true
if this object’s numerical value is an integer, is -(2^63) or greater, and is less than 2^63; otherwise, false
.
public bool CanFitInSingle();
Deprecated. Instead, use the following: (cbor.IsNumber && cbor.AsNumber().CanFitInSingle()).
Returns whether this object’s value can be converted to a 32-bit floating point number without its value being rounded to another numerical value.
Return Value:
true
if this object’s value can be converted to a 32-bit floating point number without its value being rounded to another numerical value, or if this is a not-a-number value, even if the value’s diagnostic information can’ t fit in a 32-bit floating point number; otherwise, false
.
public bool CanTruncatedIntFitInInt32();
Deprecated. Instead, use the following: (cbor.CanValueFitInInt32() if only integers of any tag are allowed, or (cbor.IsNumber && cbor.AsNumber().CanTruncatedIntFitInInt32()).
Returns whether this object’s value, converted to an integer by discarding its fractional part, would be -(2^31) or greater, and less than 2^31.
Return Value:
true
if this object’s value, converted to an integer by discarding its fractional part, would be -(2^31) or greater, and less than 2^31; otherwise, false
.
public bool CanTruncatedIntFitInInt64();
Deprecated. Instead, use the following: (cbor.CanValueFitInInt64() if only integers of any tag are allowed, or (cbor.IsNumber && cbor.AsNumber().CanTruncatedIntFitInInt64()).
Returns whether this object’s value, converted to an integer by discarding its fractional part, would be -(2^63) or greater, and less than 2^63.
Return Value:
true
if this object’s value, converted to an integer by discarding its fractional part, would be -(2^63) or greater, and less than 2^63; otherwise, false
.
public bool CanValueFitInInt32();
Returns whether this CBOR object stores an integer (CBORType.Integer) within the range of a 32-bit signed integer. This method disregards the tags this object has, if any.
Return Value:
true
if this CBOR object stores an integer (CBORType.Integer) whose value is at least -(2^31) and less than 2^31; otherwise, false
.
public bool CanValueFitInInt64();
Returns whether this CBOR object stores an integer (CBORType.Integer) within the range of a 64-bit signed integer. This method disregards the tags this object has, if any.
Return Value:
true
if this CBOR object stores an integer (CBORType.Integer) whose value is at least -(2^63) and less than 2^63; otherwise, false
.
public void Clear();
Removes all items from this CBOR array or all keys and values from this CBOR map.
Exceptions:
- System.InvalidOperationException: This object is not a CBOR array or CBOR map.
public sealed int CompareTo( PeterO.Cbor.CBORObject other);
Compares two CBOR objects. This implementation was changed in version 4.0. In this implementation:
-
The null pointer (null reference) is considered less than any other object.
-
If the two objects are both integers (CBORType.Integer) both floating-point values, both byte strings, both simple values (including True and False), or both text strings, their CBOR encodings (as though EncodeToBytes were called on each integer) are compared as though by a byte-by-byte comparison. (This means, for example, that positive integers sort before negative integers).
-
If both objects have a tag, they are compared first by the tag’s value then by the associated item (which itself can have a tag).
-
If both objects are arrays, they are compared item by item. In this case, if the arrays have different numbers of items, the array with more items is treated as greater than the other array.
-
If both objects are maps, their key-value pairs, sorted by key in accordance with this method, are compared, where each pair is compared first by key and then by value. In this case, if the maps have different numbers of key-value pairs, the map with more pairs is treated as greater than the other map.
-
If the two objects have different types, the object whose type comes first in the order of untagged integers, untagged byte strings, untagged text strings, untagged arrays, untagged maps, tagged objects, untagged simple values (including True and False) and untagged floating point values sorts before the other object.
This method is consistent with the Equals method.
Parameters:
- other: A value to compare with.
Return Value:
A negative number, if this value is less than the other object; or 0, if both values are equal; or a positive number, if this value is less than the other object or if the other object is null. This implementation returns a positive number if other
is null, to conform to the.NET definition of CompareTo. This is the case even in the Java version of this library, for consistency’s sake, even though implementations of Comparable.compareTo()
in Java ought to throw an exception if they receive a null argument rather than treating null as less or greater than any object.
.
public int CompareToIgnoreTags( PeterO.Cbor.CBORObject other);
Compares this object and another CBOR object, ignoring the tags they have, if any. See the CompareTo method for more information on the comparison function.
Parameters:
- other: A value to compare with.
Return Value:
Less than 0, if this value is less than the other object; or 0, if both values are equal; or greater than 0, if this value is less than the other object or if the other object is null.
public bool ContainsKey( object objKey);
Determines whether a value of the given key exists in this object.
Parameters:
- objKey: The parameter objKey is an arbitrary object.
Return Value:
true
if the given key is found, or false
if the given key is not found or this object is not a map.
public bool ContainsKey( PeterO.Cbor.CBORObject key);
Determines whether a value of the given key exists in this object.
Parameters:
- key: An object that serves as the key. If this is
null
, checks forCBORObject.Null
.
Return Value:
true
if the given key is found, or false
if the given key is not found or this object is not a map.
public bool ContainsKey( string key);
Determines whether a value of the given key exists in this object.
Parameters:
- key: A text string that serves as the key. If this is
null
, checks forCBORObject.Null
.
Return Value:
true
if the given key (as a CBOR object) is found, or false
if the given key is not found or this object is not a map.
public static PeterO.Cbor.CBORObject DecodeFromBytes( byte[] data);
Generates a CBOR object from an array of CBOR-encoded bytes.
Parameters:
- data: A byte array in which a single CBOR object is encoded.
Return Value:
A CBOR object decoded from the given byte array.
Exceptions:
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data. This includes cases where not all of the byte array represents a CBOR object. This exception is also thrown if the parameter data is empty.
-
System.ArgumentNullException: The parameter data is null.
public static PeterO.Cbor.CBORObject DecodeFromBytes( byte[] data, PeterO.Cbor.CBOREncodeOptions options);
Generates a CBOR object from an array of CBOR-encoded bytes, using the given CBOREncodeOptions
object to control the decoding process.
The following example (originally written in C# for the.NET version) implements a method that decodes a text string from a CBOR byte array. It’s successful only if the CBOR object contains an untagged text string.
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(); }
.
Parameters:
-
data: A byte array in which a single CBOR object is encoded.
-
options: Specifies options to control how the CBOR object is decoded. See PeterO.Cbor.CBOREncodeOptions for more information.
Return Value:
A CBOR object decoded from the given byte array. Returns null (as opposed to CBORObject.Null) if data is empty and the AllowEmpty property is set on the given options object.
Exceptions:
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data. This includes cases where not all of the byte array represents a CBOR object. This exception is also thrown if the parameter data is empty unless the AllowEmpty property is set on the given options object.
-
System.ArgumentNullException: The parameter data is null, or the parameter options is null.
public static object DecodeObjectFromBytes( byte[] data, PeterO.Cbor.CBOREncodeOptions enc, System.Type t);
Generates an object of an arbitrary type from an array of CBOR-encoded bytes, using the given CBOREncodeOptions
object to control the decoding process. It is equivalent to DecodeFromBytes followed by ToObject. See the documentation for those methods for more information.
Parameters:
-
data: A byte array in which a single CBOR object is encoded.
-
enc: Specifies options to control how the CBOR object is decoded. See PeterO.Cbor.CBOREncodeOptions for more information.
-
t: The type, class, or interface that this method’s return value will belong to. To express a generic type in Java, see the example. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method, such as
int
orString
, or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
Return Value:
An object of the given type decoded from the given byte array. Returns null (as opposed to CBORObject.Null) if data is empty and the AllowEmpty property is set on the given CBOREncodeOptions object.
Exceptions:
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data. This includes cases where not all of the byte array represents a CBOR object. This exception is also thrown if the parameter data is empty unless the AllowEmpty property is set on the given options object. Also thrown if the given type t , or this object’s CBOR type, is not supported, or the given object’s nesting is too deep, or another error occurred when serializing the object.
-
System.ArgumentNullException: The parameter data is null, or the parameter enc is null, or the parameter t is null.
public static object DecodeObjectFromBytes( byte[] data, PeterO.Cbor.CBOREncodeOptions enc, System.Type t, PeterO.Cbor.CBORTypeMapper mapper, PeterO.Cbor.PODOptions pod);
Generates an object of an arbitrary type from an array of CBOR-encoded bytes, using the given CBOREncodeOptions
object to control the decoding process. It is equivalent to DecodeFromBytes followed by ToObject. See the documentation for those methods for more information.
Parameters:
-
data: A byte array in which a single CBOR object is encoded.
-
enc: Specifies options to control how the CBOR object is decoded. See PeterO.Cbor.CBOREncodeOptions for more information.
-
t: The type, class, or interface that this method’s return value will belong to. To express a generic type in Java, see the example. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method, such as
int
orString
, or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above. -
mapper: This parameter controls which data types are eligible for Plain-Old-Data deserialization and includes custom converters from CBOR objects to certain data types. Can be null.
-
pod: Specifies options for controlling deserialization of CBOR objects.
Return Value:
An object of the given type decoded from the given byte array. Returns null (as opposed to CBORObject.Null) if data is empty and the AllowEmpty property is set on the given CBOREncodeOptions object.
Exceptions:
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data. This includes cases where not all of the byte array represents a CBOR object. This exception is also thrown if the parameter data is empty unless the AllowEmpty property is set on the given options object. Also thrown if the given type t , or this object’s CBOR type, is not supported, or the given object’s nesting is too deep, or another error occurred when serializing the object.
-
System.ArgumentNullException: The parameter data is null, or the parameter enc is null, or the parameter t or pod is null.
public static object DecodeObjectFromBytes( byte[] data, System.Type t);
Generates an object of an arbitrary type from an array of CBOR-encoded bytes. It is equivalent to DecodeFromBytes followed by ToObject. See the documentation for those methods for more information.
Parameters:
-
data: A byte array in which a single CBOR object is encoded.
-
t: The type, class, or interface that this method’s return value will belong to. To express a generic type in Java, see the example. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method, such as
int
orString
, or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
Return Value:
An object of the given type decoded from the given byte array. Returns null (as opposed to CBORObject.Null) if data is empty and the AllowEmpty property is set on the given CBOREncodeOptions object.
Exceptions:
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data. This includes cases where not all of the byte array represents a CBOR object. This exception is also thrown if the parameter data is empty unless the AllowEmpty property is set on the given options object. Also thrown if the given type t , or this object’s CBOR type, is not supported, or the given object’s nesting is too deep, or another error occurred when serializing the object.
-
System.ArgumentNullException: The parameter data is null, or the parameter t is null.
public static object DecodeObjectFromBytes( byte[] data, System.Type t, PeterO.Cbor.CBORTypeMapper mapper, PeterO.Cbor.PODOptions pod);
Generates an object of an arbitrary type from an array of CBOR-encoded bytes. It is equivalent to DecodeFromBytes followed by ToObject. See the documentation for those methods for more information.
Parameters:
-
data: A byte array in which a single CBOR object is encoded.
-
t: The type, class, or interface that this method’s return value will belong to. To express a generic type in Java, see the example. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method, such as
int
orString
, or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above. -
mapper: This parameter controls which data types are eligible for Plain-Old-Data deserialization and includes custom converters from CBOR objects to certain data types. Can be null.
-
pod: Specifies options for controlling deserialization of CBOR objects.
Return Value:
An object of the given type decoded from the given byte array. Returns null (as opposed to CBORObject.Null) if data is empty and the AllowEmpty property is set on the given CBOREncodeOptions object.
Exceptions:
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data. This includes cases where not all of the byte array represents a CBOR object. This exception is also thrown if the parameter data is empty unless the AllowEmpty property is set on the given options object. Also thrown if the given type t , or this object’s CBOR type, is not supported, or the given object’s nesting is too deep, or another error occurred when serializing the object.
-
System.ArgumentNullException: The parameter data is null, or the parameter t or pod is null.
public static T DecodeObjectFromBytes<T>( byte[] data);
Generates an object of an arbitrary type from an array of CBOR-encoded bytes. It is equivalent to DecodeFromBytes followed by ToObject. See the documentation for those methods for more information.
Parameters:
-
data: A byte array in which a single CBOR object is encoded.
-
<T>: The type, class, or interface that this method’s return value will belong to. To express a generic type in Java, see the example. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method, such as
int
orString
, or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
Return Value:
An object of the given type decoded from the given byte array. Returns null (as opposed to CBORObject.Null) if data is empty and the AllowEmpty property is set on the given CBOREncodeOptions object.
Exceptions:
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data. This includes cases where not all of the byte array represents a CBOR object. This exception is also thrown if the parameter data is empty unless the AllowEmpty property is set on the given options object. Also thrown if the given type “T”, or this object’s CBOR type, is not supported, or the given object’s nesting is too deep, or another error occurred when serializing the object.
-
System.ArgumentNullException: The parameter data is null.
public static T DecodeObjectFromBytes<T>( byte[] data, PeterO.Cbor.CBOREncodeOptions enc);
Generates an object of an arbitrary type from an array of CBOR-encoded bytes, using the given CBOREncodeOptions
object to control the decoding process. It is equivalent to DecodeFromBytes followed by ToObject. See the documentation for those methods for more information.
Parameters:
-
data: A byte array in which a single CBOR object is encoded.
-
enc: Specifies options to control how the CBOR object is decoded. See PeterO.Cbor.CBOREncodeOptions for more information.
-
<T>: The type, class, or interface that this method’s return value will belong to. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method, such as
int
orString
, or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
Return Value:
An object of the given type decoded from the given byte array. Returns null (as opposed to CBORObject.Null) if data is empty and the AllowEmpty property is set on the given CBOREncodeOptions object.
Exceptions:
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data. This includes cases where not all of the byte array represents a CBOR object. This exception is also thrown if the parameter data is empty unless the AllowEmpty property is set on the given options object. Also thrown if the given type “T”, or this object’s CBOR type, is not supported, or the given object’s nesting is too deep, or another error occurred when serializing the object.
-
System.ArgumentNullException: The parameter data is null, or the parameter enc is null.
public static T DecodeObjectFromBytes<T>( byte[] data, PeterO.Cbor.CBOREncodeOptions enc, PeterO.Cbor.CBORTypeMapper mapper, PeterO.Cbor.PODOptions pod);
Generates an object of an arbitrary type from an array of CBOR-encoded bytes, using the given CBOREncodeOptions
object to control the decoding process. It is equivalent to DecodeFromBytes followed by ToObject. See the documentation for those methods for more information.
Parameters:
-
data: A byte array in which a single CBOR object is encoded.
-
enc: Specifies options to control how the CBOR object is decoded. See PeterO.Cbor.CBOREncodeOptions for more information.
-
mapper: This parameter controls which data types are eligible for Plain-Old-Data deserialization and includes custom converters from CBOR objects to certain data types. Can be null.
-
pod: Specifies options for controlling deserialization of CBOR objects.
-
<T>: The type, class, or interface that this method’s return value will belong to. To express a generic type in Java, see the example. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method, such as
int
orString
, or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
Return Value:
An object of the given type decoded from the given byte array. Returns null (as opposed to CBORObject.Null) if data is empty and the AllowEmpty property is set on the given CBOREncodeOptions object.
Exceptions:
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data. This includes cases where not all of the byte array represents a CBOR object. This exception is also thrown if the parameter data is empty unless the AllowEmpty property is set on the given options object. Also thrown if the given type “T”, or this object’s CBOR type, is not supported, or the given object’s nesting is too deep, or another error occurred when serializing the object.
-
System.ArgumentNullException: The parameter data is null, or the parameter enc is null, or the parameter “T” or pod is null.
public static T DecodeObjectFromBytes<T>( byte[] data, PeterO.Cbor.CBORTypeMapper mapper, PeterO.Cbor.PODOptions pod);
Generates an object of an arbitrary type from an array of CBOR-encoded bytes. It is equivalent to DecodeFromBytes followed by ToObject. See the documentation for those methods for more information.
Parameters:
-
data: A byte array in which a single CBOR object is encoded.
-
mapper: This parameter controls which data types are eligible for Plain-Old-Data deserialization and includes custom converters from CBOR objects to certain data types. Can be null.
-
pod: Specifies options for controlling deserialization of CBOR objects.
-
<T>: The type, class, or interface that this method’s return value will belong to. To express a generic type in Java, see the example. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method, such as
int
orString
, or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
Return Value:
An object of the given type decoded from the given byte array. Returns null (as opposed to CBORObject.Null) if data is empty and the AllowEmpty property is set on the given CBOREncodeOptions object.
Exceptions:
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data. This includes cases where not all of the byte array represents a CBOR object. This exception is also thrown if the parameter data is empty unless the AllowEmpty property is set on the given options object. Also thrown if the given type “T”, or this object’s CBOR type, is not supported, or the given object’s nesting is too deep, or another error occurred when serializing the object.
-
System.ArgumentNullException: The parameter data is null, or the parameter “T” or pod is null.
public static PeterO.Cbor.CBORObject[] DecodeSequenceFromBytes( byte[] data);
Generates a sequence of CBOR objects from an array of CBOR-encoded bytes.
Parameters:
- data: A byte array in which any number of CBOR objects (including zero) are encoded, one after the other. Can be empty, but cannot be null.
Return Value:
An array of CBOR objects decoded from the given byte array. Returns an empty array if data is empty.
Exceptions:
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data. This includes cases where the last CBOR object in the data was read only partly.
-
System.ArgumentNullException: The parameter data is null.
public static PeterO.Cbor.CBORObject[] DecodeSequenceFromBytes( byte[] data, PeterO.Cbor.CBOREncodeOptions options);
Generates a sequence of CBOR objects from an array of CBOR-encoded bytes.
Parameters:
-
data: A byte array in which any number of CBOR objects (including zero) are encoded, one after the other. Can be empty, but cannot be null.
-
options: Specifies options to control how the CBOR object is decoded. See PeterO.Cbor.CBOREncodeOptions for more information. In this method, the AllowEmpty property is treated as always set regardless of that value as specified in this parameter.
Return Value:
An array of CBOR objects decoded from the given byte array. Returns an empty array if data is empty.
Exceptions:
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data. This includes cases where the last CBOR object in the data was read only partly.
-
System.ArgumentNullException: The parameter data is null, or the parameter options is null.
public static PeterO.Cbor.CBORObject Divide( PeterO.Cbor.CBORObject first, PeterO.Cbor.CBORObject second);
Deprecated. Instead, convert both CBOR objects to numbers (with .AsNumber()), and use the first number’s .Divide() method.
Divides a CBORObject object by the value of a CBORObject object.
Parameters:
-
first: The parameter first is a CBOR object.
-
second: The parameter second is a CBOR object.
Return Value:
The quotient of the two objects.
Exceptions:
- System.ArgumentNullException: The parameter first or second is null.
public byte[] EncodeToBytes( PeterO.Cbor.CBOREncodeOptions options);
Writes the binary representation of this CBOR object and returns a byte array of that representation, using the specified options for encoding the object to CBOR format. For the CTAP2 (FIDO Client-to-Authenticator Protocol 2) canonical ordering, which is useful for implementing Web Authentication, call this method as follows: EncodeToBytes(new
CBOREncodeOptions("ctap2canonical=true"))
.
Parameters:
- options: Options for encoding the data to CBOR.
Return Value:
A byte array in CBOR format.
Exceptions:
- System.ArgumentNullException: The parameter options is null.
public byte[] EncodeToBytes();
Writes the binary representation of this CBOR object and returns a byte array of that representation. If the CBOR object contains CBOR maps, or is a CBOR map itself, the order in which the keys to the map are written out to the byte array is undefined unless the map was created using the NewOrderedMap method. The example code given in M:PeterO.Cbor.CBORObject.WriteTo(System.IO.Stream) can be used to write out certain keys of a CBOR map in a given order. For the CTAP2 (FIDO Client-to-Authenticator Protocol 2) canonical ordering, which is useful for implementing Web Authentication, call EncodeToBytes(new CBOREncodeOptions("ctap2canonical=true"))
rather than this method.
Return Value:
A byte array in CBOR format.
public override bool Equals( object obj);
Determines whether this object and another object are equal and have the same type. Not-a-number values can be considered equal by this method.
Parameters:
- obj: The parameter obj is an arbitrary object.
Return Value:
true
if the objects are equal; otherwise, false
. In this method, two objects are not equal if they don’t have the same type or if one is null and the other isn’t.
public sealed bool Equals( PeterO.Cbor.CBORObject other);
Compares the equality of two CBOR objects. Not-a-number values can be considered equal by this method.
Parameters:
- other: The object to compare.
Return Value:
true
if the objects are equal; otherwise, false
. In this method, two objects are not equal if they don’t have the same type or if one is null and the other isn’t.
public static PeterO.Cbor.CBORObject FromFloatingPointBits( long floatingBits, int byteCount);
Generates a CBOR object from a floating-point number represented by its bits.
Parameters:
-
floatingBits: The bits of a floating-point number number to write.
-
byteCount: The number of bytes of the stored floating-point number; this also specifies the format of the “floatingBits” parameter. This value can be 2 if “floatingBits”’s lowest (least significant) 16 bits identify the floating-point number in IEEE 754r binary16 format; or 4 if “floatingBits”’s lowest (least significant) 32 bits identify the floating-point number in IEEE 754r binary32 format; or 8 if “floatingBits” identifies the floating point number in IEEE 754r binary64 format. Any other values for this parameter are invalid.
Return Value:
A CBOR object storing the given floating-point number.
Exceptions:
- System.ArgumentException: The parameter byteCount is other than 2, 4, or 8.
public static PeterO.Cbor.CBORObject FromJSONBytes( byte[] bytes);
Generates a CBOR object from a byte array in JavaScript Object Notation (JSON) format.
If a JSON object has duplicate keys, a CBORException is thrown.
Note that if a CBOR object is converted to JSON with ToJSONBytes
, then the JSON is converted back to CBOR with this method, the new CBOR object will not necessarily be the same as the old CBOR object, especially if the old CBOR object uses data types not supported in JSON, such as integers in map keys.
Parameters:
- bytes: A byte array in JSON format. The entire byte array must contain a single JSON object and not multiple objects. The byte array may begin with a byte-order mark (U+FEFF). The byte array can be in UTF-8, UTF-16, or UTF-32 encoding; the encoding is detected by assuming that the first character read must be a byte-order mark or a nonzero basic character (U+0001 to U+007F). (This behavior may change to supporting only UTF-8, with or without a byte order mark, in version 5.0 or later, perhaps with an option to restore the previous behavior of also supporting UTF-16 and UTF-32.).
Return Value:
A CBOR object containing the JSON data decoded.
Exceptions:
-
System.ArgumentNullException: The parameter bytes is null.
-
PeterO.Cbor.CBORException: The byte array contains invalid encoding or is not in JSON format.
public static PeterO.Cbor.CBORObject FromJSONBytes( byte[] bytes, int offset, int count);
Generates a CBOR object from a byte array in JavaScript Object Notation (JSON) format.
If a JSON object has duplicate keys, a CBORException is thrown.
Note that if a CBOR object is converted to JSON with ToJSONBytes
, then the JSON is converted back to CBOR with this method, the new CBOR object will not necessarily be the same as the old CBOR object, especially if the old CBOR object uses data types not supported in JSON, such as integers in map keys.
Parameters:
-
bytes: A byte array, the specified portion of which is in JSON format. The specified portion of the byte array must contain a single JSON object and not multiple objects. The portion may begin with a byte-order mark (U+FEFF). The portion can be in UTF-8, UTF-16, or UTF-32 encoding; the encoding is detected by assuming that the first character read must be a byte-order mark or a nonzero basic character (U+0001 to U+007F). (This behavior may change to supporting only UTF-8, with or without a byte order mark, in version 5.0 or later, perhaps with an option to restore the previous behavior of also supporting UTF-16 and UTF-32.).
-
offset: An index, starting at 0, showing where the desired portion of bytes begins.
-
count: The length, in bytes, of the desired portion of bytes (but not more than bytes ‘s length).
Return Value:
A CBOR object containing the JSON data decoded.
Exceptions:
-
System.ArgumentNullException: The parameter bytes is null.
-
PeterO.Cbor.CBORException: The byte array contains invalid encoding or is not in JSON format.
-
System.ArgumentException: Either offset or count is less than 0 or greater than bytes ‘s length, or bytes ‘s length minus offset is less than count .
public static PeterO.Cbor.CBORObject FromJSONBytes( byte[] bytes, int offset, int count, PeterO.Cbor.JSONOptions jsonoptions);
Generates a CBOR object from a byte array in JavaScript Object Notation (JSON) format, using the specified options to control the decoding process. Note that if a CBOR object is converted to JSON with ToJSONBytes
, then the JSON is converted back to CBOR with this method, the new CBOR object will not necessarily be the same as the old CBOR object, especially if the old CBOR object uses data types not supported in JSON, such as integers in map keys.
Parameters:
-
bytes: A byte array, the specified portion of which is in JSON format. The specified portion of the byte array must contain a single JSON object and not multiple objects. The portion may begin with a byte-order mark (U+FEFF). The portion can be in UTF-8, UTF-16, or UTF-32 encoding; the encoding is detected by assuming that the first character read must be a byte-order mark or a nonzero basic character (U+0001 to U+007F). (This behavior may change to supporting only UTF-8, with or without a byte order mark, in version 5.0 or later, perhaps with an option to restore the previous behavior of also supporting UTF-16 and UTF-32.).
-
offset: An index, starting at 0, showing where the desired portion of bytes begins.
-
count: The length, in bytes, of the desired portion of bytes (but not more than bytes ‘s length).
-
jsonoptions: Specifies options to control how the JSON data is decoded to CBOR. See the JSONOptions class.
Return Value:
A CBOR object containing the JSON data decoded.
Exceptions:
-
System.ArgumentNullException: The parameter bytes or jsonoptions is null.
-
PeterO.Cbor.CBORException: The byte array contains invalid encoding or is not in JSON format.
-
System.ArgumentException: Either offset or count is less than 0 or greater than bytes ‘s length, or bytes ‘s length minus offset is less than count .
public static PeterO.Cbor.CBORObject FromJSONBytes( byte[] bytes, PeterO.Cbor.JSONOptions jsonoptions);
Generates a CBOR object from a byte array in JavaScript Object Notation (JSON) format, using the specified options to control the decoding process. Note that if a CBOR object is converted to JSON with ToJSONBytes
, then the JSON is converted back to CBOR with this method, the new CBOR object will not necessarily be the same as the old CBOR object, especially if the old CBOR object uses data types not supported in JSON, such as integers in map keys.
Parameters:
-
bytes: A byte array in JSON format. The entire byte array must contain a single JSON object and not multiple objects. The byte array may begin with a byte-order mark (U+FEFF). The byte array can be in UTF-8, UTF-16, or UTF-32 encoding; the encoding is detected by assuming that the first character read must be a byte-order mark or a nonzero basic character (U+0001 to U+007F). (This behavior may change to supporting only UTF-8, with or without a byte order mark, in version 5.0 or later, perhaps with an option to restore the previous behavior of also supporting UTF-16 and UTF-32.).
-
jsonoptions: Specifies options to control how the JSON data is decoded to CBOR. See the JSONOptions class.
Return Value:
A CBOR object containing the JSON data decoded.
Exceptions:
-
System.ArgumentNullException: The parameter bytes or jsonoptions is null.
-
PeterO.Cbor.CBORException: The byte array contains invalid encoding or is not in JSON format.
public static PeterO.Cbor.CBORObject[] FromJSONSequenceBytes( byte[] bytes);
Generates a list of CBOR objects from an array of bytes in JavaScript Object Notation (JSON) text sequence format (RFC 7464). The byte array must be in UTF-8 encoding and may not begin with a byte-order mark (U+FEFF).
Generally, each JSON text in a JSON text sequence is written as follows: Write a record separator byte (0x1e), then write the JSON text in UTF-8 (without a byte order mark, U+FEFF), then write the line feed byte (0x0a). RFC 7464, however, uses a more liberal syntax for parsing JSON text sequences.
Parameters:
- bytes: A byte array in which a JSON text sequence is encoded.
Return Value:
A list of CBOR objects read from the JSON sequence. Objects that could not be parsed are replaced with null
(as opposed to CBORObject.Null
) in the given list.
Exceptions:
-
System.ArgumentNullException: The parameter bytes is null.
-
PeterO.Cbor.CBORException: The byte array is not empty and does not begin with a record separator byte (0x1e), or an I/O error occurred.
public static PeterO.Cbor.CBORObject[] FromJSONSequenceBytes( byte[] data, PeterO.Cbor.JSONOptions options);
Generates a list of CBOR objects from an array of bytes in JavaScript Object Notation (JSON) text sequence format (RFC 7464), using the specified options to control the decoding process. The byte array must be in UTF-8 encoding and may not begin with a byte-order mark (U+FEFF).
Generally, each JSON text in a JSON text sequence is written as follows: Write a record separator byte (0x1e), then write the JSON text in UTF-8 (without a byte order mark, U+FEFF), then write the line feed byte (0x0a). RFC 7464, however, uses a more liberal syntax for parsing JSON text sequences.
Parameters:
-
data: A byte array in which a JSON text sequence is encoded.
-
options: Specifies options to control the JSON decoding process.
Return Value:
A list of CBOR objects read from the JSON sequence. Objects that could not be parsed are replaced with null
(as opposed to CBORObject.Null
) in the given list.
Exceptions:
-
System.ArgumentNullException: The parameter data is null.
-
PeterO.Cbor.CBORException: The byte array is not empty and does not begin with a record separator byte (0x1e), or an I/O error occurred.
public static PeterO.Cbor.CBORObject FromJSONString( string str);
Generates a CBOR object from a text string in JavaScript Object Notation (JSON) format.
If a JSON object has duplicate keys, a CBORException is thrown. This is a change in version 4.0.
Note that if a CBOR object is converted to JSON with ToJSONString
, then the JSON is converted back to CBOR with this method, the new CBOR object will not necessarily be the same as the old CBOR object, especially if the old CBOR object uses data types not supported in JSON, such as integers in map keys.
Parameters:
- str: A text string in JSON format. The entire string must contain a single JSON object and not multiple objects. The string may not begin with a byte-order mark (U+FEFF).
Return Value:
A CBOR object.
Exceptions:
-
System.ArgumentNullException: The parameter str is null.
-
PeterO.Cbor.CBORException: The string is not in JSON format.
public static PeterO.Cbor.CBORObject FromJSONString( string str, int offset, int count);
Generates a CBOR object from a text string in JavaScript Object Notation (JSON) format.
If a JSON object has duplicate keys, a CBORException is thrown. This is a change in version 4.0.
Note that if a CBOR object is converted to JSON with ToJSONString
, then the JSON is converted back to CBOR with this method, the new CBOR object will not necessarily be the same as the old CBOR object, especially if the old CBOR object uses data types not supported in JSON, such as integers in map keys.
Parameters:
-
str: A text string in JSON format. The entire string must contain a single JSON object and not multiple objects. The string may not begin with a byte-order mark (U+FEFF).
-
offset: An index, starting at 0, showing where the desired portion of str begins.
-
count: The length, in code units, of the desired portion of str (but not more than str ‘s length).
Return Value:
A CBOR object.
Exceptions:
-
System.ArgumentNullException: The parameter str is null.
-
PeterO.Cbor.CBORException: The string is not in JSON format.
-
System.ArgumentException: Either offset or count is less than 0 or greater than str ‘s length, or str ‘s length minus offset is less than count .
public static PeterO.Cbor.CBORObject FromJSONString( string str, int offset, int count, PeterO.Cbor.JSONOptions jsonoptions);
Generates a CBOR object from a text string in JavaScript Object Notation (JSON) format, using the specified options to control the decoding process. Note that if a CBOR object is converted to JSON with ToJSONString
, then the JSON is converted back to CBOR with this method, the new CBOR object will not necessarily be the same as the old CBOR object, especially if the old CBOR object uses data types not supported in JSON, such as integers in map keys.
Parameters:
-
str: The parameter str is a text string.
-
offset: An index, starting at 0, showing where the desired portion of str begins.
-
count: The length, in code units, of the desired portion of str (but not more than str ‘s length).
-
jsonoptions: The parameter jsonoptions is a Cbor.JSONOptions object.
Return Value:
A CBOR object containing the JSON data decoded.
Exceptions:
-
System.ArgumentNullException: The parameter str or jsonoptions is null.
-
PeterO.Cbor.CBORException: The string is not in JSON format.
-
System.ArgumentException: Either offset or count is less than 0 or greater than str ‘s length, or str ‘s length minus offset is less than count .
public static PeterO.Cbor.CBORObject FromJSONString( string str, PeterO.Cbor.CBOREncodeOptions options);
Deprecated. Instead, use .FromJSONString(str, new JSONOptions("allowduplicatekeys=true")) or .FromJSONString(str, new JSONOptions("allowduplicatekeys=false")), as appropriate.
Generates a CBOR object from a text string in JavaScript Object Notation (JSON) format, using the specified options to control the decoding process. Note that if a CBOR object is converted to JSON with ToJSONString
, then the JSON is converted back to CBOR with this method, the new CBOR object will not necessarily be the same as the old CBOR object, especially if the old CBOR object uses data types not supported in JSON, such as integers in map keys.
Parameters:
-
str: A text string in JSON format. The entire string must contain a single JSON object and not multiple objects. The string may not begin with a byte-order mark (U+FEFF).
-
options: Specifies options to control the decoding process. This method uses only the AllowDuplicateKeys property of this object.
Return Value:
A CBOR object containing the JSON data decoded.
Exceptions:
-
System.ArgumentNullException: The parameter str or options is null.
-
PeterO.Cbor.CBORException: The string is not in JSON format.
public static PeterO.Cbor.CBORObject FromJSONString( string str, PeterO.Cbor.JSONOptions jsonoptions);
Generates a CBOR object from a text string in JavaScript Object Notation (JSON) format, using the specified options to control the decoding process. Note that if a CBOR object is converted to JSON with ToJSONString
, then the JSON is converted back to CBOR with this method, the new CBOR object will not necessarily be the same as the old CBOR object, especially if the old CBOR object uses data types not supported in JSON, such as integers in map keys.
Parameters:
-
str: A text string in JSON format. The entire string must contain a single JSON object and not multiple objects. The string may not begin with a byte-order mark (U+FEFF).
-
jsonoptions: Specifies options to control the JSON decoding process.
Return Value:
A CBOR object containing the JSON data decoded.
Exceptions:
-
System.ArgumentNullException: The parameter str or jsonoptions is null.
-
PeterO.Cbor.CBORException: The string is not in JSON format.
public static PeterO.Cbor.CBORObject FromObject( bool value);
Returns the CBOR true value or false value, depending on “value”.
Parameters:
- value: Either
true
orfalse
.
Return Value:
CBORObject.True if value is true; otherwise CBORObject.False.
public static PeterO.Cbor.CBORObject FromObject( byte value);
Generates a CBOR object from a byte (0 to 255).
Parameters:
- value: The parameter value is a byte (from 0 to 255).
Return Value:
A CBOR object generated from the given integer.
public static PeterO.Cbor.CBORObject FromObject( byte[] bytes);
Generates a CBOR object from an array of 8-bit bytes; the byte array is copied to a new byte array in this process. (This method can’t be used to decode CBOR data from a byte array; for that, use the DecodeFromBytes method instead.).
Parameters:
- bytes: An array of 8-bit bytes; can be null.
Return Value:
A CBOR object where each element of the given byte array is copied to a new array, or CBORObject.Null if the value is null.
public static PeterO.Cbor.CBORObject FromObject( decimal value);
Converts a.NET decimal to a CBOR object.
Parameters:
- value: The parameter value is a Decimal object.
Return Value:
A CBORObject object with the same value as the.NET decimal.
public static PeterO.Cbor.CBORObject FromObject( double value);
Generates a CBOR object from a 64-bit floating-point number. The input value can be a not-a-number (NaN) value (such as Double.NaN
); however, NaN values have multiple forms that are equivalent for many applications’ purposes, and Double.NaN
is only one of these equivalent forms. In fact, CBORObject.FromObject(Double.NaN)
could produce a CBOR-encoded object that differs between DotNet and Java, because Double.NaN
may have a different form in DotNet and Java (for example, the NaN value’s sign may be negative in DotNet, but positive in Java).
Parameters:
- value: The parameter value is a 64-bit floating-point number.
Return Value:
A CBOR object generated from the given number.
public static PeterO.Cbor.CBORObject FromObject( float value);
Generates a CBOR object from a 32-bit floating-point number. The input value can be a not-a-number (NaN) value (such as Single.NaN
in DotNet or Float.NaN in Java); however, NaN values have multiple forms that are equivalent for many applications’ purposes, and Single.NaN
/ Float.NaN
is only one of these equivalent forms. In fact, CBORObject.FromObject(Single.NaN)
or CBORObject.FromObject(Float.NaN)
could produce a CBOR-encoded object that differs between DotNet and Java, because Single.NaN
/ Float.NaN
may have a different form in DotNet and Java (for example, the NaN value’s sign may be negative in DotNet, but positive in Java).
Parameters:
- value: The parameter value is a 32-bit floating-point number.
Return Value:
A CBOR object generated from the given number.
public static PeterO.Cbor.CBORObject FromObject( int value);
Generates a CBOR object from a 32-bit signed integer.
Parameters:
- value: The parameter value is a 32-bit signed integer.
Return Value:
A CBOR object.
public static PeterO.Cbor.CBORObject FromObject( int[] array);
Generates a CBOR object from an array of 32-bit integers.
Parameters:
- array: An array of 32-bit integers.
Return Value:
A CBOR array object where each element of the given array is copied to a new array, or CBORObject.Null if the value is null.
public static PeterO.Cbor.CBORObject FromObject( long value);
Generates a CBOR object from a 64-bit signed integer.
Parameters:
- value: The parameter value is a 64-bit signed integer.
Return Value:
A CBOR object.
public static PeterO.Cbor.CBORObject FromObject( long[] array);
Generates a CBOR object from an array of 64-bit integers.
Parameters:
- array: An array of 64-bit integers.
Return Value:
A CBOR array object where each element of the given array is copied to a new array, or CBORObject.Null if the value is null.
public static PeterO.Cbor.CBORObject FromObject( object obj);
Generates a CBORObject from an arbitrary object. See the overload of this method that takes CBORTypeMapper and PODOptions arguments.
Parameters:
- obj: The parameter obj
is an arbitrary object, which can be null. NOTE: For security reasons, whenever possible, an application should not base this parameter on user input or other externally supplied data unless the application limits this parameter’s inputs to types specially handled by this method (such as
int
orString
) and/or to plain-old-data types (POCO or POJO types) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
.
Return Value:
A CBOR object corresponding to the given object. Returns CBORObject.Null if the object is null.
public static PeterO.Cbor.CBORObject FromObject( object obj, PeterO.Cbor.CBORTypeMapper mapper);
Generates a CBORObject from an arbitrary object. See the overload of this method that takes CBORTypeMapper and PODOptions arguments.
Parameters:
- obj: The parameter obj
is an arbitrary object. NOTE: For security reasons, whenever possible, an application should not base this parameter on user input or other externally supplied data unless the application limits this parameter’s inputs to types specially handled by this method (such as
int
orString
) and/or to plain-old-data types (POCO or POJO types) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
.
- mapper: An object containing optional converters to convert objects of certain types to CBOR objects.
Return Value:
A CBOR object corresponding to the given object. Returns CBORObject.Null if the object is null.
Exceptions:
- System.ArgumentNullException: The parameter mapper is null.
public static PeterO.Cbor.CBORObject FromObject( object obj, PeterO.Cbor.CBORTypeMapper mapper, PeterO.Cbor.PODOptions options);
Generates a CBORObject from an arbitrary object, using the given options to control how certain objects are converted to CBOR objects. The following cases are checked in the logical order given (rather than the strict order in which they are implemented by this library):
-
null
is converted toCBORObject.Null
. -
A
CBORObject
is returned as itself. -
If the object is of a type corresponding to a type converter mentioned in the mapper parameter, that converter will be used to convert the object to a CBOR object. Type converters can be used to override the default conversion behavior of almost any object.
-
A
char
is converted to an integer (from 0 through 65535), and returns a CBOR object of that integer. (This is a change in version 4.0 from previous versions, which convertedchar
, except surrogate code points from 0xd800 through 0xdfff, into single-character text strings.) -
A
bool
(boolean
in Java) is converted toCBORObject.True
orCBORObject.False
. -
A
byte
is converted to a CBOR integer from 0 through 255. -
A primitive integer type (
int
,short
,long
, as well assbyte
,ushort
,uint
, andulong
in.NET) is converted to the corresponding CBOR integer. -
A primitive floating-point type (
float
,double
, as well asdecimal
in.NET) is converted to the corresponding CBOR number. -
A
String
is converted to a CBOR text string. To create a CBOR byte string object fromString
, see the example given in M:PeterO.Cbor.CBORObject.FromObject(System.Byte[]). -
In the.NET version, a nullable is converted to
CBORObject.Null
if the nullable’s value isnull
, or converted according to the nullable’s underlying type, if that type is supported by this method. -
In the Java version, a number of type
BigInteger
orBigDecimal
is converted to the corresponding CBOR number. -
A number of type
EDecimal
,EFloat
,EInteger
, andERational
in thePeterO.Numbers
library (in .NET) or thecom.github.peteroupc/numbers
artifact (in Java) is converted to the corresponding CBOR number. -
An array other than
byte[]
is converted to a CBOR array. In the.NET version, a multidimensional array is converted to an array of arrays. -
A
byte[]
(1-dimensional byte array) is converted to a CBOR byte string; the byte array is copied to a new byte array in this process. (This method can’t be used to decode CBOR data from a byte array; for that, use the DecodeFromBytes method instead.) -
An object implementing IDictionary (Map in Java) is converted to a CBOR map containing the keys and values enumerated.
-
An object implementing IEnumerable (Iterable in Java) is converted to a CBOR array containing the items enumerated.
-
An enumeration (
Enum
) object is converted to its underlying value in the.NET version, or the result of itsordinal()
method in the Java version. -
An object of type
DateTime
,Uri
, orGuid
(Date
,URI
, orUUID
, respectively, in Java) will be converted to a tagged CBOR object of the appropriate kind. By default,DateTime
/Date
will be converted to a tag-0 string following the date format used in the Atom syndication format, but this behavior can be changed by passing a suitable CBORTypeMapper to this method, such as a CBORTypeMapper that registers a CBORDateConverter forDateTime
orDate
objects. See the examples. -
If the object is a type not specially handled above, this method checks the obj parameter for eligible getters as follows:
-
(*) In the .NET version, eligible getters are the public, nonstatic getters of read/write properties (and also those of read-only properties in the case of a compiler-generated type or an F# type). Eligible getters also include public, nonstatic, non-
const
, non-readonly
fields. If a class has two properties and/or fields of the form “X” and “IsX”, where “X” is any name, or has multiple properties and/or fields with the same name, those properties and fields are ignored. -
(*) In the Java version, eligible getters are public, nonstatic methods starting with “get” or “is” (either word followed by a character other than a basic digit or lower-case letter, that is, other than “a” to “z” or “0” to “9”), that take no parameters and do not return void, except that methods named “getClass” are not eligible getters. In addition, public, nonstatic, nonfinal fields are also eligible getters. If a class has two otherwise eligible getters (methods and/or fields) of the form “isX” and “getX”, where “X” is the same in both, or two such getters with the same name but different return type, they are not eligible getters.
-
Then, the method returns a CBOR map with each eligible getter’s name or property name as each key, and with the corresponding value returned by that getter as that key’s value. Before adding a key-value pair to the map, the key’s name is adjusted according to the rules described in the PeterO.Cbor.PODOptions documentation. Note that for security reasons, certain types are not supported even if they contain eligible getters.
REMARK: .NET enumeration ( Enum
) constants could also have been converted to text strings with ToString()
, but that method will return multiple names if the given Enum object is a combination of Enum objects (e.g. if the object is FileAccess.Read | FileAccess.Write
). More generally, if Enums are converted to text strings, constants from Enum types with the Flags
attribute, and constants from the same Enum type that share an underlying value, should not be passed to this method.
The following example (originally written in C# for the DotNet version) uses a CBORTypeMapper to change how DateTime objects are converted to CBOR. In this case, such objects are converted to CBOR objects with tag 1 that store numbers giving the number of seconds since the start of 1970.
var conv = new CBORTypeMapper().AddConverter(typeof(DateTime), CBORDateConverter.TaggedNumber); CBORObject obj = CBORObject.FromObject(DateTime.Now, conv);
The following example generates a CBOR object from a 64-bit signed integer that is treated as a 64-bit unsigned integer (such as DotNet’s UInt64, which has no direct equivalent in the Java language), in the sense that the value is treated as 2^64 plus the original value if it’s negative.
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));
In the Java version, which has java.math.BigInteger, the following can be used instead:
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));
Parameters:
- obj: An arbitrary object to convert to a CBOR object. NOTE: For security reasons, whenever possible, an application should not base this parameter on user input or other externally supplied data unless the application limits this parameter’s inputs to types specially handled by this method (such as
int
orString
) and/or to plain-old-data types (POCO or POJO types) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
.
-
mapper: An object containing optional converters to convert objects of certain types to CBOR objects. Can be null.
-
options: An object containing options to control how certain objects are converted to CBOR objects.
Return Value:
A CBOR object corresponding to the given object. Returns CBORObject.Null if the object is null.
Exceptions:
-
System.ArgumentNullException: The parameter options is null.
-
PeterO.Cbor.CBORException: An error occurred while converting the given object to a CBOR object.
public static PeterO.Cbor.CBORObject FromObject( object obj, PeterO.Cbor.PODOptions options);
Generates a CBORObject from an arbitrary object. See the overload of this method that takes CBORTypeMapper and PODOptions arguments.
Parameters:
- obj: The parameter obj
is an arbitrary object. NOTE: For security reasons, whenever possible, an application should not base this parameter on user input or other externally supplied data unless the application limits this parameter’s inputs to types specially handled by this method (such as
int
orString
) and/or to plain-old-data types (POCO or POJO types) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
.
- options: An object containing options to control how certain objects are converted to CBOR objects.
Return Value:
A CBOR object corresponding to the given object. Returns CBORObject.Null if the object is null.
Exceptions:
- System.ArgumentNullException: The parameter options is null.
public static PeterO.Cbor.CBORObject FromObject( PeterO.Cbor.CBORObject value);
Generates a CBOR object from a CBOR object.
Parameters:
- value: The parameter value is a CBOR object.
Return Value:
Same as value , or “CBORObject.Null” is value is null.
public static PeterO.Cbor.CBORObject FromObject( PeterO.Cbor.CBORObject[] array);
Generates a CBOR object from an array of CBOR objects.
Parameters:
- array: An array of CBOR objects.
Return Value:
A CBOR object where each element of the given array is copied to a new array, or CBORObject.Null if the value is null.
public static PeterO.Cbor.CBORObject FromObject( PeterO.Numbers.EDecimal bigValue);
Generates a CBOR object from a decimal number. The CBOR object is generated as follows (this is a change in version 4.0):
-
If the number is null, returns CBORObject.Null.
-
Otherwise, if the number expresses infinity, not-a-number, or negative zero, the CBOR object will have tag 268 and the appropriate format.
-
If the number’s exponent is at least 2^64 or less than -(2^64), the CBOR object will have tag 264 and the appropriate format.
-
Otherwise, the CBOR object will have tag 4 and the appropriate format.
Parameters:
- bigValue: An arbitrary-precision decimal number. Can be null.
Return Value:
The given number encoded as a CBOR object. Returns CBORObject.Null if bigValue is null.
public static PeterO.Cbor.CBORObject FromObject( PeterO.Numbers.EFloat bigValue);
Generates a CBOR object from an arbitrary-precision binary floating-point number. The CBOR object is generated as follows (this is a change in version 4.0):
-
If the number is null, returns CBORObject.Null.
-
Otherwise, if the number expresses infinity, not-a-number, or negative zero, the CBOR object will have tag 269 and the appropriate format.
-
Otherwise, if the number’s exponent is at least 2^64 or less than -(2^64), the CBOR object will have tag 265 and the appropriate format.
-
Otherwise, the CBOR object will have tag 5 and the appropriate format.
Parameters:
- bigValue: An arbitrary-precision binary floating-point number. Can be null.
Return Value:
The given number encoded as a CBOR object. Returns CBORObject.Null if bigValue is null.
public static PeterO.Cbor.CBORObject FromObject( PeterO.Numbers.EInteger bigintValue);
Generates a CBOR object from an arbitrary-precision integer. The CBOR object is generated as follows:
-
If the number is null, returns CBORObject.Null.
-
Otherwise, if the number is greater than or equal to -(2^64) and less than 2^64, the CBOR object will have the object type Integer and the appropriate value.
-
Otherwise, the CBOR object will have tag 2 (zero or positive) or 3 (negative) and the appropriate value.
Parameters:
- bigintValue: An arbitrary-precision integer. Can be null.
Return Value:
The given number encoded as a CBOR object. Returns CBORObject.Null if bigintValue is null.
public static PeterO.Cbor.CBORObject FromObject( PeterO.Numbers.ERational bigValue);
Generates a CBOR object from an arbitrary-precision rational number. The CBOR object is generated as follows (this is a change in version 4.0):
-
If the number is null, returns CBORObject.Null.
-
Otherwise, if the number expresses infinity, not-a-number, or negative zero, the CBOR object will have tag 270 and the appropriate format.
-
Otherwise, the CBOR object will have tag 30 and the appropriate format.
Parameters:
- bigValue: An arbitrary-precision rational number. Can be null.
Return Value:
The given number encoded as a CBOR object. Returns CBORObject.Null if bigValue is null.
public static PeterO.Cbor.CBORObject FromObject( sbyte value);
This API is not CLS-compliant.
Converts a signed 8-bit integer to a CBOR object.
Parameters:
- value: The parameter value is an 8-bit signed integer.
Return Value:
A CBORObject object.
public static PeterO.Cbor.CBORObject FromObject( short value);
Generates a CBOR object from a 16-bit signed integer.
Parameters:
- value: The parameter value is a 16-bit signed integer.
Return Value:
A CBOR object generated from the given integer.
public static PeterO.Cbor.CBORObject FromObject( string strValue);
Generates a CBOR object from a text string.
Parameters:
- strValue: A text string value. Can be null.
Return Value:
A CBOR object representing the string, or CBORObject.Null if stringValue is null.
Exceptions:
- System.ArgumentException: The string contains an unpaired surrogate code point.
public static PeterO.Cbor.CBORObject FromObject( uint value);
This API is not CLS-compliant.
Converts a 32-bit unsigned integer to a CBOR object.
Parameters:
- value: A 32-bit unsigned integer.
Return Value:
A CBORObject object.
public static PeterO.Cbor.CBORObject FromObject( ulong value);
This API is not CLS-compliant.
Converts a 64-bit unsigned integer to a CBOR object.
Parameters:
- value: A 64-bit unsigned integer.
Return Value:
A CBORObject object.
public static PeterO.Cbor.CBORObject FromObject( ushort value);
This API is not CLS-compliant.
Converts a 16-bit unsigned integer to a CBOR object.
Parameters:
- value: A 16-bit unsigned integer.
Return Value:
A CBORObject object.
public static PeterO.Cbor.CBORObject FromObjectAndTag( object o, ulong tag);
This API is not CLS-compliant.
Generates a CBOR object from an arbitrary object and gives the resulting object a tag.
Parameters:
- o: The parameter o
is an arbitrary object, which can be null. NOTE: For security reasons, whenever possible, an application should not base this parameter on user input or other externally supplied data unless the application limits this parameter’s inputs to types specially handled by this method (such as
int
orString
) and/or to plain-old-data types (POCO or POJO types) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
.
- tag: A 64-bit integer that specifies a tag number. The tag number 55799 can be used to mark a “self-described CBOR” object. This document does not attempt to list all CBOR tags and their meanings. An up-to-date list can be found at the CBOR Tags registry maintained by the Internet Assigned Numbers Authority( iana.org/assignments/cbor-tags ).
Return Value:
A CBOR object where the object o is converted to a CBOR object and given the tag tag . If “valueOb” is null, returns a version of CBORObject.Null with the given tag.
public static PeterO.Cbor.CBORObject FromObjectAndTag( object valueOb, PeterO.Numbers.EInteger bigintTag);
Generates a CBOR object from an arbitrary object and gives the resulting object a tag in addition to its existing tags (the new tag is made the outermost tag).
Parameters:
- valueOb: The parameter valueOb
is an arbitrary object, which can be null. NOTE: For security reasons, whenever possible, an application should not base this parameter on user input or other externally supplied data unless the application limits this parameter’s inputs to types specially handled by this method (such as
int
orString
) and/or to plain-old-data types (POCO or POJO types) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
.
- bigintTag: Tag number. The tag number 55799 can be used to mark a “self-described CBOR” object. This document does not attempt to list all CBOR tags and their meanings. An up-to-date list can be found at the CBOR Tags registry maintained by the Internet Assigned Numbers Authority( iana.org/assignments/cbor-tags ).
Return Value:
A CBOR object where the object valueOb is converted to a CBOR object and given the tag bigintTag . If valueOb is null, returns a version of CBORObject.Null with the given tag.
Exceptions:
-
System.ArgumentException: The parameter bigintTag is less than 0 or greater than 2^64-1.
-
System.ArgumentNullException: The parameter bigintTag is null.
public static PeterO.Cbor.CBORObject FromObjectAndTag( object valueObValue, int smallTag);
Generates a CBOR object from an arbitrary object and gives the resulting object a tag in addition to its existing tags (the new tag is made the outermost tag).
Parameters:
- valueObValue: The parameter valueObValue
is an arbitrary object, which can be null. NOTE: For security reasons, whenever possible, an application should not base this parameter on user input or other externally supplied data unless the application limits this parameter’s inputs to types specially handled by this method (such as
int
orString
) and/or to plain-old-data types (POCO or POJO types) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
.
- smallTag: A 32-bit integer that specifies a tag number. The tag number 55799 can be used to mark a “self-described CBOR” object. This document does not attempt to list all CBOR tags and their meanings. An up-to-date list can be found at the CBOR Tags registry maintained by the Internet Assigned Numbers Authority ( iana.org/assignments/cbor-tags ).
Return Value:
A CBOR object where the object valueObValue is converted to a CBOR object and given the tag smallTag . If “valueOb” is null, returns a version of CBORObject.Null with the given tag.
Exceptions:
- System.ArgumentException: The parameter smallTag is less than 0.
public static PeterO.Cbor.CBORObject FromSimpleValue( int simpleValue);
Creates a CBOR object from a simple value number.
Parameters:
- simpleValue: The parameter simpleValue is a 32-bit signed integer.
Return Value:
A CBOR object.
Exceptions:
- System.ArgumentException: The parameter simpleValue is less than 0, greater than 255, or from 24 through 31.
public PeterO.Numbers.EInteger[] GetAllTags();
Gets a list of all tags, from outermost to innermost.
Return Value:
An array of tags, or the empty string if this object is untagged.
public byte[] GetByteString();
Gets the backing byte array used in this CBOR object, if this object is a byte string, without copying the data to a new byte array. Any changes in the returned array’s contents will be reflected in this CBOR object. Note, though, that the array’s length can’t be changed.
Return Value:
The byte array held by this CBOR object.
Exceptions:
- System.InvalidOperationException: This object is not a byte string.
public override int GetHashCode();
Calculates the hash code of this object. The hash code for a given instance of this class is not guaranteed to be the same across versions of this class, and no application or process IDs are used in the hash code calculation.
Return Value:
A 32-bit hash code.
public PeterO.Cbor.CBORObject GetOrDefault( object key, PeterO.Cbor.CBORObject defaultValue);
Gets the value of a CBOR object by integer index in this array or by CBOR object key in this map, or a default value if that value is not found.
Parameters:
-
key: An arbitrary object. If this is a CBOR map, this parameter is converted to a CBOR object serving as the key to the map or index to the array, and can be null. If this is a CBOR array, the key must be an integer 0 or greater and less than the size of the array, and may be any object convertible to a CBOR integer.
-
defaultValue: A value to return if an item with the given key doesn’t exist, or if the CBOR object is an array and the key is not an integer 0 or greater and less than the size of the array.
Return Value:
The CBOR object referred to by index or key in this array or map. If this is a CBOR map, returns null
(not CBORObject.Null
) if an item with the given key doesn’t exist.
public bool HasMostInnerTag( int tagValue);
Returns whether this object has an innermost tag and that tag is of the given number.
Parameters:
- tagValue: The tag number.
Return Value:
true
if this object has an innermost tag and that tag is of the given number; otherwise, false
.
Exceptions:
- System.ArgumentException: The parameter tagValue is less than 0.
public bool HasMostInnerTag( PeterO.Numbers.EInteger bigTagValue);
Returns whether this object has an innermost tag and that tag is of the given number, expressed as an arbitrary-precision number.
Parameters:
- bigTagValue: The tag number.
Return Value:
true
if this object has an innermost tag and that tag is of the given number; otherwise, false
.
Exceptions:
-
System.ArgumentNullException: The parameter bigTagValue is null.
-
System.ArgumentException: The parameter bigTagValue is less than 0.
public bool HasMostOuterTag( int tagValue);
Returns whether this object has an outermost tag and that tag is of the given number.
Parameters:
- tagValue: The tag number.
Return Value:
true
if this object has an outermost tag and that tag is of the given number; otherwise, false
.
Exceptions:
- System.ArgumentException: The parameter tagValue is less than 0.
public bool HasMostOuterTag( PeterO.Numbers.EInteger bigTagValue);
Returns whether this object has an outermost tag and that tag is of the given number.
Parameters:
- bigTagValue: The tag number.
Return Value:
true
if this object has an outermost tag and that tag is of the given number; otherwise, false
.
Exceptions:
-
System.ArgumentNullException: The parameter bigTagValue is null.
-
System.ArgumentException: The parameter bigTagValue is less than 0.
public bool HasOneTag( int tagValue);
Returns whether this object has only one tag and that tag is the given number.
Parameters:
- tagValue: The tag number.
Return Value:
true
if this object has only one tag and that tag is the given number; otherwise, false
.
Exceptions:
- System.ArgumentException: The parameter tagValue is less than 0.
public bool HasOneTag( PeterO.Numbers.EInteger bigTagValue);
Returns whether this object has only one tag and that tag is the given number, expressed as an arbitrary-precision integer.
Parameters:
- bigTagValue: An arbitrary-precision integer.
Return Value:
true
if this object has only one tag and that tag is the given number; otherwise, false
.
Exceptions:
-
System.ArgumentNullException: The parameter bigTagValue is null.
-
System.ArgumentException: The parameter bigTagValue is less than 0.
public bool HasOneTag();
Returns whether this object has only one tag.
Return Value:
true
if this object has only one tag; otherwise, false
.
public bool HasTag( int tagValue);
Returns whether this object has a tag of the given number.
Parameters:
- tagValue: The tag value to search for.
Return Value:
true
if this object has a tag of the given number; otherwise, false
.
Exceptions:
-
System.ArgumentException: The parameter tagValue is less than 0.
-
System.ArgumentNullException: The parameter tagValue is null.
public bool HasTag( PeterO.Numbers.EInteger bigTagValue);
Returns whether this object has a tag of the given number.
Parameters:
- bigTagValue: The tag value to search for.
Return Value:
true
if this object has a tag of the given number; otherwise, false
.
Exceptions:
-
System.ArgumentNullException: The parameter bigTagValue is null.
-
System.ArgumentException: The parameter bigTagValue is less than 0.
public PeterO.Cbor.CBORObject Insert( int index, object valueOb);
Inserts an object at the specified position in this CBOR array.
Parameters:
-
index: Index starting at 0 to insert at.
-
valueOb: An object representing the value, which will be converted to a CBORObject. Can be null, in which case this value is converted to CBORObject.Null.
Return Value:
This instance.
Exceptions:
-
System.InvalidOperationException: This object is not an array.
-
System.ArgumentException: The parameter valueOb has an unsupported type; or index is not a valid index into this array.
public bool IsInfinity();
Deprecated. Instead, use the following: (cbor.IsNumber && cbor.AsNumber().IsInfinity()).
Gets a value indicating whether this CBOR object represents infinity.
Return Value:
true
if this CBOR object represents infinity; otherwise, false
.
public bool IsNaN();
Deprecated. Instead, use the following: (cbor.IsNumber && cbor.AsNumber().IsNaN()).
Gets a value indicating whether this CBOR object represents a not-a-number value (as opposed to whether this object does not express a number).
Return Value:
true
if this CBOR object represents a not-a-number value (as opposed to whether this object does not represent a number as defined by the IsNumber property or isNumber()
method in Java); otherwise, false
.
public bool IsNegativeInfinity();
Deprecated. Instead, use the following: (cbor.IsNumber && cbor.AsNumber().IsNegativeInfinity()).
Gets a value indicating whether this CBOR object represents negative infinity.
Return Value:
true
if this CBOR object represents negative infinity; otherwise, false
.
public bool IsPositiveInfinity();
Deprecated. Instead, use the following: (cbor.IsNumber && cbor.AsNumber().IsPositiveInfinity()).
Gets a value indicating whether this CBOR object represents positive infinity.
Return Value:
true
if this CBOR object represents positive infinity; otherwise, false
.
public static PeterO.Cbor.CBORObject Multiply( PeterO.Cbor.CBORObject first, PeterO.Cbor.CBORObject second);
Deprecated. Instead, convert both CBOR objects to numbers (with .AsNumber()), and use the first number’s .Multiply() method.
Multiplies two CBOR numbers.
Parameters:
-
first: The parameter first is a CBOR object.
-
second: The parameter second is a CBOR object.
Return Value:
The product of the two numbers.
Exceptions:
-
System.ArgumentException: Either or both operands are not numbers (as opposed to Not-a-Number, NaN).
-
System.ArgumentNullException: The parameter first or second is null.
public PeterO.Cbor.CBORObject Negate();
Deprecated. Instead, convert this object to a number (with .AsNumber()), and use that number’s .Negate() method.
Gets this object’s value with the sign reversed.
Return Value:
The reversed-sign form of this number.
Exceptions:
- System.InvalidOperationException: This object does not represent a number (for this purpose, infinities and not-a-number or NaN values, but not CBORObject.Null, are considered numbers).
public static PeterO.Cbor.CBORObject NewArray();
Creates a new empty CBOR array.
Return Value:
A new CBOR array.
public static PeterO.Cbor.CBORObject NewMap();
Creates a new empty CBOR map that stores its keys in an undefined order.
Return Value:
A new CBOR map.
public static PeterO.Cbor.CBORObject NewOrderedMap();
Creates a new empty CBOR map that ensures that keys are stored in the order in which they are first inserted.
Return Value:
A new CBOR map.
public static PeterO.Cbor.CBORObject operator +( PeterO.Cbor.CBORObject a, PeterO.Cbor.CBORObject b);
Deprecated. May be removed in the next major version. Consider converting the objects to CBOR numbers and performing the operation there.
Does an addition on two CBOR objects and returns their result.
Parameters:
-
a: The parameter a is a CBOR object.
-
b: The parameter b is a CBOR object.
Return Value:
The sum of the two objects.
public static PeterO.Cbor.CBORObject operator /( PeterO.Cbor.CBORObject a, PeterO.Cbor.CBORObject b);
Deprecated. May be removed in the next major version. Consider converting the objects to CBOR numbers and performing the operation there.
Divides a CBORObject object by the value of a CBORObject object.
Parameters:
-
a: The parameter a is a CBOR object.
-
b: The parameter b is a CBOR object.
Return Value:
The quotient of the two objects.
public static bool operator >( PeterO.Cbor.CBORObject a, PeterO.Cbor.CBORObject b);
Returns whether one object’s value is greater than another’s.
Parameters:
-
a: The left-hand side of the comparison.
-
b: The right-hand side of the comparison.
Return Value:
true
if one object’s value is greater than another’s; otherwise, false
.
Exceptions:
- System.ArgumentNullException: The parameter a is null.
public static bool operator >=( PeterO.Cbor.CBORObject a, PeterO.Cbor.CBORObject b);
Returns whether one object’s value is at least another’s.
Parameters:
-
a: The left-hand side of the comparison.
-
b: The right-hand side of the comparison.
Return Value:
true
if one object’s value is at least another’s; otherwise, false
.
Exceptions:
- System.ArgumentNullException: The parameter a is null.
public static bool operator <( PeterO.Cbor.CBORObject a, PeterO.Cbor.CBORObject b);
Returns whether one object’s value is less than another’s.
Parameters:
-
a: The left-hand side of the comparison.
-
b: The right-hand side of the comparison.
Return Value:
true
if one object’s value is less than another’s; otherwise, false
.
Exceptions:
- System.ArgumentNullException: The parameter a is null.
public static bool operator <=( PeterO.Cbor.CBORObject a, PeterO.Cbor.CBORObject b);
Returns whether one object’s value is up to another’s.
Parameters:
-
a: The left-hand side of the comparison.
-
b: The right-hand side of the comparison.
Return Value:
true
if one object’s value is up to another’s; otherwise, false
.
Exceptions:
- System.ArgumentNullException: The parameter a is null.
public static PeterO.Cbor.CBORObject operator %( PeterO.Cbor.CBORObject a, PeterO.Cbor.CBORObject b);
Deprecated. May be removed in the next major version. Consider converting the objects to CBOR numbers and performing the operation there.
Finds the remainder that results when a CBORObject object is divided by the value of a CBORObject object.
Parameters:
-
a: The parameter a is a CBOR object.
-
b: The parameter b is a CBOR object.
Return Value:
The remainder of the two numbers.
public static PeterO.Cbor.CBORObject operator *( PeterO.Cbor.CBORObject a, PeterO.Cbor.CBORObject b);
Deprecated. May be removed in the next major version. Consider converting the objects to CBOR numbers and performing the operation there.
Multiplies a CBORObject object by the value of a CBORObject object.
Parameters:
-
a: The parameter a is a CBOR object.
-
b: The parameter b is a CBOR object.
Return Value:
The product of the two numbers.
public static PeterO.Cbor.CBORObject operator -( PeterO.Cbor.CBORObject a, PeterO.Cbor.CBORObject b);
Deprecated. May be removed in the next major version. Consider converting the objects to CBOR numbers and performing the operation there.
Subtracts a CBORObject object from a CBORObject object.
Parameters:
-
a: The parameter a is a CBOR object.
-
b: The parameter b is a CBOR object.
Return Value:
The difference of the two objects.
public static PeterO.Cbor.CBORObject Read( System.IO.Stream stream);
Reads an object in CBOR format from a data stream. This method will read from the stream until the end of the CBOR object is reached or an error occurs, whichever happens first.
Parameters:
- stream: A readable data stream.
Return Value:
A CBOR object that was read.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data.
public static PeterO.Cbor.CBORObject Read( System.IO.Stream stream, PeterO.Cbor.CBOREncodeOptions options);
Reads an object in CBOR format from a data stream, using the specified options to control the decoding process. This method will read from the stream until the end of the CBOR object is reached or an error occurs, whichever happens first.
Parameters:
-
stream: A readable data stream.
-
options: Specifies the options to use when decoding the CBOR data stream. See CBOREncodeOptions for more information.
Return Value:
A CBOR object that was read.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data.
public static PeterO.Cbor.CBORObject ReadJSON( System.IO.Stream stream);
Generates a CBOR object from a data stream in JavaScript Object Notation (JSON) format. The JSON stream may begin with a byte-order mark (U+FEFF). Since version 2.0, the JSON stream can be in UTF-8, UTF-16, or UTF-32 encoding; the encoding is detected by assuming that the first character read must be a byte-order mark or a nonzero basic character (U+0001 to U+007F). (In previous versions, only UTF-8 was allowed.). (This behavior may change to supporting only UTF-8, with or without a byte order mark, in version 5.0 or later, perhaps with an option to restore the previous behavior of also supporting UTF-16 and UTF-32.).
Parameters:
- stream: A readable data stream. The sequence of bytes read from the data stream must contain a single JSON object and not multiple objects.
Return Value:
A CBOR object.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
-
PeterO.Cbor.CBORException: The data stream contains invalid encoding or is not in JSON format.
public static PeterO.Cbor.CBORObject ReadJSON( System.IO.Stream stream, PeterO.Cbor.CBOREncodeOptions options);
Deprecated. Instead, use .ReadJSON(stream, new JSONOptions("allowduplicatekeys=true")) or .ReadJSON(stream, new JSONOptions("allowduplicatekeys=false")), as appropriate.
Generates a CBOR object from a data stream in JavaScript Object Notation (JSON) format, using the specified options to control the decoding process. The JSON stream may begin with a byte-order mark (U+FEFF). Since version 2.0, the JSON stream can be in UTF-8, UTF-16, or UTF-32 encoding; the encoding is detected by assuming that the first character read must be a byte-order mark or a nonzero basic character (U+0001 to U+007F). (In previous versions, only UTF-8 was allowed.).
Parameters:
-
stream: A readable data stream. The sequence of bytes read from the data stream must contain a single JSON object and not multiple objects.
-
options: Contains options to control the JSON decoding process. This method uses only the AllowDuplicateKeys property of this object.
Return Value:
A CBOR object containing the JSON data decoded.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
-
PeterO.Cbor.CBORException: The data stream contains invalid encoding or is not in JSON format.
public static PeterO.Cbor.CBORObject ReadJSON( System.IO.Stream stream, PeterO.Cbor.JSONOptions jsonoptions);
Generates a CBOR object from a data stream in JavaScript Object Notation (JSON) format, using the specified options to control the decoding process. The JSON stream may begin with a byte-order mark (U+FEFF). Since version 2.0, the JSON stream can be in UTF-8, UTF-16, or UTF-32 encoding; the encoding is detected by assuming that the first character read must be a byte-order mark or a nonzero basic character (U+0001 to U+007F). (In previous versions, only UTF-8 was allowed.). (This behavior may change to supporting only UTF-8, with or without a byte order mark, in version 5.0 or later, perhaps with an option to restore the previous behavior of also supporting UTF-16 and UTF-32.).
Parameters:
-
stream: A readable data stream. The sequence of bytes read from the data stream must contain a single JSON object and not multiple objects.
-
jsonoptions: Specifies options to control how the JSON stream is decoded to CBOR. See the JSONOptions class.
Return Value:
A CBOR object containing the JSON data decoded.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
-
PeterO.Cbor.CBORException: The data stream contains invalid encoding or is not in JSON format.
public static PeterO.Cbor.CBORObject[] ReadJSONSequence( System.IO.Stream stream);
Generates a list of CBOR objects from a data stream in JavaScript Object Notation (JSON) text sequence format (RFC 7464). The data stream must be in UTF-8 encoding and may not begin with a byte-order mark (U+FEFF).
Generally, each JSON text in a JSON text sequence is written as follows: Write a record separator byte (0x1e), then write the JSON text in UTF-8 (without a byte order mark, U+FEFF), then write the line feed byte (0x0a). RFC 7464, however, uses a more liberal syntax for parsing JSON text sequences.
Parameters:
- stream: A readable data stream. The sequence of bytes read from the data stream must either be empty or begin with a record separator byte (0x1e).
Return Value:
A list of CBOR objects read from the JSON sequence. Objects that could not be parsed are replaced with null
(as opposed to CBORObject.Null
) in the given list.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
-
PeterO.Cbor.CBORException: The data stream is not empty and does not begin with a record separator byte (0x1e).
public static PeterO.Cbor.CBORObject[] ReadJSONSequence( System.IO.Stream stream, PeterO.Cbor.JSONOptions jsonoptions);
Generates a list of CBOR objects from a data stream in JavaScript Object Notation (JSON) text sequence format (RFC 7464). The data stream must be in UTF-8 encoding and may not begin with a byte-order mark (U+FEFF).
Generally, each JSON text in a JSON text sequence is written as follows: Write a record separator byte (0x1e), then write the JSON text in UTF-8 (without a byte order mark, U+FEFF), then write the line feed byte (0x0a). RFC 7464, however, uses a more liberal syntax for parsing JSON text sequences.
Parameters:
-
stream: A readable data stream. The sequence of bytes read from the data stream must either be empty or begin with a record separator byte (0x1e).
-
jsonoptions: Specifies options to control how JSON texts in the stream are decoded to CBOR. See the JSONOptions class.
Return Value:
A list of CBOR objects read from the JSON sequence. Objects that could not be parsed are replaced with null
(as opposed to CBORObject.Null
) in the given list.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
-
PeterO.Cbor.CBORException: The data stream is not empty and does not begin with a record separator byte (0x1e).
public static PeterO.Cbor.CBORObject[] ReadSequence( System.IO.Stream stream);
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 reached or an error occurs, whichever happens first.
Parameters:
- stream: A readable data stream.
Return Value:
An array containing the CBOR objects that were read from the data stream. Returns an empty array if there is no unread data in the stream.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null, or the parameter “options” is null.
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data, including if the last CBOR object was read only partially.
public static PeterO.Cbor.CBORObject[] ReadSequence( System.IO.Stream stream, PeterO.Cbor.CBOREncodeOptions options);
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 reached or an error occurs, whichever happens first.
Parameters:
-
stream: A readable data stream.
-
options: Specifies the options to use when decoding the CBOR data stream. See CBOREncodeOptions for more information. In this method, the AllowEmpty property is treated as set regardless of the value of that property specified in this parameter.
Return Value:
An array containing the CBOR objects that were read from the data stream. Returns an empty array if there is no unread data in the stream.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null, or the parameter options is null.
-
PeterO.Cbor.CBORException: There was an error in reading or parsing the data, including if the last CBOR object was read only partially.
public static PeterO.Cbor.CBORObject Remainder( PeterO.Cbor.CBORObject first, PeterO.Cbor.CBORObject second);
Deprecated. Instead, convert both CBOR objects to numbers (with .AsNumber()), and use the first number’s .Remainder() method.
Finds the remainder that results when a CBORObject object is divided by the value of a CBOR object.
Parameters:
-
first: The parameter first is a CBOR object.
-
second: The parameter second is a CBOR object.
Return Value:
The remainder of the two numbers.
Exceptions:
- System.ArgumentNullException: The parameter first or second is null.
public bool Remove( object obj);
If this object is an array, removes the first instance of the specified item (once converted to a CBOR object) from the array. If this object is a map, removes the item with the given key (once converted to a CBOR object) from the map.
Parameters:
- obj: The item or key (once converted to a CBOR object) to remove.
Return Value:
true
if the item was removed; otherwise, false
.
Exceptions:
-
System.ArgumentNullException: The parameter obj is null (as opposed to CBORObject.Null).
-
System.InvalidOperationException: The object is not an array or map.
public bool Remove( PeterO.Cbor.CBORObject obj);
If this object is an array, removes the first instance of the specified item from the array. If this object is a map, removes the item with the given key from the map.
Parameters:
- obj: The item or key to remove.
Return Value:
true
if the item was removed; otherwise, false
.
Exceptions:
-
System.ArgumentNullException: The parameter obj is null (as opposed to CBORObject.Null).
-
System.InvalidOperationException: The object is not an array or map.
public bool RemoveAt( int index);
Removes the item at the given index of this CBOR array.
Parameters:
- index: The index, starting at 0, of the item to remove.
Return Value:
Returns “true” if the object was removed. Returns “false” if the given index is less than 0, or is at least as high as the number of items in the array.
Exceptions:
- System.InvalidOperationException: This object is not a CBOR array.
public PeterO.Cbor.CBORObject Set( object key, object valueOb);
Maps an object to a key in this CBOR map, or adds the value if the key doesn’t exist. If this is a CBOR array, instead sets the value at the given index to the given value.
Parameters:
-
key: If this instance is a CBOR map, this parameter is an object representing the key, which will be converted to a CBORObject; in this case, this parameter can be null, in which case this value is converted to CBORObject.Null. If this instance is a CBOR array, this parameter must be a 32-bit signed integer(
int
) identifying the index (starting from 0) of the item to set in the array. -
valueOb: An object representing the value, which will be converted to a CBORObject. Can be null, in which case this value is converted to CBORObject.Null.
Return Value:
This instance.
Exceptions:
-
System.InvalidOperationException: This object is not a map or an array.
-
System.ArgumentException: The parameter key or valueOb has an unsupported type, or this instance is a CBOR array and key is less than 0, is the size of this array or greater, or is not a 32-bit signed integer (
int
).
public static PeterO.Cbor.CBORObject Subtract( PeterO.Cbor.CBORObject first, PeterO.Cbor.CBORObject second);
Deprecated. Instead, convert both CBOR objects to numbers (with .AsNumber()), and use the first number’s .Subtract() method.
Finds the difference between two CBOR number objects.
Parameters:
-
first: The parameter first is a CBOR object.
-
second: The parameter second is a CBOR object.
Return Value:
The difference of the two objects.
Exceptions:
-
System.ArgumentException: Either or both operands are not numbers (as opposed to Not-a-Number, NaN).
-
System.ArgumentNullException: The parameter first or second is null.
public byte[] ToJSONBytes( PeterO.Cbor.JSONOptions jsonoptions);
Converts this object to a byte array in JavaScript Object Notation (JSON) format. The JSON text will be written out in UTF-8 encoding, without a byte order mark, to the byte array. See the overload to ToJSONString taking a JSONOptions argument for further information.
Parameters:
- jsonoptions: Specifies options to control writing the CBOR object to JSON.
Return Value:
A byte array containing the converted object in JSON format.
Exceptions:
- System.ArgumentNullException: The parameter jsonoptions is null.
public byte[] ToJSONBytes();
Converts this object to a byte array in JavaScript Object Notation (JSON) format. The JSON text will be written out in UTF-8 encoding, without a byte order mark, to the byte array. See the overload to ToJSONString taking a JSONOptions argument for further information.
Return Value:
A byte array containing the converted in JSON format.
public string ToJSONString( PeterO.Cbor.JSONOptions options);
Converts this object to a text string in JavaScript Object Notation (JSON) format, using the specified options to control the encoding process. This function works not only with arrays and maps, but also integers, strings, byte arrays, and other JSON data types. Notes:
-
If this object contains maps with non-string keys, the keys are converted to JSON strings before writing the map as a JSON string.
-
If this object represents a number (the IsNumber property, or isNumber() method in Java, returns true), then it is written out as a number.
-
If the CBOR object contains CBOR maps, or is a CBOR map itself, the order in which the keys to the map are written out to the JSON string is undefined unless the map was created using the NewOrderedMap method. Map keys other than untagged text strings are converted to JSON strings before writing them out (for example,
22("Test")
is converted to"Test"
andtrue
is converted to"true"
). After such conversion, if two or more keys for the same map are identical, this method throws a CBORException. -
If a number in the form of an arbitrary-precision binary floating-point number has a very high binary exponent, it will be converted to a double before being converted to a JSON string. (The resulting double could overflow to infinity, in which case the arbitrary-precision binary floating-point number is converted to null.)
-
The string will not begin with a byte-order mark (U+FEFF); RFC 8259 (the JSON specification) forbids placing a byte-order mark at the beginning of a JSON string.
-
Byte strings are converted to Base64 URL without whitespace or padding by default (see section 3.4.5.3 of RFC 8949). A byte string will instead be converted to traditional base64 without whitespace and with padding if it has tag 22, or base16 for tag 23. (To create a CBOR object with a given tag, call the
CBORObject.FromObjectAndTag
method and pass the CBOR object and the desired tag number to that method.) -
Rational numbers will be converted to their exact form, if possible, otherwise to a high-precision approximation. (The resulting approximation could overflow to infinity, in which case the rational number is converted to null.)
-
Simple values other than true and false will be converted to null. (This doesn’t include floating-point numbers.)
-
Infinity and not-a-number will be converted to null.
Warning: In general, if this CBOR object contains integer map keys or uses other features not supported in JSON, and the application converts this CBOR object to JSON and back to CBOR, the application should not expect the new CBOR object to be exactly the same as the original. This is because the conversion in many cases may have to convert unsupported features in JSON to supported features which correspond to a different feature in CBOR (such as converting integer map keys, which are supported in CBOR but not JSON, to text strings, which are supported in both).
The example code given below (originally written in C# for the.NET version) can be used to write out certain keys of a CBOR map in a given order to a JSON string.
/* 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(); }
.
Parameters:
- options: Specifies options to control writing the CBOR object to JSON.
Return Value:
A text string containing the converted object in JSON format.
Exceptions:
- System.ArgumentNullException: The parameter options is null.
public string ToJSONString();
Converts this object to a text string in JavaScript Object Notation (JSON) format. See the overload to ToJSONString taking a JSONOptions argument for further information. If the CBOR object contains CBOR maps, or is a CBOR map itself, the order in which the keys to the map are written out to the JSON string is undefined unless the map was created using the NewOrderedMap method. Map keys other than untagged text strings are converted to JSON strings before writing them out (for example, 22("Test")
is converted to "Test"
and true
is converted to "true"
). After such conversion, if two or more keys for the same map are identical, this method throws a CBORException. The example code given in PeterO.Cbor.CBORObject.ToJSONString(PeterO.Cbor.JSONOptions) can be used to write out certain keys of a CBOR map in a given order to a JSON string, or to write out a CBOR object as part of a JSON text sequence.
Warning: In general, if this CBOR object contains integer map keys or uses other features not supported in JSON, and the application converts this CBOR object to JSON and back to CBOR, the application should not expect the new CBOR object to be exactly the same as the original. This is because the conversion in many cases may have to convert unsupported features in JSON to supported features which correspond to a different feature in CBOR (such as converting integer map keys, which are supported in CBOR but not JSON, to text strings, which are supported in both).
Return Value:
A text string containing the converted object in JSON format.
public object ToObject( System.Type t);
Converts this CBOR object to an object of an arbitrary type. See the documentation for the overload of this method taking a CBORTypeMapper parameter for more information. This method doesn’t use a CBORTypeMapper parameter to restrict which data types are eligible for Plain-Old-Data serialization.
Java offers no easy way to express a generic type, at least none as easy as C#’s typeof
operator. The following example, written in Java, is a way to specify that the return value will be an ArrayList of String objects.
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);
By comparison, the C# version is much shorter.
var array = (List<String>)cborArray.ToObject( typeof(List<String>));
.
Parameters:
- t: The type, class, or interface that this method’s return value will belong to. To express a generic type in Java, see the example. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method (such as
int
orString
) or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
Return Value:
The converted object.
Exceptions:
-
PeterO.Cbor.CBORException: The given type t , or this object’s CBOR type, is not supported, or the given object’s nesting is too deep, or another error occurred when serializing the object.
-
System.ArgumentNullException: The parameter t is null.
public object ToObject( System.Type t, PeterO.Cbor.CBORTypeMapper mapper);
Converts this CBOR object to an object of an arbitrary type. See the documentation for the overload of this method taking a CBORTypeMapper and PODOptions parameters parameters for more information.
Parameters:
-
t: The type, class, or interface that this method’s return value will belong to. To express a generic type in Java, see the example. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method (such as
int
orString
) or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above. -
mapper: This parameter controls which data types are eligible for Plain-Old-Data deserialization and includes custom converters from CBOR objects to certain data types.
Return Value:
The converted object.
Exceptions:
-
PeterO.Cbor.CBORException: The given type t , or this object’s CBOR type, is not supported, or the given object’s nesting is too deep, or another error occurred when serializing the object.
-
System.ArgumentNullException: The parameter t is null.
public object ToObject( System.Type t, PeterO.Cbor.CBORTypeMapper mapper, PeterO.Cbor.PODOptions options);
Converts this CBOR object to an object of an arbitrary type. The following cases are checked in the logical order given (rather than the strict order in which they are implemented by this library):
-
If the type is
CBORObject
, return this object. -
If the given object is
CBORObject.Null
(with or without tags), returnsnull
. -
If the object is of a type corresponding to a type converter mentioned in the mapper parameter, that converter will be used to convert the CBOR object to an object of the given type. Type converters can be used to override the default conversion behavior of almost any object.
-
If the type is
object
, return this object. -
If the type is
char
, converts single-character CBOR text strings and CBOR integers from 0 through 65535 to achar
object and returns thatchar
object. -
If the type is
bool
(boolean
in Java), returns the result of AsBoolean. -
If the type is
short
, returns this number as a 16-bit signed integer after converting its value to an integer by discarding its fractional part, and throws an exception if this object’s value is infinity or a not-a-number value, or does not represent a number (currently InvalidOperationException, but may change in the next major version), or if the value, once converted to an integer by discarding its fractional part, is less than -32768 or greater than 32767 (currently OverflowException, but may change in the next major version). -
If the type is
long
, returns this number as a 64-bit signed integer after converting its value to an integer by discarding its fractional part, and throws an exception if this object’s value is infinity or a not-a-number value, or does not represent a number (currently InvalidOperationException, but may change in the next major version), or if the value, once converted to an integer by discarding its fractional part, is less than -2^63 or greater than 2^63-1 (currently OverflowException, but may change in the next major version). -
If the type is
short
, the same rules as forlong
are used, but the range is from -32768 through 32767 and the return type isshort
. -
If the type is
byte
, the same rules as forlong
are used, but the range is from 0 through 255 and the return type isbyte
. -
If the type is
sbyte
, the same rules as forlong
are used, but the range is from -128 through 127 and the return type issbyte
. -
If the type is
ushort
, the same rules as forlong
are used, but the range is from 0 through 65535 and the return type isushort
. -
If the type is
uint
, the same rules as forlong
are used, but the range is from 0 through 2^31-1 and the return type isuint
. -
If the type is
ulong
, the same rules as forlong
are used, but the range is from 0 through 2^63-1 and the return type isulong
. -
If the type is
int
or a primitive floating-point type (float
,double
, as well asdecimal
in.NET), returns the result of the corresponding As* method. -
If the type is
String
, returns the result of AsString. -
If the type is
EFloat
,EDecimal
,EInteger
, orERational
in thePeterO.Numbers
library (in .NET) or thecom.github.peteroupc/numbers
artifact (in Java), or if the type isBigInteger
orBigDecimal
in the Java version, converts the given object to a number of the corresponding type and throws an exception (currently InvalidOperationException) if the object does not represent a number (for this purpose, infinity and not-a-number values, but notCBORObject.Null
, are considered numbers). Currently, this is equivalent to the result ofAsEFloat()
,AsEDecimal()
,AsEInteger
, orAsERational()
, respectively, but may change slightly in the next major version. Note that in the case ofEFloat
, if this object represents a decimal number with a fractional part, the conversion may lose information depending on the number, and if the object is a rational number with a nonterminating binary expansion, the number returned is a binary floating-point number rounded to a high but limited precision. In the case ofEDecimal
, if this object expresses a rational number with a nonterminating decimal expansion, returns a decimal number rounded to 34 digits of precision. In the case ofEInteger
, if this CBOR object expresses a floating-point number, it is converted to an integer by discarding its fractional part, and if this CBOR object expresses a rational number, it is converted to an integer by dividing the numerator by the denominator and discarding the fractional part of the result, and this method throws an exception (currently OverflowException, but may change in the next major version) if this object expresses infinity or a not-a-number value. -
In the.NET version, if the type is a nullable (e.g.,
Nullable<int>
orint?
, returnsnull
if this CBOR object is null, or this object’s value converted to the nullable’s underlying type, e.g.,int
. -
If the type is an enumeration (
Enum
) type and this CBOR object is a text string or an integer, returns the appropriate enumerated constant. (For example, ifMyEnum
includes an entry forMyValue
, this method will returnMyEnum.MyValue
if the CBOR object represents"MyValue"
or the underlying value forMyEnum.MyValue
.) Note: If an integer is converted to a.NET Enum constant, and that integer is shared by more than one constant of the same type, it is undefined which constant from among them is returned. (For example, ifMyEnum.Zero=0
andMyEnum.Null=0
, converting 0 toMyEnum
may return eitherMyEnum.Zero
orMyEnum.Null
.) As a result, .NET Enum types with constants that share an underlying value should not be passed to this method. -
If the type is
byte[]
(a one-dimensional byte array) and this CBOR object is a byte string, returns a byte array which this CBOR byte string’s data will be copied to. (This method can’t be used to encode CBOR data to a byte array; for that, use the EncodeToBytes method instead.) -
If the type is a one-dimensional or multidimensional array type and this CBOR object is an array, returns an array containing the items in this CBOR object.
-
If the type is List, ReadOnlyCollection or the generic or non-generic IList, ICollection, IEnumerable, IReadOnlyCollection, or IReadOnlyList (or ArrayList, List, Collection, or Iterable in Java), and if this CBOR object is an array, returns an object conforming to the type, class, or interface passed to this method, where the object will contain all items in this CBOR array.
-
If the type is Dictionary, ReadOnlyDictionary or the generic or non-generic IDictionary or IReadOnlyDictionary (or HashMap or Map in Java), and if this CBOR object is a map, returns an object conforming to the type, class, or interface passed to this method, where the object will contain all keys and values in this CBOR map.
-
If the type is an enumeration constant (“enum”), and this CBOR object is an integer or text string, returns the enumeration constant with the given number or name, respectively. (Enumeration constants made up of multiple enumeration constants, as allowed by .NET, can only be matched by number this way.)
-
If the type is
DateTime
(orDate
in Java) , returns a date/time object if the CBOR object’s outermost tag is 0 or 1. For tag 1, this method treats the CBOR object as a number of seconds since the start of 1970, which is based on the POSIX definition of “seconds since the Epoch”, a definition that does not count leap seconds. In this method, this number of seconds assumes the use of a proleptic Gregorian calendar, in which the rules regarding the number of days in each month and which years are leap years are the same for all years as they were in 1970 (including without regard to time zone differences or transitions from other calendars to the Gregorian). The string format used in tag 0 supports only years up to 4 decimal digits long. For tag 1, CBOR objects that express infinity or not-a-number (NaN) are treated as invalid by this method. This default behavior forDateTime
andDate
can be changed by passing a suitable CBORTypeMapper to this method, such as a CBORTypeMapper that registers a CBORDateConverter forDateTime
orDate
objects. See the examples. -
If the type is
Uri
(orURI
in Java), returns a URI object if possible. -
If the type is
Guid
(orUUID
in Java), returns a UUID object if possible. -
Plain-Old-Data deserialization: If the object is a type not specially handled above, the type includes a zero-parameter constructor (default or not), this CBOR object is a CBOR map, and the “mapper” parameter (if any) allows this type to be eligible for Plain-Old-Data deserialization, then this method checks the given type for eligible setters as follows:
-
(*) In the .NET version, eligible setters are the public, nonstatic setters of properties with a public, nonstatic getter. Eligible setters also include public, nonstatic, non-
const
, non-readonly
fields. If a class has two properties and/or fields of the form “X” and “IsX”, where “X” is any name, or has multiple properties and/or fields with the same name, those properties and fields are ignored. -
(*) In the Java version, eligible setters are public, nonstatic methods starting with “set” followed by a character other than a basic digit or lower-case letter, that is, other than “a” to “z” or “0” to “9”, that take one parameter. The class containing an eligible setter must have a public, nonstatic method with the same name, but starting with “get” or “is” rather than “set”, that takes no parameters and does not return void. (For example, if a class has “public setValue(String)” and “public getValue()”, “setValue” is an eligible setter. However, “setValue()” and “setValue(String, int)” are not eligible setters.) In addition, public, nonstatic, nonfinal fields are also eligible setters. If a class has two or more otherwise eligible setters (methods and/or fields) with the same name, but different parameter type, they are not eligible setters.
-
Then, the method creates an object of the given type and invokes each eligible setter with the corresponding value in the CBOR map, if any. Key names in the map are matched to eligible setters according to the rules described in the PeterO.Cbor.PODOptions documentation. Note that for security reasons, certain types are not supported even if they contain eligible setters. For the Java version, the object creation may fail in the case of a nested nonstatic class.
The following example (originally written in C# for the DotNet version) uses a CBORTypeMapper to change how CBOR objects are converted to DateTime objects. In this case, the ToObject method assumes the CBOR object is an untagged number giving the number of seconds since the start of 1970.
var conv = new CBORTypeMapper().AddConverter(typeof(DateTime), CBORDateConverter.UntaggedNumber); var obj = CBORObject.FromObject().ToObject<DateTime>(conv);
Java offers no easy way to express a generic type, at least none as easy as C#’s typeof
operator. The following example, written in Java, is a way to specify that the return value will be an ArrayList of String objects.
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);
By comparison, the C# version is much shorter.
var array = (List<String>)cborArray.ToObject( typeof(List<String>));
.
Parameters:
-
t: The type, class, or interface that this method’s return value will belong to. To express a generic type in Java, see the example. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method, such as
int
orString
, or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above. -
mapper: This parameter controls which data types are eligible for Plain-Old-Data deserialization and includes custom converters from CBOR objects to certain data types. Can be null.
-
options: Specifies options for controlling deserialization of CBOR objects.
Return Value:
The converted object.
Exceptions:
-
PeterO.Cbor.CBORException: The given type t , or this object’s CBOR type, is not supported, or the given object’s nesting is too deep, or another error occurred when serializing the object.
-
System.ArgumentNullException: The parameter t or options is null.
public object ToObject( System.Type t, PeterO.Cbor.PODOptions options);
Converts this CBOR object to an object of an arbitrary type. See the documentation for the overload of this method taking a CBORTypeMapper and PODOptions parameters for more information. This method (without a CBORTypeMapper parameter) allows all data types not otherwise handled to be eligible for Plain-Old-Data serialization.
Parameters:
-
t: The type, class, or interface that this method’s return value will belong to. To express a generic type in Java, see the example. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method (such as
int
orString
) or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above. -
options: Specifies options for controlling deserialization of CBOR objects.
Return Value:
The converted object.
Exceptions:
-
System.NotSupportedException: The given type t , or this object’s CBOR type, is not supported.
-
System.ArgumentNullException: The parameter t is null.
-
PeterO.Cbor.CBORException: The given object’s nesting is too deep, or another error occurred when serializing the object.
public T ToObject<T>( PeterO.Cbor.CBORTypeMapper mapper);
Converts this CBOR object to an object of an arbitrary type. See M:PeterO.Cbor.CBORObject.ToObject(System.Type) for further information.
Parameters:
-
mapper: This parameter controls which data types are eligible for Plain-Old-Data deserialization and includes custom converters from CBOR objects to certain data types.
-
<T>: The type, class, or interface that this method’s return value will belong to. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method (such as
int
orString
) or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
Return Value:
The converted object.
Exceptions:
- System.NotSupportedException: The given type “T”, or this object’s CBOR type, is not supported.
public T ToObject<T>( PeterO.Cbor.CBORTypeMapper mapper, PeterO.Cbor.PODOptions options);
Converts this CBOR object to an object of an arbitrary type. See M:PeterO.Cbor.CBORObject.ToObject(System.Type) for further information.
Parameters:
-
mapper: This parameter controls which data types are eligible for Plain-Old-Data deserialization and includes custom converters from CBOR objects to certain data types.
-
options: Specifies options for controlling deserialization of CBOR objects.
-
<T>: The type, class, or interface that this method’s return value will belong to. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method (such as
int
orString
) or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
Return Value:
The converted object.
Exceptions:
- System.NotSupportedException: The given type “T”, or this object’s CBOR type, is not supported.
public T ToObject<T>( PeterO.Cbor.PODOptions options);
Converts this CBOR object to an object of an arbitrary type. See M:PeterO.Cbor.CBORObject.ToObject(System.Type) for further information.
Parameters:
-
options: Specifies options for controlling deserialization of CBOR objects.
-
<T>: The type, class, or interface that this method’s return value will belong to. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method (such as
int
orString
) or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
Return Value:
The converted object.
Exceptions:
- System.NotSupportedException: The given type “T”, or this object’s CBOR type, is not supported.
public T ToObject<T>();
Converts this CBOR object to an object of an arbitrary type. See M:PeterO.Cbor.CBORObject.ToObject(System.Type) for further information.
Parameters:
- <T>: The type, class, or interface that this method’s return value will belong to. Note: For security reasons, an application should not base this parameter on user input or other externally supplied data. Whenever possible, this parameter should be either a type specially handled by this method (such as
int
orString
) or a plain-old-data type (POCO or POJO type) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
Return Value:
The converted object.
Exceptions:
- System.NotSupportedException: The given type “T”, or this object’s CBOR type, is not supported.
public override string ToString();
Returns this CBOR object in a text form intended to be read by humans. The value returned by this method is not intended to be parsed by computer programs, and the exact text of the value may change at any time between versions of this library. The returned string is not necessarily in JavaScript Object Notation (JSON); to convert CBOR objects to JSON strings, use the PeterO.Cbor.CBORObject.ToJSONString(PeterO.Cbor.JSONOptions) method instead.
Return Value:
A text representation of this object.
public PeterO.Cbor.CBORObject Untag();
Gets an object with the same value as this one but without the tags it has, if any. If this object is an array, map, or byte string, the data will not be copied to the returned object, so changes to the returned object will be reflected in this one.
Return Value:
A CBOR object.
public PeterO.Cbor.CBORObject UntagOne();
Gets an object with the same value as this one but without this object’s outermost tag, if any. If this object is an array, map, or byte string, the data will not be copied to the returned object, so changes to the returned object will be reflected in this one.
Return Value:
A CBOR object.
public PeterO.Cbor.CBORObject WithTag( int smallTag);
Generates a CBOR object from an arbitrary object and gives the resulting object a tag in addition to its existing tags (the new tag is made the outermost tag).
Parameters:
- smallTag: A 32-bit integer that specifies a tag number. The tag number 55799 can be used to mark a “self-described CBOR” object. This document does not attempt to list all CBOR tags and their meanings. An up-to-date list can be found at the CBOR Tags registry maintained by the Internet Assigned Numbers Authority ( iana.org/assignments/cbor-tags ).
Return Value:
A CBOR object with the same value as this one but given the tag smallTag in addition to its existing tags (the new tag is made the outermost tag).
Exceptions:
- System.ArgumentException: The parameter smallTag is less than 0.
public PeterO.Cbor.CBORObject WithTag( PeterO.Numbers.EInteger bigintTag);
Generates a CBOR object from this one, but gives the resulting object a tag in addition to its existing tags (the new tag is made the outermost tag).
Parameters:
- bigintTag: Tag number. The tag number 55799 can be used to mark a “self-described CBOR” object. This document does not attempt to list all CBOR tags and their meanings. An up-to-date list can be found at the CBOR Tags registry maintained by the Internet Assigned Numbers Authority( iana.org/assignments/cbor-tags ).
Return Value:
A CBOR object with the same value as this one but given the tag bigintTag in addition to its existing tags (the new tag is made the outermost tag).
Exceptions:
-
System.ArgumentException: The parameter bigintTag is less than 0 or greater than 2^64-1.
-
System.ArgumentNullException: The parameter bigintTag is null.
public PeterO.Cbor.CBORObject WithTag( ulong tag);
This API is not CLS-compliant.
Generates a CBOR object from this one, but gives the resulting object a tag in addition to its existing tags (the new tag is made the outermost tag).
Parameters:
- tag: A 64-bit integer that specifies a tag number. The tag number 55799 can be used to mark a “self-described CBOR” object. This document does not attempt to list all CBOR tags and their meanings. An up-to-date list can be found at the CBOR Tags registry maintained by the Internet Assigned Numbers Authority( iana.org/assignments/cbor-tags ).
Return Value:
A CBOR object with the same value as this one but given the tag tag in addition to its existing tags (the new tag is made the outermost tag).
public static void Write( bool value, System.IO.Stream stream);
Writes a Boolean value in CBOR format to a data stream.
Parameters:
-
value: The value to write.
-
stream: A writable data stream.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
public static void Write( byte value, System.IO.Stream stream);
Writes a byte (0 to 255) in CBOR format to a data stream. If the value is less than 24, writes that byte. If the value is 25 to 255, writes the byte 24, then this byte’s value.
Parameters:
-
value: The value to write.
-
stream: A writable data stream.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
public static void Write( double value, System.IO.Stream stream);
Writes a 64-bit floating-point number in CBOR format to a data stream. The number is written using the shortest floating-point encoding possible; this is a change from previous versions.
Parameters:
-
value: The value to write.
-
stream: A writable data stream.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
public static void Write( float value, System.IO.Stream stream);
Writes a 32-bit floating-point number in CBOR format to a data stream. The number is written using the shortest floating-point encoding possible; this is a change from previous versions.
Parameters:
-
value: The value to write.
-
stream: A writable data stream.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
public static void Write( int value, System.IO.Stream stream);
Writes a 32-bit signed integer in CBOR format to a data stream.
Parameters:
-
value: The value to write.
-
stream: A writable data stream.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
public static void Write( long value, System.IO.Stream stream);
Writes a 64-bit signed integer in CBOR format to a data stream.
Parameters:
-
value: The value to write.
-
stream: A writable data stream.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
public static void Write( object objValue, System.IO.Stream output, PeterO.Cbor.CBOREncodeOptions options);
Writes an arbitrary object to a CBOR data stream, using the specified options for controlling how the object is encoded to CBOR data format. If the object is convertible to a CBOR map or a CBOR object that contains CBOR maps, the order in which the keys to those maps are written out to the data stream is undefined unless the map was created using the NewOrderedMap method. The example code given in M:PeterO.Cbor.CBORObject.WriteTo(System.IO.Stream) can be used to write out certain keys of a CBOR map in a given order. Currently, the following objects are supported:
-
Lists of CBORObject.
-
Maps of CBORObject. The order in which the keys to the map are written out to the data stream is undefined unless the map was created using the NewOrderedMap method.
-
Null.
-
Byte arrays, which will always be written as definite-length byte strings.
-
String objects. The strings will be encoded using definite-length encoding regardless of their length.
-
Any object accepted by the FromObject static methods.
Parameters:
- objValue: The arbitrary object to be serialized. Can be null. NOTE: For security reasons, whenever possible, an application should not base this parameter on user input or other externally supplied data unless the application limits this parameter’s inputs to types specially handled by this method (such as
int
orString
) and/or to plain-old-data types (POCO or POJO types) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
.
-
output: A writable data stream.
-
options: CBOR options for encoding the CBOR object to bytes.
Exceptions:
-
System.ArgumentException: The object’s type is not supported.
-
System.ArgumentNullException: The parameter options or output is null.
public static void Write( object objValue, System.IO.Stream stream);
Writes a CBOR object to a CBOR data stream. See the three-parameter Write method that takes a CBOREncodeOptions.
Parameters:
-
objValue: The arbitrary object to be serialized. Can be null.
-
stream: A writable data stream.
public static void Write( PeterO.Cbor.CBORObject value, System.IO.Stream stream);
Writes a CBOR object to a CBOR data stream.
Parameters:
-
value: The value to write. Can be null.
-
stream: A writable data stream.
Exceptions:
- System.ArgumentNullException: The parameter stream is null.
public static void Write( PeterO.Numbers.EDecimal bignum, System.IO.Stream stream);
Writes a decimal floating-point number in CBOR format to a data stream, as though it were converted to a CBOR object via CBORObject.FromObject(EDecimal) and then written out.
Parameters:
-
bignum: The arbitrary-precision decimal number to write. Can be null.
-
stream: Stream to write to.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
public static void Write( PeterO.Numbers.EFloat bignum, System.IO.Stream stream);
Writes a binary floating-point number in CBOR format to a data stream, as though it were converted to a CBOR object via CBORObject.FromObject(EFloat) and then written out.
Parameters:
-
bignum: An arbitrary-precision binary floating-point number. Can be null.
-
stream: A writable data stream.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
public static void Write( PeterO.Numbers.EInteger bigint, System.IO.Stream stream);
Writes a arbitrary-precision integer in CBOR format to a data stream.
Parameters:
-
bigint: Arbitrary-precision integer to write. Can be null.
-
stream: A writable data stream.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
public static void Write( PeterO.Numbers.ERational rational, System.IO.Stream stream);
Writes a rational number in CBOR format to a data stream, as though it were converted to a CBOR object via CBORObject.FromObject(ERational) and then written out.
Parameters:
-
rational: An arbitrary-precision rational number. Can be null.
-
stream: A writable data stream.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
public static void Write( sbyte value, System.IO.Stream stream);
This API is not CLS-compliant.
Writes an 8-bit signed integer in CBOR format to a data stream.
Parameters:
-
value: The parameter value is an 8-bit signed integer.
-
stream: A writable data stream.
public static void Write( short value, System.IO.Stream stream);
Writes a 16-bit signed integer in CBOR format to a data stream.
Parameters:
-
value: The value to write.
-
stream: A writable data stream.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
public static void Write( string str, System.IO.Stream stream);
Writes a text string in CBOR format to a data stream. The string will be encoded using definite-length encoding regardless of its length.
Parameters:
-
str: The string to write. Can be null.
-
stream: A writable data stream.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
public static void Write( string str, System.IO.Stream stream, PeterO.Cbor.CBOREncodeOptions options);
Writes a text string in CBOR format to a data stream, using the given options to control the encoding process.
Parameters:
-
str: The string to write. Can be null.
-
stream: A writable data stream.
-
options: Options for encoding the data to CBOR.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
public static void Write( uint value, System.IO.Stream stream);
This API is not CLS-compliant.
Writes a 32-bit unsigned integer in CBOR format to a data stream.
Parameters:
-
value: A 32-bit unsigned integer.
-
stream: A writable data stream.
public static void Write( ulong value, System.IO.Stream stream);
This API is not CLS-compliant.
Writes a 64-bit unsigned integer in CBOR format to a data stream.
Parameters:
-
value: A 64-bit unsigned integer.
-
stream: A writable data stream.
Exceptions:
- System.ArgumentNullException: The parameter stream is null.
public static void Write( ushort value, System.IO.Stream stream);
This API is not CLS-compliant.
Writes a 16-bit unsigned integer in CBOR format to a data stream.
Parameters:
-
value: A 16-bit unsigned integer.
-
stream: A writable data stream.
public static int WriteFloatingPointBits( System.IO.Stream outputStream, long floatingBits, int byteCount);
Writes the bits of a floating-point number in CBOR format to a data stream.
Parameters:
-
outputStream: A writable data stream.
-
floatingBits: The bits of a floating-point number number to write.
-
byteCount: The number of bytes of the stored floating-point number; this also specifies the format of the “floatingBits” parameter. This value can be 2 if “floatingBits”’s lowest (least significant) 16 bits identify the floating-point number in IEEE 754r binary16 format; or 4 if “floatingBits”’s lowest (least significant) 32 bits identify the floating-point number in IEEE 754r binary32 format; or 8 if “floatingBits” identifies the floating point number in IEEE 754r binary64 format. Any other values for this parameter are invalid. This method will write one plus this many bytes to the data stream.
Return Value:
The number of 8-bit bytes ordered to be written to the data stream.
Exceptions:
-
System.ArgumentException: The parameter byteCount is other than 2, 4, or 8.
-
System.ArgumentNullException: The parameter outputStream is null.
public static int WriteFloatingPointBits( System.IO.Stream outputStream, long floatingBits, int byteCount, bool shortestForm);
Writes the bits of a floating-point number in CBOR format to a data stream.
Parameters:
-
outputStream: A writable data stream.
-
floatingBits: The bits of a floating-point number number to write.
-
byteCount: The number of bytes of the stored floating-point number; this also specifies the format of the “floatingBits” parameter. This value can be 2 if “floatingBits”’s lowest (least significant) 16 bits identify the floating-point number in IEEE 754r binary16 format; or 4 if “floatingBits”’s lowest (least significant) 32 bits identify the floating-point number in IEEE 754r binary32 format; or 8 if “floatingBits” identifies the floating point number in IEEE 754r binary64 format. Any other values for this parameter are invalid.
-
shortestForm: If true, writes the shortest form of the floating-point number that preserves its value. If false, this method will write the number in the form given by ‘floatingBits’ by writing one plus the number of bytes given by ‘byteCount’ to the data stream.
Return Value:
The number of 8-bit bytes ordered to be written to the data stream.
Exceptions:
-
System.ArgumentException: The parameter byteCount is other than 2, 4, or 8.
-
System.ArgumentNullException: The parameter outputStream is null.
public static int WriteFloatingPointValue( System.IO.Stream outputStream, double doubleVal, int byteCount);
Writes a 64-bit binary floating-point number in CBOR format to a data stream, either in its 64-bit form, or its rounded 32-bit or 16-bit equivalent.
Parameters:
-
outputStream: A writable data stream.
-
doubleVal: The double-precision floating-point number to write.
-
byteCount: The number of 8-bit bytes of the stored number. This value can be 2 to store the number in IEEE 754r binary16, rounded to nearest, ties to even; or 4 to store the number in IEEE 754r binary32, rounded to nearest, ties to even; or 8 to store the number in IEEE 754r binary64. Any other values for this parameter are invalid.
Return Value:
The number of 8-bit bytes ordered to be written to the data stream.
Exceptions:
-
System.ArgumentException: The parameter byteCount is other than 2, 4, or 8.
-
System.ArgumentNullException: The parameter outputStream is null.
public static int WriteFloatingPointValue( System.IO.Stream outputStream, float singleVal, int byteCount);
Writes a 32-bit binary floating-point number in CBOR format to a data stream, either in its 64- or 32-bit form, or its rounded 16-bit equivalent.
Parameters:
-
outputStream: A writable data stream.
-
singleVal: The single-precision floating-point number to write.
-
byteCount: The number of 8-bit bytes of the stored number. This value can be 2 to store the number in IEEE 754r binary16, rounded to nearest, ties to even; or 4 to store the number in IEEE 754r binary32; or 8 to store the number in IEEE 754r binary64. Any other values for this parameter are invalid.
Return Value:
The number of 8-bit bytes ordered to be written to the data stream.
Exceptions:
-
System.ArgumentException: The parameter byteCount is other than 2, 4, or 8.
-
System.ArgumentNullException: The parameter outputStream is null.
public static void WriteJSON( object obj, System.IO.Stream outputStream);
Converts an arbitrary object to a text string in JavaScript Object Notation (JSON) format, as in the ToJSONString method, and writes that string to a data stream in UTF-8. If the object is convertible to a CBOR map, or to a CBOR object that contains CBOR maps, the order in which the keys to those maps are written out to the JSON string is undefined unless the map was created using the NewOrderedMap method. The example code given in PeterO.Cbor.CBORObject.ToJSONString(PeterO.Cbor.JSONOptions) can be used to write out certain keys of a CBOR map in a given order to a JSON string.
Parameters:
- obj: The parameter obj
is an arbitrary object. Can be null. NOTE: For security reasons, whenever possible, an application should not base this parameter on user input or other externally supplied data unless the application limits this parameter’s inputs to types specially handled by this method (such as
int
orString
) and/or to plain-old-data types (POCO or POJO types) within the control of the application. If the plain-old-data type references other data types, those types should likewise meet either criterion above.
.
- outputStream: A writable data stream.
Exceptions:
- System.ArgumentNullException: The parameter outputStream is null.
public void WriteJSONTo( System.IO.Stream outputStream);
Converts this object to a text string in JavaScript Object Notation (JSON) format, as in the ToJSONString method, and writes that string to a data stream in UTF-8. If the CBOR object contains CBOR maps, or is a CBOR map, the order in which the keys to the map are written out to the JSON string is undefined unless the map was created using the NewOrderedMap method. The example code given in PeterO.Cbor.CBORObject.ToJSONString(PeterO.Cbor.JSONOptions) can be used to write out certain keys of a CBOR map in a given order to a JSON string.
The following example (originally written in C# for the.NET version) writes out a CBOR object as part of a JSON text sequence (RFC 7464).
stream.WriteByte(0x1e); /* RS */ cborObject.WriteJSONTo(stream); /* JSON */ stream.WriteByte(0x0a); /* LF */
The following example (originally written in C# for the.NET version) shows how to use the LimitedMemoryStream
class (implemented in LimitedMemoryStream.cs in the peteroupc/CBOR open-source repository) to limit the size of supported JSON serializations of CBOR objects.
/* maximum supported JSON size in bytes*/ var maxSize = 20000; using (var ms = new LimitedMemoryStream(maxSize)) { cborObject.WriteJSONTo(ms); var bytes = ms.ToArray(); }
The following example (written in Java for the Java version) shows how to use a subclassed OutputStream
together with a ByteArrayOutputStream
to limit the size of supported JSON serializations of CBOR objects.
/* 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();
The following example (originally written in C# for the.NET version) shows how to use a.NET MemoryStream to limit the size of supported JSON serializations of CBOR objects. The disadvantage is that the extra memory needed to do so can be wasteful, especially if the average serialized object is much smaller than the maximum size given (for example, if the maximum size is 20000 bytes, but the average serialized object has a size of 50 bytes).
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); }
Parameters:
- outputStream: A writable data stream.
Exceptions:
-
System.IO.IOException: An I/O error occurred.
-
System.ArgumentNullException: The parameter outputStream is null.
public void WriteJSONTo( System.IO.Stream outputStream, PeterO.Cbor.JSONOptions options);
Converts this object to a text string in JavaScript Object Notation (JSON) format, as in the ToJSONString method, and writes that string to a data stream in UTF-8, using the given JSON options to control the encoding process. If the CBOR object contains CBOR maps, or is a CBOR map, the order in which the keys to the map are written out to the JSON string is undefined unless the map was created using the NewOrderedMap method. The example code given in PeterO.Cbor.CBORObject.ToJSONString(PeterO.Cbor.JSONOptions) can be used to write out certain keys of a CBOR map in a given order to a JSON string.
Parameters:
-
outputStream: A writable data stream.
-
options: An object containing the options to control writing the CBOR object to JSON.
Exceptions:
-
System.IO.IOException: An I/O error occurred.
-
System.ArgumentNullException: The parameter outputStream is null.
public void WriteTo( System.IO.Stream stream);
Writes this CBOR object to a data stream. If the CBOR object contains CBOR maps, or is a CBOR map, the order in which the keys to the map are written out to the data stream is undefined unless the map was created using the NewOrderedMap method. See the examples (originally written in C# for the.NET version) for ways to write out certain keys of a CBOR map in a given order. In the case of CBOR objects of type FloatingPoint, the number is written using the shortest floating-point encoding possible; this is a change from previous versions.
The following example shows a method that writes each key of ‘mapObj’ to ‘outputStream’, in the order given in ‘keys’, where ‘mapObj’ is written out in the form of a CBOR definite-length map . Only keys found in ‘keys’ will be written if they exist in ‘mapObj’.
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); } } }
The following example shows a method that writes each key of ‘mapObj’ to ‘outputStream’, in the order given in ‘keys’, where ‘mapObj’ is written out in the form of a CBOR indefinite-length map . Only keys found in ‘keys’ will be written if they exist in ‘mapObj’.
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); }
The following example shows a method that writes out a list of objects to ‘outputStream’ as an indefinite-length CBOR array .
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); }
The following example (originally written in C# for the.NET version) shows how to use the LimitedMemoryStream
class (implemented in LimitedMemoryStream.cs in the peteroupc/CBOR open-source repository) to limit the size of supported CBOR serializations.
/* maximum supported CBOR size in bytes*/ var maxSize = 20000; using (var ms = new LimitedMemoryStream(maxSize)) { cborObject.WriteTo(ms); var bytes = ms.ToArray(); }
The following example (written in Java for the Java version) shows how to use a subclassed OutputStream
together with a ByteArrayOutputStream
to limit the size of supported CBOR serializations.
/* 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();
The following example (originally written in C# for the.NET version) shows how to use a.NET MemoryStream to limit the size of supported CBOR serializations. The disadvantage is that the extra memory needed to do so can be wasteful, especially if the average serialized object is much smaller than the maximum size given (for example, if the maximum size is 20000 bytes, but the average serialized object has a size of 50 bytes).
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); }
Parameters:
- stream: A writable data stream.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
public void WriteTo( System.IO.Stream stream, PeterO.Cbor.CBOREncodeOptions options);
Writes this CBOR object to a data stream, using the specified options for encoding the data to CBOR format. If the CBOR object contains CBOR maps, or is a CBOR map, the order in which the keys to the map are written out to the data stream is undefined unless the map was created using the NewOrderedMap method. The example code given in M:PeterO.Cbor.CBORObject.WriteTo(System.IO.Stream) can be used to write out certain keys of a CBOR map in a given order. In the case of CBOR objects of type FloatingPoint, the number is written using the shortest floating-point encoding possible; this is a change from previous versions.
Parameters:
-
stream: A writable data stream.
-
options: Options for encoding the data to CBOR.
Exceptions:
-
System.ArgumentNullException: The parameter stream is null.
-
System.IO.IOException: An I/O error occurred.
-
System.ArgumentException: Unexpected data type”.
public static int WriteValue( System.IO.Stream outputStream, int majorType, int value);
Writes a CBOR major type number and an integer 0 or greater associated with it to a data stream, where that integer is passed to this method as a 32-bit signed integer. This is a low-level method that is useful for implementing custom CBOR encoding methodologies. This method encodes the given major type and value in the shortest form allowed for the major type.
There are other useful things to note when encoding CBOR that are not covered by this WriteValue method. To mark the start of an indefinite-length array, write the 8-bit byte 0x9f to the output stream. To mark the start of an indefinite-length map, write the 8-bit byte 0xbf to the output stream. To mark the end of an indefinite-length array or map, write the 8-bit byte 0xff to the output stream.
In the following example, an array of three objects is written as CBOR to a data stream.
/* 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*/
In the following example, a map consisting of two key-value pairs is written as CBOR to a data stream.
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*/
In the following example (originally written in C# for the.NET Framework version), a text string is written as CBOR to a data stream.
string str = "hello world"; byte[] bytes = DataUtilities.GetUtf8Bytes(str, true); CBORObject.WriteValue(stream, 4, bytes.Length); stream.Write(bytes, 0, bytes.Length);
.
Parameters:
-
outputStream: A writable data stream.
-
majorType: The CBOR major type to write. This is a number from 0 through 7 as follows. 0: integer 0 or greater; 1: negative integer; 2: byte string; 3: UTF-8 text string; 4: array; 5: map; 6: tag; 7: simple value. See RFC 8949 for details on these major types.
-
value: An integer 0 or greater associated with the major type, as follows. 0: integer 0 or greater; 1: the negative integer’s absolute value is 1 plus this number; 2: length in bytes of the byte string; 3: length in bytes of the UTF-8 text string; 4: number of items in the array; 5: number of key-value pairs in the map; 6: tag number; 7: simple value number, which must be in the interval [0, 23] or [32, 255].
Return Value:
The number of bytes ordered to be written to the data stream.
Exceptions:
-
System.ArgumentException: Value is from 24 to 31 and major type is 7.
-
System.ArgumentNullException: The parameter outputStream is null.
public static int WriteValue( System.IO.Stream outputStream, int majorType, long value);
Writes a CBOR major type number and an integer 0 or greater associated with it to a data stream, where that integer is passed to this method as a 64-bit signed integer. This is a low-level method that is useful for implementing custom CBOR encoding methodologies. This method encodes the given major type and value in the shortest form allowed for the major type.
There are other useful things to note when encoding CBOR that are not covered by this WriteValue method. To mark the start of an indefinite-length array, write the 8-bit byte 0x9f to the output stream. To mark the start of an indefinite-length map, write the 8-bit byte 0xbf to the output stream. To mark the end of an indefinite-length array or map, write the 8-bit byte 0xff to the output stream. For examples, see the WriteValue(Stream, int, int) overload.
Parameters:
-
outputStream: A writable data stream.
-
majorType: The CBOR major type to write. This is a number from 0 through 7 as follows. 0: integer 0 or greater; 1: negative integer; 2: byte string; 3: UTF-8 text string; 4: array; 5: map; 6: tag; 7: simple value. See RFC 8949 for details on these major types.
-
value: An integer 0 or greater associated with the major type, as follows. 0: integer 0 or greater; 1: the negative integer’s absolute value is 1 plus this number; 2: length in bytes of the byte string; 3: length in bytes of the UTF-8 text string; 4: number of items in the array; 5: number of key-value pairs in the map; 6: tag number; 7: simple value number, which must be in the interval [0, 23] or [32, 255].
Return Value:
The number of bytes ordered to be written to the data stream.
Exceptions:
-
System.ArgumentException: Value is from 24 to 31 and major type is 7.
-
System.ArgumentNullException: The parameter outputStream is null.
public static int WriteValue( System.IO.Stream outputStream, int majorType, PeterO.Numbers.EInteger bigintValue);
Writes a CBOR major type number and an integer 0 or greater associated with it to a data stream, where that integer is passed to this method as an arbitrary-precision integer. This is a low-level method that is useful for implementing custom CBOR encoding methodologies. This method encodes the given major type and value in the shortest form allowed for the major type.
There are other useful things to note when encoding CBOR that are not covered by this WriteValue method. To mark the start of an indefinite-length array, write the 8-bit byte 0x9f to the output stream. To mark the start of an indefinite-length map, write the 8-bit byte 0xbf to the output stream. To mark the end of an indefinite-length array or map, write the 8-bit byte 0xff to the output stream.
Parameters:
-
outputStream: A writable data stream.
-
majorType: The CBOR major type to write. This is a number from 0 through 7 as follows. 0: integer 0 or greater; 1: negative integer; 2: byte string; 3: UTF-8 text string; 4: array; 5: map; 6: tag; 7: simple value. See RFC 8949 for details on these major types.
-
bigintValue: An integer 0 or greater associated with the major type, as follows. 0: integer 0 or greater; 1: the negative integer’s absolute value is 1 plus this number; 2: length in bytes of the byte string; 3: length in bytes of the UTF-8 text string; 4: number of items in the array; 5: number of key-value pairs in the map; 6: tag number; 7: simple value number, which must be in the interval [0, 23] or [32, 255]. For major types 0 to 6, this number may not be greater than 2^64 - 1.
Return Value:
The number of bytes ordered to be written to the data stream.
Exceptions:
-
System.ArgumentException: The parameter majorType is 7 and value is greater than 255.
-
System.ArgumentNullException: The parameter outputStream or bigintValue is null.
public static int WriteValue( System.IO.Stream outputStream, int majorType, uint value);
This API is not CLS-compliant.
Writes a CBOR major type number and an integer 0 or greater associated with it to a data stream, where that integer is passed to this method as a 32-bit unsigned integer. This is a low-level method that is useful for implementing custom CBOR encoding methodologies. This method encodes the given major type and value in the shortest form allowed for the major type.
Parameters:
-
outputStream: A writable data stream.
-
majorType: The CBOR major type to write. This is a number from 0 through 7 as follows. 0: integer 0 or greater; 1: negative integer; 2: byte string; 3: UTF-8 text string; 4: array; 5: map; 6: tag; 7: simple value. See RFC 7049 for details on these major types.
-
value: An integer 0 or greater associated with the major type, as follows. 0: integer 0 or greater; 1: the negative integer’s absolute value is 1 plus this number; 2: length in bytes of the byte string; 3: length in bytes of the UTF-8 text string; 4: number of items in the array; 5: number of key-value pairs in the map; 6: tag number; 7: simple value number, which must be in the interval [0, 23] or [32, 255].
Return Value:
The number of bytes ordered to be written to the data stream.
Exceptions:
- System.ArgumentNullException: The parameter outputStream is null.
public static int WriteValue( System.IO.Stream outputStream, int majorType, ulong value);
This API is not CLS-compliant.
Writes a CBOR major type number and an integer 0 or greater associated with it to a data stream, where that integer is passed to this method as a 64-bit unsigned integer. This is a low-level method that is useful for implementing custom CBOR encoding methodologies. This method encodes the given major type and value in the shortest form allowed for the major type.
Parameters:
-
outputStream: A writable data stream.
-
majorType: The CBOR major type to write. This is a number from 0 through 7 as follows. 0: integer 0 or greater; 1: negative integer; 2: byte string; 3: UTF-8 text string; 4: array; 5: map; 6: tag; 7: simple value. See RFC 7049 for details on these major types.
-
value: An integer 0 or greater associated with the major type, as follows. 0: integer 0 or greater; 1: the negative integer’s absolute value is 1 plus this number; 2: length in bytes of the byte string; 3: length in bytes of the UTF-8 text string; 4: number of items in the array; 5: number of key-value pairs in the map; 6: tag number; 7: simple value number, which must be in the interval [0, 23] or [32, 255].
Return Value:
The number of bytes ordered to be written to the data stream.
Exceptions:
-
System.ArgumentException: The parameter majorType is 7 and value is greater than 255.
-
System.ArgumentNullException: The parameter outputStream is null.