Skip to content

Commit

Permalink
finish annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Roddie committed Oct 10, 2023
1 parent ab98e97 commit 87cbc68
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 116 deletions.
31 changes: 1 addition & 30 deletions CBOR.sln
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CBOR", "CBOR\CBOR.csproj",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CBORTest", "CBORTest\CBORTest.csproj", "{8FE63143-541E-448D-8337-A98D1FA51541}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrimTester", "TrimTester\TrimTester.csproj", "{DDE5B555-6823-4A30-9DF6-D0777C96F659}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -28,39 +26,12 @@ Global
{8FE63143-541E-448D-8337-A98D1FA51541}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FE63143-541E-448D-8337-A98D1FA51541}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FE63143-541E-448D-8337-A98D1FA51541}.Release|Any CPU.Build.0 = Release|Any CPU
{DDE5B555-6823-4A30-9DF6-D0777C96F659}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DDE5B555-6823-4A30-9DF6-D0777C96F659}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DDE5B555-6823-4A30-9DF6-D0777C96F659}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DDE5B555-6823-4A30-9DF6-D0777C96F659}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FDBC18D9-2C43-41BF-A01B-7F87F9C1AEA2}
SolutionGuid = {AE3EA107-473B-4E31-B14D-F901FE8F17B1}
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B38659A7-A3DB-4CD1-8DFF-641B579E5092}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B38659A7-A3DB-4CD1-8DFF-641B579E5092}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B38659A7-A3DB-4CD1-8DFF-641B579E5092}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B38659A7-A3DB-4CD1-8DFF-641B579E5092}.Release|Any CPU.Build.0 = Release|Any CPU
{44A061F7-B6A8-4FBD-993E-3746E8C42D6B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{44A061F7-B6A8-4FBD-993E-3746E8C42D6B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{44A061F7-B6A8-4FBD-993E-3746E8C42D6B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{44A061F7-B6A8-4FBD-993E-3746E8C42D6B}.Release|Any CPU.Build.0 = Release|Any CPU
{8FE63143-541E-448D-8337-A98D1FA51541}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8FE63143-541E-448D-8337-A98D1FA51541}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8FE63143-541E-448D-8337-A98D1FA51541}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8FE63143-541E-448D-8337-A98D1FA51541}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FDBC18D9-2C43-41BF-A01B-7F87F9C1AEA2}
EndGlobalSection
EndGlobal
2 changes: 1 addition & 1 deletion CBOR/CBOR.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard1.0</TargetFrameworks>
<TargetFrameworks>netstandard1.0; net7.0</TargetFrameworks>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>5.0-alpha1</Version>
<Owners>Peter Occil</Owners>
Expand Down
17 changes: 4 additions & 13 deletions CBOR/PeterO/Cbor/CBORJson2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,7 @@ private CBORObject NextJSONNegativeNumber(
this.options);
#if DEBUG
if (this.options.NumberConversion == JSONOptions.ConversionMode.Full &&
(
(EDecimal)obj.ToObject(
typeof(EDecimal))).CompareToValue(EDecimal.FromString(this.bytes,
obj.ToEDecimal().CompareToValue(EDecimal.FromString(this.bytes,
numberStartIndex,
numberEndIndex - numberStartIndex)) != 0) {
this.RaiseError(String.Empty + obj);
Expand Down Expand Up @@ -394,9 +392,7 @@ private CBORObject NextJSONNonnegativeNumber(int c, int[] nextChar) {
// All-digit number that's short enough
obj = CBORDataUtilities.ParseSmallNumber(cval, this.options);
#if DEBUG
if ((
(EDecimal)obj.ToObject(
typeof(EDecimal))).CompareToValue(EDecimal.FromInt32(cval)) !=
if (obj.ToEDecimal().CompareToValue(EDecimal.FromInt32(cval)) !=
0) {
this.RaiseError(String.Empty + obj);
}
Expand All @@ -409,10 +405,7 @@ private CBORObject NextJSONNonnegativeNumber(int c, int[] nextChar) {
// is two digits without sign, decimal point, or exponent
obj = CBORDataUtilities.ParseSmallNumber(cval, this.options);
#if DEBUG
if ((
(EDecimal)obj.ToObject(
typeof(EDecimal))).CompareToValue(EDecimal.FromInt32(cval)) !=
0) {
if (obj.ToEDecimal().CompareToValue(EDecimal.FromInt32(cval)) != 0) {
this.RaiseError(String.Empty + obj);
}
#endif
Expand All @@ -438,9 +431,7 @@ private CBORObject NextJSONNonnegativeNumber(int c, int[] nextChar) {
this.options);
#if DEBUG
if (this.options.NumberConversion == JSONOptions.ConversionMode.Full &&
(
(EDecimal)obj.ToObject(
typeof(EDecimal))).CompareToValue(EDecimal.FromString(this.bytes,
obj.ToEDecimal().CompareToValue(EDecimal.FromString(this.bytes,
numberStartIndex,
numberEndIndex - numberStartIndex)) != 0) {
this.RaiseError(String.Empty + obj);
Expand Down
20 changes: 18 additions & 2 deletions CBOR/PeterO/Cbor/CBORNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public enum NumberKind {
new CBORExtendedRational(),
};
private readonly object value;
internal CBORNumber(NumberKind kind, object value) {
private CBORNumber(NumberKind kind, object value) {
this.Kind = kind;
this.value = value;
}
Expand Down Expand Up @@ -84,7 +84,23 @@ internal static ICBORNumber GetNumberInterface(NumberKind kind) {
/// <summary>Converts this object's value to a CBOR object.</summary>
/// <returns>A CBOR object that stores this object's value.</returns>
public CBORObject ToCBORObject() { // TODO: use a safe method to get this
return CBORObject.FromObject(this.value);
object obj = this.value;
if (obj is long l) {
return CBORObject.FromInt64(l);
}
if (obj is EInteger eif) {
return CBORObject.FromEInteger(eif);
}
if (obj is EDecimal edf) {
return CBORObject.FromObject(edf);
}
if (obj is EFloat eff) {
return CBORObject.FromObject(eff);
}
if (obj is ERational erf) {
return CBORObject.FromObject(erf);
}
throw new Exception("Unexpected type: " + obj.GetType());
}

/// <summary>Gets this value's sign: -1 if nonzero and negative; 1 if
Expand Down
56 changes: 30 additions & 26 deletions CBOR/PeterO/Cbor/CBORObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ public CBORObject this[int index]
/// <c>CBORObject.Null</c> ) if an item with the given key doesn't
/// exist.</returns>
[RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")]
public CBORObject GetOrDefault(object key, CBORObject defaultValue) {
public CBORObject GetOrDefault(object key, CBORObject defaultValue) { // Suspect method. This is always used with a string key in the main library. But if the key is not a number it will return defaultValue.
if (this.Type == CBORType.Array) {
int index;
if (key is int) {
Expand Down Expand Up @@ -698,7 +698,7 @@ public CBORObject this[string key]
if (key == null) {
throw new ArgumentNullException(nameof(key));
}
var objkey = CBORObject.FromObject(key);
var objkey = FromString(key);
return this[objkey];
}

Expand All @@ -709,7 +709,7 @@ public CBORObject this[string key]
if (value == null) {
throw new ArgumentNullException(nameof(value));
}
var objkey = CBORObject.FromObject(key);
var objkey = FromString(key);
if (this.Type == CBORType.Map) {
IDictionary<CBORObject, CBORObject> map = this.AsMap();
map[objkey] = value;
Expand Down Expand Up @@ -2003,9 +2003,7 @@ public static CBORObject FromEInteger(EInteger bigintValue) {
return new CBORObject(CBORObjectTypeEInteger, bigintValue);
} else {
int tag = (bigintValue.Sign < 0) ? 3 : 2;
return CBORObject.FromObjectAndTag(
EIntegerBytes(bigintValue),
tag);
return FromByteArray(EIntegerBytes(bigintValue)).WithTag(tag);
}
}
}
Expand Down Expand Up @@ -2304,14 +2302,15 @@ public static CBORObject FromObject(double value) {
/// <returns>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.</returns>
public static CBORObject FromObject(byte[] bytes) {
public static CBORObject FromByteArray(byte[] bytes) {
if (bytes == null) {
return CBORObject.Null;
}
var newvalue = new byte[bytes.Length];
Array.Copy(bytes, 0, newvalue, 0, bytes.Length);
return new CBORObject(CBORObjectTypeByteString, bytes);
}
// TODO: readd FromObject(byte[] bytes) as deprecated

/// <summary>Generates a CBOR object from an array of CBOR
/// objects.</summary>
Expand Down Expand Up @@ -2510,7 +2509,7 @@ public static CBORObject FromObject(
/// string. To create a CBOR byte string object from <c>String</c>
/// ,
/// see the example given in <see
/// cref='PeterO.Cbor.CBORObject.FromObject(byte[])'/>.</item>
/// cref='PeterO.Cbor.CBORObject.FromByteArray(byte[])'/>.</item>
/// <item>In
/// the.NET version, a nullable is converted to <c>CBORObject.Null</c>
/// if the nullable's value is <c>null</c>
Expand Down Expand Up @@ -2880,8 +2879,8 @@ public CBORObject WithTag(EInteger bigintTag) {
/// <summary>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).</summary>
/// <param name='valueOb'>The parameter <paramref name='valueOb'/> is
/// an arbitrary object, which can be null.
/// <param name='cborObj'>The parameter <paramref name='cborObj'/> is
/// a CBORObject.
/// <para><b>NOTE:</b> For security reasons, whenever possible, an
/// application should not base this parameter on user input or other
/// externally supplied data, and whenever possible, the application
Expand All @@ -2897,18 +2896,16 @@ public CBORObject WithTag(EInteger bigintTag) {
/// list can be found at the CBOR Tags registry maintained by the
/// Internet Assigned Numbers Authority(
/// <i>iana.org/assignments/cbor-tags</i> ).</param>
/// <returns>A CBOR object where the object <paramref name='valueOb'/>
/// is converted to a CBOR object and given the tag <paramref
/// name='bigintTag'/>. If <paramref name='valueOb'/> is null, returns
/// a version of CBORObject.Null with the given tag.</returns>
/// <returns>A CBOR object where the object <paramref name='cborObj'/>
/// is given the tag <paramref
/// name='bigintTag'/>.</returns>
/// <exception cref='ArgumentException'>The parameter <paramref
/// name='bigintTag'/> is less than 0 or greater than
/// 2^64-1.</exception>
/// <exception cref='ArgumentNullException'>The parameter <paramref
/// name='bigintTag'/> is null.</exception>
[RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")]
public static CBORObject FromObjectAndTag(
object valueOb,
public static CBORObject FromCBORObjectAndTag(
CBORObject cborObj,
EInteger bigintTag) {
return bigintTag == null ?
throw new ArgumentNullException(nameof(bigintTag)) :
Expand All @@ -2917,9 +2914,11 @@ public static CBORObject FromObjectAndTag(
") is less than 0") : bigintTag.CompareTo(UInt64MaxValue) > 0 ?
throw new ArgumentException(
"tag more than 18446744073709551615 (" + bigintTag + ")") :
FromObject(valueOb).WithTag(bigintTag);
cborObj.WithTag(bigintTag);
}

// TODO: reAdd FromObjectAndTag as deprecated

/// <summary>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).</summary>
Expand All @@ -2943,8 +2942,8 @@ public CBORObject WithTag(int smallTag) {
/// <summary>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).</summary>
/// <param name='valueObValue'>The parameter <paramref
/// name='valueObValue'/> is an arbitrary object, which can be null.
/// <param name='cborObj'>The parameter <paramref
/// name='cborObj'/> is a CBORObject.
/// <para><b>NOTE:</b> For security reasons, whenever possible, an
/// application should not base this parameter on user input or other
/// externally supplied data, and whenever possible, the application
Expand All @@ -2962,18 +2961,18 @@ public CBORObject WithTag(int smallTag) {
/// (
/// <i>iana.org/assignments/cbor-tags</i> ).</param>
/// <returns>A CBOR object where the object <paramref
/// name='valueObValue'/> is converted to a CBOR object and given the
/// name='cborObj'/> is given the
/// tag <paramref name='smallTag'/>. If "valueOb" is null, returns a
/// version of CBORObject.Null with the given tag.</returns>
/// <exception cref='ArgumentException'>The parameter <paramref
/// name='smallTag'/> is less than 0.</exception>
[RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")]
public static CBORObject FromObjectAndTag(
object valueObValue,
public static CBORObject FromCBORObjectAndTag(
CBORObject cborObj,
int smallTag) {
return smallTag < 0 ? throw new ArgumentException("smallTag(" + smallTag +
") is less than 0") : FromObject(valueObValue).WithTag(smallTag);
") is less than 0") : cborObj.WithTag(smallTag);
}
// TODO: reAdd FromObjectAndTag as deprecated

/// <summary>Creates a CBOR object from a simple value
/// number.</summary>
Expand Down Expand Up @@ -4182,7 +4181,6 @@ public CBORObject Add(object key, object valueOb) {
/// string")) .Add(CBORObject.FromObjectAndTag(9999, 1));</code>
/// .
/// </example>
[RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")]
public CBORObject Add(CBORObject obj) {
if (this.Type == CBORType.Array) {
IList<CBORObject> list = this.AsList();
Expand Down Expand Up @@ -4958,6 +4956,7 @@ public byte[] EncodeToBytes(CBOREncodeOptions options) {
/// 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.</exception>
[RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted.
public CBORObject AtJSONPointer(string pointer) {
CBORObject ret = this.AtJSONPointer(pointer, null);
return ret ?? throw new CBORException("Invalid JSON pointer");
Expand Down Expand Up @@ -4990,6 +4989,7 @@ public CBORObject AtJSONPointer(string pointer) {
/// 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.</returns>
[RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted.
public CBORObject AtJSONPointer(string pointer, CBORObject defaultValue) {
return JSONPointer.GetObject(this, pointer, null);
}
Expand Down Expand Up @@ -5021,6 +5021,7 @@ public CBORObject AtJSONPointer(string pointer, CBORObject defaultValue) {
/// <item>"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.</item></list></remarks>
[RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted.
public CBORObject ApplyJSONPatch(CBORObject patch) {
return JSONPatch.Patch(this, patch);
}
Expand Down Expand Up @@ -5376,6 +5377,7 @@ public bool HasTag(EInteger bigTagValue) {
/// <exception cref='ArgumentException'>The parameter <paramref
/// name='valueOb'/> has an unsupported type; or <paramref
/// name='index'/> is not a valid index into this array.</exception>
[RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")]
public CBORObject Insert(int index, object valueOb) {
if (this.Type == CBORType.Array) {
CBORObject mapValue;
Expand Down Expand Up @@ -5426,6 +5428,7 @@ public void Clear() {
/// name='obj'/> is null (as opposed to CBORObject.Null).</exception>
/// <exception cref='InvalidOperationException'>The object is not an
/// array or map.</exception>
[RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")]
public bool Remove(object obj) {
return this.Remove(CBORObject.FromObject(obj));
}
Expand Down Expand Up @@ -5497,6 +5500,7 @@ public bool Remove(CBORObject obj) {
/// or this instance is a CBOR array and <paramref name='key'/> is less
/// than 0, is the size of this array or greater, or is not a 32-bit
/// signed integer ( <c>int</c> ).</exception>
[RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")]
public CBORObject Set(object key, object valueOb) {
if (this.Type == CBORType.Map) {
CBORObject mapKey;
Expand Down
19 changes: 5 additions & 14 deletions CBOR/PeterO/Cbor/CBORObjectExtra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,36 +306,27 @@ public static CBORObject FromUShort(ushort value) {
/// (the new tag is made the outermost tag).</returns>
[CLSCompliant(false)]
public CBORObject WithTag(ulong tag) {
return FromObjectAndTag(this, UInt64ToEInteger(tag));
return FromCBORObjectAndTag(this, UInt64ToEInteger(tag));
}

/// <summary>Generates a CBOR object from an arbitrary object and gives
/// the resulting object a tag.</summary>
/// <param name='o'>The parameter <paramref name='o'/> is an arbitrary
/// object, which can be null.
/// <para><b>NOTE:</b> For security reasons, whenever possible, an
/// application should not base this parameter on user input or other
/// externally supplied data, and whenever possible, the application
/// should limit this parameter's inputs to types specially handled by
/// this method (such as <c>int</c> or <c>String</c> ) 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.</para>.</param>
/// CBORObject.</param>
/// <param name='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(
/// <i>iana.org/assignments/cbor-tags</i> ).</param>
/// <returns>A CBOR object where the object <paramref name='o'/> is
/// converted to a CBOR object and given the tag <paramref name='tag'/>
/// given the tag <paramref name='tag'/>
/// . If "valueOb" is null, returns a version of CBORObject.Null with
/// the given tag.</returns>
[CLSCompliant(false)]
[RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")]
public static CBORObject FromObjectAndTag(object o, ulong tag) {
return FromObjectAndTag(o, UInt64ToEInteger(tag));
public static CBORObject FromCBORObjectAndTag(CBORObject o, ulong tag) {
return FromCBORObjectAndTag(o, UInt64ToEInteger(tag));
}

/// <summary>
Expand Down
Loading

0 comments on commit 87cbc68

Please sign in to comment.