From 1b6a9f2d5148996acfeaf2fcd212e5dc76fb79b7 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Sat, 7 Oct 2023 12:44:05 +0100 Subject: [PATCH 01/29] add a trimming project and set IsTrimmable --- CBOR.sln | 6 ++++++ CBOR/CBOR.csproj | 12 ++++++++---- CBORDocs2/CBORDocs2.csproj | 2 +- CBORDocs2/DocVisitor.cs | 2 +- CBORDocs2/packages.config | 2 +- CBORTest/CBORTest.csproj | 2 +- TrimTester/Program.cs | 4 ++++ TrimTester/TrimTester.csproj | 16 ++++++++++++++++ 8 files changed, 38 insertions(+), 8 deletions(-) create mode 100644 TrimTester/Program.cs create mode 100644 TrimTester/TrimTester.csproj diff --git a/CBOR.sln b/CBOR.sln index b2942131..b23d92b8 100644 --- a/CBOR.sln +++ b/CBOR.sln @@ -8,6 +8,8 @@ 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 @@ -26,6 +28,10 @@ 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 diff --git a/CBOR/CBOR.csproj b/CBOR/CBOR.csproj index 6a9841b0..4bfeccdd 100644 --- a/CBOR/CBOR.csproj +++ b/CBOR/CBOR.csproj @@ -1,4 +1,4 @@ - + netstandard1.0 @@ -29,7 +29,10 @@ Note that after version 4.5x, the CBOR library's repository will stop including PeterO.snk CBOR (Concise Binary Object Representation) true - rules.rulesetCC0-1.0 + rules.rulesetCC0-1.0 + + true + full @@ -40,7 +43,8 @@ Note that after version 4.5x, the CBOR library's repository will stop including none bin\Release\netstandard1.0\CBOR.xml - rules.ruleset + rules.ruleset + @@ -50,5 +54,5 @@ Note that after version 4.5x, the CBOR library's repository will stop including - + diff --git a/CBORDocs2/CBORDocs2.csproj b/CBORDocs2/CBORDocs2.csproj index 52b77fa9..22b15e6a 100644 --- a/CBORDocs2/CBORDocs2.csproj +++ b/CBORDocs2/CBORDocs2.csproj @@ -14,7 +14,7 @@ - + diff --git a/CBORDocs2/DocVisitor.cs b/CBORDocs2/DocVisitor.cs index b9164edd..7fc8e95c 100644 --- a/CBORDocs2/DocVisitor.cs +++ b/CBORDocs2/DocVisitor.cs @@ -465,7 +465,7 @@ public static string FormatTypeSig(Type typeInfo) { var builder = new StringBuilder(); _ = builder.Append(FourSpaces); - _ = typeInfo.IsNested ? typeInfo.IsNestedPublic : typeInfo.IsPublic ? builder.Append("public ") : builder.Append("internal "); + _ = (typeInfo.IsNested ? typeInfo.IsNestedPublic : typeInfo.IsPublic) ? builder.Append("public ") : builder.Append("internal "); if (typeInfo.IsAbstract && typeInfo.IsSealed) { _ = builder.Append("static "); diff --git a/CBORDocs2/packages.config b/CBORDocs2/packages.config index f4f55a89..0b8ddfd5 100644 --- a/CBORDocs2/packages.config +++ b/CBORDocs2/packages.config @@ -1,6 +1,6 @@ - + diff --git a/CBORTest/CBORTest.csproj b/CBORTest/CBORTest.csproj index 483257a9..254efa2d 100644 --- a/CBORTest/CBORTest.csproj +++ b/CBORTest/CBORTest.csproj @@ -18,7 +18,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive all diff --git a/TrimTester/Program.cs b/TrimTester/Program.cs new file mode 100644 index 00000000..a491f431 --- /dev/null +++ b/TrimTester/Program.cs @@ -0,0 +1,4 @@ +using System; + +// See https://aka.ms/new-console-template for more information +Console.WriteLine("Hello, World!"); \ No newline at end of file diff --git a/TrimTester/TrimTester.csproj b/TrimTester/TrimTester.csproj new file mode 100644 index 00000000..e98516d2 --- /dev/null +++ b/TrimTester/TrimTester.csproj @@ -0,0 +1,16 @@ + + + Exe + net7.0 + true + + link + + + + + + + + + From afe761c646b3682a2df0d07b09c7bfe51f316d24 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Sat, 7 Oct 2023 17:33:51 +0100 Subject: [PATCH 02/29] non-working DynamicallyAccessedMembers via Polysharp --- CBOR/CBOR.csproj | 24 +++++++++++++++--------- CBOR/PeterO/Cbor/PropertyMap.cs | 3 +++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/CBOR/CBOR.csproj b/CBOR/CBOR.csproj index 4bfeccdd..0cee8a5f 100644 --- a/CBOR/CBOR.csproj +++ b/CBOR/CBOR.csproj @@ -1,4 +1,4 @@ - + netstandard1.0 @@ -32,14 +32,15 @@ Note that after version 4.5x, the CBOR library's repository will stop including rules.rulesetCC0-1.0 true + 11.0 - + full bin\Debug\netstandard1.0\CBOR.xml rules.ruleset - + none bin\Release\netstandard1.0\CBOR.xml @@ -47,12 +48,17 @@ Note that after version 4.5x, the CBOR library's repository will stop including - - - + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + - + + + - - + + + + diff --git a/CBOR/PeterO/Cbor/PropertyMap.cs b/CBOR/PeterO/Cbor/PropertyMap.cs index be579d12..167596a8 100644 --- a/CBOR/PeterO/Cbor/PropertyMap.cs +++ b/CBOR/PeterO/Cbor/PropertyMap.cs @@ -13,6 +13,8 @@ licensed under Creative Commons Zero (CC0): using System.Globalization; using System.IO; using System.Reflection; +using System.Diagnostics.CodeAnalysis; +using System.Runtime.CompilerServices; namespace PeterO.Cbor { @@ -543,6 +545,7 @@ private static IEnumerable GetTypeProperties(Type t) return t.GetRuntimeProperties(); } + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] private static IEnumerable GetTypeFields(Type t) { return t.GetRuntimeFields(); From 2b4d8dea37825df7806d1b21f85fd9b7330e4c61 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Sat, 7 Oct 2023 19:53:13 +0100 Subject: [PATCH 03/29] follow advice in https://github.com/Sergio0694/PolySharp/issues/84 --- CBOR/CBOR.csproj | 1 + CBOR/PeterO/Cbor/PropertyMap.cs | 24 +++++++++++------------- CBOR/PeterO/DebugUtility.cs | 12 ++++++------ 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/CBOR/CBOR.csproj b/CBOR/CBOR.csproj index 0cee8a5f..c71cc64e 100644 --- a/CBOR/CBOR.csproj +++ b/CBOR/CBOR.csproj @@ -33,6 +33,7 @@ Note that after version 4.5x, the CBOR library's repository will stop including true 11.0 + true diff --git a/CBOR/PeterO/Cbor/PropertyMap.cs b/CBOR/PeterO/Cbor/PropertyMap.cs index 167596a8..23c5ff03 100644 --- a/CBOR/PeterO/Cbor/PropertyMap.cs +++ b/CBOR/PeterO/Cbor/PropertyMap.cs @@ -14,7 +14,6 @@ licensed under Creative Commons Zero (CC0): using System.IO; using System.Reflection; using System.Diagnostics.CodeAnalysis; -using System.Runtime.CompilerServices; namespace PeterO.Cbor { @@ -540,28 +539,27 @@ private static bool IsAssignableFrom(Type superType, Type subType) return superType.GetTypeInfo().IsAssignableFrom(subType.GetTypeInfo()); } - private static IEnumerable GetTypeProperties(Type t) + private static IEnumerable GetTypeProperties([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t) { return t.GetRuntimeProperties(); } - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] - private static IEnumerable GetTypeFields(Type t) + private static IEnumerable GetTypeFields([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t) { return t.GetRuntimeFields(); } - private static IEnumerable GetTypeInterfaces(Type t) + private static IEnumerable GetTypeInterfaces([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t) { return t.GetTypeInfo().ImplementedInterfaces; } private static MethodInfo GetTypeMethod( - Type t, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t, string name, - Type[] parameters) + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type parameter) { - return t.GetRuntimeMethod(name, parameters); + return t.GetRuntimeMethod(name, new[] { parameter }); } private static bool HasCustomAttribute( @@ -1037,9 +1035,9 @@ public static IDictionary NewOrderedDict() public static object FindOneArgumentMethod( object obj, string name, - Type argtype) + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type argtype) { - return GetTypeMethod(obj.GetType(), name, new[] { argtype }); + return GetTypeMethod(obj.GetType(), name, argtype ); } public static object InvokeOneArgumentMethod( @@ -1105,7 +1103,7 @@ private static object TypeToIntegerObject(CBORObject objThis, Type t) { public static object TypeToObject( CBORObject objThis, - Type t, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t, CBORTypeMapper mapper, PODOptions options, int depth) @@ -1550,7 +1548,7 @@ public static CBORObject FromObjectOther(object obj) } public static object ObjectWithProperties( - Type t, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t, IEnumerable> keysValues, CBORTypeMapper mapper, PODOptions options, @@ -1686,4 +1684,4 @@ public static DateTime BuildUpDateTime(EInteger year, int[] dt) TicksDivFracSeconds); } } -} +} \ No newline at end of file diff --git a/CBOR/PeterO/DebugUtility.cs b/CBOR/PeterO/DebugUtility.cs index 4af138ab..db1d8255 100644 --- a/CBOR/PeterO/DebugUtility.cs +++ b/CBOR/PeterO/DebugUtility.cs @@ -8,6 +8,7 @@ licensed under Creative Commons Zero (CC0): */ using System; using System.Reflection; +using System.Diagnostics.CodeAnalysis; // Use directives rather than the Conditional attribute, // to avoid the chance of logging statements leaking in release builds #if DEBUG @@ -28,15 +29,15 @@ public static void SetWriter(Action wr) } private static MethodInfo GetTypeMethod( - Type t, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t, string name, - Type[] parameters) + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type parameter) { #if NET40 || NET20 - return t.GetMethod(name, parameters); + return t.GetMethod(name, parameter); #else { - return t?.GetRuntimeMethod(name, parameters); + return t?.GetRuntimeMethod(name, new[] { parameter }); } #endif } @@ -71,8 +72,7 @@ public static void Log(string str) #endif } } - Type[] types = new[] { typeof(string) }; - MethodInfo typeMethod = GetTypeMethod(type, "WriteLine", types); + MethodInfo typeMethod = GetTypeMethod(type, "WriteLine", typeof(string)); if (typeMethod != null) { typeMethod.Invoke( From c77740050ffa5b326bd675e314748fd28e2c5268 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Sun, 8 Oct 2023 10:09:38 +0100 Subject: [PATCH 04/29] wip trimming --- CBOR/CBOR.csproj | 1 + CBOR/PeterO/Cbor/CBORDateConverter.cs | 3 +++ CBOR/PeterO/Cbor/CBORNumber.cs | 2 +- CBOR/PeterO/Cbor/CBORObject.cs | 21 +++++++++++++++++++-- CBOR/PeterO/Cbor/CBORObjectExtra.cs | 11 ++++++++++- CBOR/PeterO/Cbor/CBORTypeMapper.cs | 6 ++++-- CBOR/PeterO/Cbor/PropertyMap.cs | 16 +++++++++++----- 7 files changed, 49 insertions(+), 11 deletions(-) diff --git a/CBOR/CBOR.csproj b/CBOR/CBOR.csproj index c71cc64e..67da3f6f 100644 --- a/CBOR/CBOR.csproj +++ b/CBOR/CBOR.csproj @@ -34,6 +34,7 @@ Note that after version 4.5x, the CBOR library's repository will stop including true 11.0 true + MULTI_TARGETING_SUPPORT_ATTRIBUTES diff --git a/CBOR/PeterO/Cbor/CBORDateConverter.cs b/CBOR/PeterO/Cbor/CBORDateConverter.cs index 15318e62..fb22862f 100644 --- a/CBOR/PeterO/Cbor/CBORDateConverter.cs +++ b/CBOR/PeterO/Cbor/CBORDateConverter.cs @@ -8,6 +8,7 @@ licensed under Creative Commons Zero (CC0): */ using PeterO.Numbers; using System; +using System.Diagnostics.CodeAnalysis; namespace PeterO.Cbor { @@ -144,6 +145,7 @@ private static string DateTimeToString(DateTime bi) /// The format of the CBOR /// object is not supported, or another error occurred in /// conversion. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // TODO: fix this method and remove this annotation public DateTime FromCBORObject(CBORObject obj) { if (obj == null) @@ -225,6 +227,7 @@ public bool TryGetDateTimeFields(CBORObject obj, EInteger[] year, int[] } } + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] private string TryGetDateTimeFieldsInternal( CBORObject obj, EInteger[] year, diff --git a/CBOR/PeterO/Cbor/CBORNumber.cs b/CBOR/PeterO/Cbor/CBORNumber.cs index 78f2cb8e..a70081e7 100644 --- a/CBOR/PeterO/Cbor/CBORNumber.cs +++ b/CBOR/PeterO/Cbor/CBORNumber.cs @@ -92,7 +92,7 @@ internal static ICBORNumber GetNumberInterface(NumberKind kind) /// Converts this object's value to a CBOR object. /// A CBOR object that stores this object's value. - public CBORObject ToCBORObject() + public CBORObject ToCBORObject() // TODO: use a safe method to get this { return CBORObject.FromObject(this.value); } diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index fcf59b07..24caa564 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -11,6 +11,7 @@ licensed under Creative Commons Zero (CC0): using System.Collections.Generic; using System.IO; using System.Text; +using System.Diagnostics.CodeAnalysis; // TODO: Add ReadObject that combines Read and ToObject; similarly // for ReadJSON, FromJSONString, FromJSONBytes @@ -594,6 +595,7 @@ public CBORObject this[int index] /// or map. If this is a CBOR map, returns null (not /// CBORObject.Null ) if an item with the given key doesn't /// exist. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public CBORObject GetOrDefault(object key, CBORObject defaultValue) { if (this.Type == CBORType.Array) @@ -1244,6 +1246,7 @@ public static CBORObject FromJSONString( /// typeof(List<String>)); /// . /// + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public object ToObject(Type t) { return this.ToObject(t, null, null, 0); @@ -1273,6 +1276,7 @@ public object ToObject(Type t) /// error occurred when serializing the object. /// The parameter is null. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public object ToObject(Type t, CBORTypeMapper mapper) { return mapper == null ? throw new ArgumentNullException(nameof(mapper)) : this.ToObject(t, mapper, null, 0); @@ -1305,6 +1309,7 @@ public object ToObject(Type t, CBORTypeMapper mapper) /// The given object's /// nesting is too deep, or another error occurred when serializing the /// object. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public object ToObject(Type t, PODOptions options) { return options == null ? throw new ArgumentNullException(nameof(options)) : this.ToObject(t, null, options, 0); @@ -1647,6 +1652,7 @@ public object ToObject(Type t, PODOptions options) /// typeof(List<String>)); /// . /// + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public object ToObject(Type t, CBORTypeMapper mapper, PODOptions options) { @@ -1696,6 +1702,7 @@ public object ToObject(Type t, CBORTypeMapper mapper, PODOptions /// name='data'/> is null, or the parameter is /// null, or the parameter or is null. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static object DecodeObjectFromBytes( byte[] data, CBOREncodeOptions enc, @@ -1744,6 +1751,7 @@ public static object DecodeObjectFromBytes( /// The parameter is null, or the parameter is /// null, or the parameter is null. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static object DecodeObjectFromBytes( byte[] data, CBOREncodeOptions enc, @@ -1789,6 +1797,7 @@ public static object DecodeObjectFromBytes( /// The parameter is null, or the parameter or /// is null. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static object DecodeObjectFromBytes( byte[] data, Type t, @@ -1830,13 +1839,14 @@ public static object DecodeObjectFromBytes( /// The parameter is null, or the parameter is /// null. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static object DecodeObjectFromBytes(byte[] data, Type t) { return DecodeObjectFromBytes(data, CBOREncodeOptions.Default, t); } - + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] internal object ToObject( - Type t, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t, CBORTypeMapper mapper, PODOptions options, int depth) @@ -2556,6 +2566,7 @@ public static CBORObject FromObject(long[] array) /// above.. /// A CBOR object corresponding to the given object. Returns /// CBORObject.Null if the object is null. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static CBORObject FromObject(object obj) { return FromObject(obj, PODOptions.Default); @@ -2581,6 +2592,7 @@ public static CBORObject FromObject(object obj) /// CBORObject.Null if the object is null. /// The parameter is null. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static CBORObject FromObject( object obj, PODOptions options) @@ -2608,6 +2620,7 @@ public static CBORObject FromObject( /// CBORObject.Null if the object is null. /// The parameter is null. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static CBORObject FromObject( object obj, CBORTypeMapper mapper) @@ -2837,6 +2850,7 @@ public static CBORObject FromObject( /// v < 0 ? BigInteger.valueOf(1).shiftLeft(64).add(BigInteger.valueOf(v)) : /// BigInteger.valueOf(v)); /// + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static CBORObject FromObject( object obj, CBORTypeMapper mapper, @@ -2845,6 +2859,7 @@ public static CBORObject FromObject( return mapper == null ? throw new ArgumentNullException(nameof(mapper)) : FromObject(obj, options, mapper, 0); } + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // Annotation comes from use of GetProperties internal static CBORObject FromObject( object obj, PODOptions options, @@ -3115,6 +3130,7 @@ public CBORObject WithTag(EInteger bigintTag) /// 2^64-1. /// The parameter is null. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static CBORObject FromObjectAndTag( object valueOb, EInteger bigintTag) @@ -3180,6 +3196,7 @@ public CBORObject WithTag(int smallTag) /// version of CBORObject.Null with the given tag. /// The parameter is less than 0. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static CBORObject FromObjectAndTag( object valueObValue, int smallTag) diff --git a/CBOR/PeterO/Cbor/CBORObjectExtra.cs b/CBOR/PeterO/Cbor/CBORObjectExtra.cs index 35bea914..7506cbe9 100644 --- a/CBOR/PeterO/Cbor/CBORObjectExtra.cs +++ b/CBOR/PeterO/Cbor/CBORObjectExtra.cs @@ -9,6 +9,7 @@ licensed under Creative Commons Zero (CC0): using PeterO.Numbers; using System; using System.IO; +using System.Diagnostics.CodeAnalysis; namespace PeterO.Cbor { @@ -382,6 +383,7 @@ public static CBORObject FromObjectAndTag(object o, ulong tag) /// The converted object. /// The given type "T", or this /// object's CBOR type, is not supported. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public T ToObject() { return (T)this.ToObject(typeof(T)); @@ -407,6 +409,7 @@ public T ToObject() /// The converted object. /// The given type "T", or this /// object's CBOR type, is not supported. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public T ToObject(CBORTypeMapper mapper) { return (T)this.ToObject(typeof(T), mapper); @@ -431,6 +434,7 @@ public T ToObject(CBORTypeMapper mapper) /// The converted object. /// The given type "T", or this /// object's CBOR type, is not supported. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public T ToObject(PODOptions options) { return (T)this.ToObject(typeof(T), options); @@ -458,6 +462,7 @@ public T ToObject(PODOptions options) /// The converted object. /// The given type "T", or this /// object's CBOR type, is not supported. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public T ToObject(CBORTypeMapper mapper, PODOptions options) { return (T)this.ToObject(typeof(T), mapper, options); @@ -506,6 +511,7 @@ public T ToObject(CBORTypeMapper mapper, PODOptions options) /// name='data'/> is null, or the parameter is /// null, or the parameter "T" or is /// null. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static T DecodeObjectFromBytes( byte[] data, CBOREncodeOptions enc, @@ -550,6 +556,7 @@ public static T DecodeObjectFromBytes( /// The parameter is null, or the parameter is /// null. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static T DecodeObjectFromBytes(byte[] data, CBOREncodeOptions enc) { @@ -593,6 +600,7 @@ public static T DecodeObjectFromBytes(byte[] data, CBOREncodeOptions /// The parameter is null, or the parameter "T" or is null. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static T DecodeObjectFromBytes( byte[] data, CBORTypeMapper mapper, @@ -631,9 +639,10 @@ public static T DecodeObjectFromBytes( /// error occurred when serializing the object. /// The parameter is null. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static T DecodeObjectFromBytes(byte[] data) { return (T)DecodeObjectFromBytes(data, typeof(T)); } } -} +} \ No newline at end of file diff --git a/CBOR/PeterO/Cbor/CBORTypeMapper.cs b/CBOR/PeterO/Cbor/CBORTypeMapper.cs index b8ecaa1b..7096bc39 100644 --- a/CBOR/PeterO/Cbor/CBORTypeMapper.cs +++ b/CBOR/PeterO/Cbor/CBORTypeMapper.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; namespace PeterO.Cbor { @@ -42,6 +43,7 @@ public CBORTypeMapper() /// name='type'/> or is null. /// Converter doesn't contain a /// proper ToCBORObject method". + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public CBORTypeMapper AddConverter( Type type, ICBORConverter converter) @@ -58,7 +60,7 @@ public CBORTypeMapper AddConverter( { Converter = converter, ToObject = PropertyMap.FindOneArgumentMethod( - converter, + typeof(ICBORConverter), "ToCBORObject", type), }; @@ -68,7 +70,7 @@ public CBORTypeMapper AddConverter( "Converter doesn't contain a proper ToCBORObject method"); } ci.FromObject = PropertyMap.FindOneArgumentMethod( - converter, + typeof(ICBORConverter), "FromCBORObject", typeof(CBORObject)); this.converters[type] = ci; diff --git a/CBOR/PeterO/Cbor/PropertyMap.cs b/CBOR/PeterO/Cbor/PropertyMap.cs index 23c5ff03..31d140ac 100644 --- a/CBOR/PeterO/Cbor/PropertyMap.cs +++ b/CBOR/PeterO/Cbor/PropertyMap.cs @@ -587,7 +587,7 @@ private static string RemoveIsPrefix(string pn) return CBORUtilities.NameStartsWithWord(pn, "Is") ? pn.Substring(2) : pn; } - + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] private static IList GetPropertyList(Type t) { { @@ -764,6 +764,7 @@ public static CBORObject BuildCBORArray(int[] dimensions) return ret; } + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static CBORObject FromArray( object arrObj, PODOptions options, @@ -846,6 +847,7 @@ private static void SetCBORObject( ret[ilen] = obj; } + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static Array FillArray( Array arr, Type elementType, @@ -1033,11 +1035,11 @@ public static IDictionary NewOrderedDict() } public static object FindOneArgumentMethod( - object obj, + [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type objType, string name, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type argtype) { - return GetTypeMethod(obj.GetType(), name, argtype ); + return GetTypeMethod(objType, name, argtype ); } public static object InvokeOneArgumentMethod( @@ -1100,7 +1102,7 @@ private static object TypeToIntegerObject(CBORObject objThis, Type t) { } throw new CBORException("Type not supported"); } - + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static object TypeToObject( CBORObject objThis, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t, @@ -1358,7 +1360,7 @@ public static object TypeToObject( if (objThis.Type == CBORType.Array && genericListObject != null) { object addMethod = FindOneArgumentMethod( - genericListObject, + genericListObject.GetType(), "Add", objectType); if (addMethod == null) @@ -1547,6 +1549,7 @@ public static CBORObject FromObjectOther(object obj) return null; } + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static object ObjectWithProperties( [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t, IEnumerable> keysValues, @@ -1612,12 +1615,14 @@ public static object CallFromObject( obj); } + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static IEnumerable> GetProperties( object o) { return GetProperties(o, true); } + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static IEnumerable GetPropertyNames(Type t, bool useCamelCase) { @@ -1627,6 +1632,7 @@ public static IEnumerable GetPropertyNames(Type t, bool } } + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static IEnumerable> GetProperties( object o, bool useCamelCase) From 84bcd4bdac02f39588498b88d54bfc134cb4b8e7 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Mon, 9 Oct 2023 22:07:37 +0100 Subject: [PATCH 05/29] fix merge --- CBOR/PeterO/Cbor/CBORObjectExtra.cs | 2 +- CBOR/PeterO/Cbor/PropertyMap.cs | 6 ++---- CBOR/PeterO/DebugUtility.cs | 5 ++--- CBORDocs2/DocVisitor.cs | 2 +- CBORDocs2/TypeNameUtil.cs | 4 ++-- CBORTest/CBORPlistWriter.cs | 4 ++-- 6 files changed, 10 insertions(+), 13 deletions(-) diff --git a/CBOR/PeterO/Cbor/CBORObjectExtra.cs b/CBOR/PeterO/Cbor/CBORObjectExtra.cs index cdc770ce..7aab8133 100644 --- a/CBOR/PeterO/Cbor/CBORObjectExtra.cs +++ b/CBOR/PeterO/Cbor/CBORObjectExtra.cs @@ -605,4 +605,4 @@ public static T DecodeObjectFromBytes(byte[] data) { return (T)DecodeObjectFromBytes(data, typeof(T)); } } -} \ No newline at end of file +} diff --git a/CBOR/PeterO/Cbor/PropertyMap.cs b/CBOR/PeterO/Cbor/PropertyMap.cs index 5fc719f2..8459f404 100644 --- a/CBOR/PeterO/Cbor/PropertyMap.cs +++ b/CBOR/PeterO/Cbor/PropertyMap.cs @@ -451,7 +451,6 @@ private static Type FirstGenericArgument(Type type) { return type.GenericTypeArguments[0]; } - private static bool IsAssignableFrom(Type superType, Type subType) { return superType.GetTypeInfo().IsAssignableFrom(subType.GetTypeInfo()); } @@ -466,7 +465,6 @@ private static IEnumerable GetTypeFields(Type t) { return t.GetRuntimeFields(); } - [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] private static IEnumerable GetTypeInterfaces(Type t) { return t.GetTypeInfo().ImplementedInterfaces; @@ -872,7 +870,7 @@ public static object FindOneArgumentMethod( Type objType, string name, Type argtype) { - return GetTypeMethod(obj.GetType(), name, new[] { argtype }); + return GetTypeMethod(objType, name, argtype); } public static object InvokeOneArgumentMethod( @@ -1438,4 +1436,4 @@ public static DateTime BuildUpDateTime(EInteger year, int[] dt) { TicksDivFracSeconds); } } -} \ No newline at end of file +} diff --git a/CBOR/PeterO/DebugUtility.cs b/CBOR/PeterO/DebugUtility.cs index d0b97d5d..c7c031bf 100644 --- a/CBOR/PeterO/DebugUtility.cs +++ b/CBOR/PeterO/DebugUtility.cs @@ -62,9 +62,8 @@ public static void Log(string str) { #endif } } - Type[] types = new[] { typeof(string) }; - if (typeMethod != null) - { + MethodInfo typeMethod = GetTypeMethod(type, "WriteLine", typeof(string)); + if (typeMethod != null) { typeMethod.Invoke( type, new object[] { str }); diff --git a/CBORDocs2/DocVisitor.cs b/CBORDocs2/DocVisitor.cs index edb82be6..c5dbb2f3 100644 --- a/CBORDocs2/DocVisitor.cs +++ b/CBORDocs2/DocVisitor.cs @@ -800,7 +800,7 @@ private static string Heading(MemberInfo info) { } else if (info is PropertyInfo property) { return property.Name; } else { - return (info is FieldInfo field) ? field.Name : (ret); + return (info is FieldInfo field) ? field.Name : ret; } } diff --git a/CBORDocs2/TypeNameUtil.cs b/CBORDocs2/TypeNameUtil.cs index 7c943524..b9a9fb83 100644 --- a/CBORDocs2/TypeNameUtil.cs +++ b/CBORDocs2/TypeNameUtil.cs @@ -130,8 +130,8 @@ public static string XmlDocMemberName(object obj) { } _ = msb.Append(XmlDocTypeName( p.ParameterType, - true, - genericMethod)); + true, + genericMethod)); first = false; } _ = msb.Append(')'); diff --git a/CBORTest/CBORPlistWriter.cs b/CBORTest/CBORPlistWriter.cs index 53cce5d0..5371d749 100644 --- a/CBORTest/CBORPlistWriter.cs +++ b/CBORTest/CBORPlistWriter.cs @@ -329,8 +329,8 @@ internal static void WritePlistToInternalCore( str = sb.ToString(); break; } - default: str = key.ToJSONString(options); - break; + default: str = key.ToJSONString(options); + break; } if (stringMap.ContainsKey(str)) { throw new CBORException( From 4d57b1471228129446f52aad5e825f3ef778e544 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Mon, 9 Oct 2023 22:07:54 +0100 Subject: [PATCH 06/29] fix merge --- CBORTest/CBORPlistWriter.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/CBORTest/CBORPlistWriter.cs b/CBORTest/CBORPlistWriter.cs index 5371d749..b1760b82 100644 --- a/CBORTest/CBORPlistWriter.cs +++ b/CBORTest/CBORPlistWriter.cs @@ -329,8 +329,9 @@ internal static void WritePlistToInternalCore( str = sb.ToString(); break; } - default: str = key.ToJSONString(options); - break; + default: + str = key.ToJSONString(options); + break; } if (stringMap.ContainsKey(str)) { throw new CBORException( From f21918ea524481c7268ed47a9fb0edaeb0008384 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Mon, 9 Oct 2023 22:17:34 +0100 Subject: [PATCH 07/29] remove unused --- CBOR/PeterO/Cbor/CBORDateConverter.cs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/CBOR/PeterO/Cbor/CBORDateConverter.cs b/CBOR/PeterO/Cbor/CBORDateConverter.cs index fe8fd478..93cee052 100644 --- a/CBOR/PeterO/Cbor/CBORDateConverter.cs +++ b/CBOR/PeterO/Cbor/CBORDateConverter.cs @@ -115,17 +115,6 @@ public CBORDateConverter(ConversionType convType) { this.Type = convType; } - private static string DateTimeToString(DateTime bi) { - try { - var lesserFields = new int[7]; - var outYear = new EInteger[1]; - PropertyMap.BreakDownDateTime(bi, outYear, lesserFields); - return CBORUtilities.ToAtomDateTimeString(outYear[0], lesserFields); - } catch (ArgumentException ex) { - throw new CBORException(ex.Message, ex); - } - } - /// Converts a CBOR object to a DateTime (in DotNet) or a Date /// (in Java). /// A CBOR object that specifies a date/time From 6e70900ad28430a741f60a1fb46e8fe844845ca8 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Mon, 9 Oct 2023 22:22:41 +0100 Subject: [PATCH 08/29] toEDecimal --- CBOR/PeterO/Cbor/CBORDateConverter.cs | 3 +-- CBOR/PeterO/Cbor/CBORObject.cs | 8 ++++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/CBOR/PeterO/Cbor/CBORDateConverter.cs b/CBOR/PeterO/Cbor/CBORDateConverter.cs index 93cee052..87895833 100644 --- a/CBOR/PeterO/Cbor/CBORDateConverter.cs +++ b/CBOR/PeterO/Cbor/CBORDateConverter.cs @@ -203,7 +203,6 @@ public bool TryGetDateTimeFields(CBORObject obj, EInteger[] year, int[] } } - [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] private string TryGetDateTimeFieldsInternal( CBORObject obj, EInteger[] year, @@ -249,7 +248,7 @@ private string TryGetDateTimeFieldsInternal( lesserFields); } else { EDecimal dec; - dec = (EDecimal)untagobj.ToObject(typeof(EDecimal)); + dec = untagobj.ToEDecimal(); CBORUtilities.BreakDownSecondsSinceEpoch( dec, outYear, diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index b9b87e77..6cea23ac 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -1755,6 +1755,11 @@ public static object DecodeObjectFromBytes( public static object DecodeObjectFromBytes(byte[] data, Type t) { return DecodeObjectFromBytes(data, CBOREncodeOptions.Default, t); } + internal EDecimal ToEDecimal() { + CBORNumber cn = this.AsNumber(); + return cn.GetNumberInterface().AsEDecimal(cn.GetValue()); + } + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] internal object ToObject( [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t, @@ -1793,8 +1798,7 @@ internal object ToObject( // might throw InvalidOperationException rather than CBORException. // Make them throw CBORException in next major version. if (t.Equals(typeof(EDecimal))) { - CBORNumber cn = this.AsNumber(); - return cn.GetNumberInterface().AsEDecimal(cn.GetValue()); + this.ToEDecimal(); } if (t.Equals(typeof(EFloat))) { var cn = CBORNumber.FromCBORObject(this); From e3e9c62d2050ec7e1d78fa7194426467e1620997 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Mon, 9 Oct 2023 23:08:16 +0100 Subject: [PATCH 09/29] add safe named static methods and use them --- CBOR/PeterO/Cbor/CBORDataUtilities.cs | 4 +- .../Cbor/CBORDataUtilitiesByteArrayString.cs | 16 +-- .../Cbor/CBORDataUtilitiesCharArrayString.cs | 16 +-- .../Cbor/CBORDataUtilitiesTextString.cs | 16 +-- CBOR/PeterO/Cbor/CBORDateConverter.cs | 8 +- CBOR/PeterO/Cbor/CBORNumber.cs | 4 +- CBOR/PeterO/Cbor/CBORObject.cs | 118 ++++++++++-------- CBOR/PeterO/Cbor/CBORObjectExtra.cs | 22 ++-- CBOR/PeterO/Cbor/CBORReader.cs | 8 +- CBORTest/BEncoding.cs | 2 +- CBORTest/CBORNumberTest.cs | 4 +- CBORTest/CBORObjectTest.cs | 18 +-- CBORTest/CBORTest.cs | 2 +- CBORTest/CBORTestCommon.cs | 4 +- 14 files changed, 130 insertions(+), 112 deletions(-) diff --git a/CBOR/PeterO/Cbor/CBORDataUtilities.cs b/CBOR/PeterO/Cbor/CBORDataUtilities.cs index 341376f6..03627b79 100644 --- a/CBOR/PeterO/Cbor/CBORDataUtilities.cs +++ b/CBOR/PeterO/Cbor/CBORDataUtilities.cs @@ -305,7 +305,7 @@ internal static CBORObject ParseSmallNumberAsNegative( } else { // NOTE: Assumes digit is greater than zero, so PreserveNegativeZeros is // irrelevant - return CBORObject.FromObject(-digit); + return CBORObject.FromInt(-digit); } } @@ -328,7 +328,7 @@ internal static CBORObject ParseSmallNumber(int digit, JSONOptions return CBORObject.FromObject(EDecimal.FromInt32(digit)); } else { // NOTE: Assumes digit is nonnegative, so PreserveNegativeZeros is irrelevant - return CBORObject.FromObject(digit); + return CBORObject.FromInt(digit); } } diff --git a/CBOR/PeterO/Cbor/CBORDataUtilitiesByteArrayString.cs b/CBOR/PeterO/Cbor/CBORDataUtilitiesByteArrayString.cs index cca40071..7a6be86a 100644 --- a/CBOR/PeterO/Cbor/CBORDataUtilitiesByteArrayString.cs +++ b/CBOR/PeterO/Cbor/CBORDataUtilitiesByteArrayString.cs @@ -203,7 +203,7 @@ internal static CBORObject ParseJSONNumber( ? CBORObject.FromFloatingPointBits(EFloat.FromInt64(v).ToDoubleBits(), 8) : kind == JSONOptions.ConversionMode.Decimal128 ? -CBORObject.FromObject(EDecimal.FromInt64(v)) : CBORObject.FromObject(v); +CBORObject.FromObject(EDecimal.FromInt64(v)) : CBORObject.FromInt64(v); } } if (kind == JSONOptions.ConversionMode.Full) { @@ -211,7 +211,7 @@ internal static CBORObject ParseJSONNumber( EInteger ei = EInteger.FromSubstring(chars, initialOffset, endPos); return (preserveNegativeZero && ei.IsZero && negative) ? CBORObject.FromObject(EDecimal.NegativeZero) : -CBORObject.FromObject(ei); +CBORObject.FromEInteger(ei); } if (!haveExponent && haveDecimalPoint) { // No more than 18 digits plus one decimal point (which @@ -243,12 +243,12 @@ internal static CBORObject ParseJSONNumber( } if (digitCount >= 0 && (!negative || lv != 0)) { if (expo == 0) { - return CBORObject.FromObject(lv); + return CBORObject.FromInt64(lv); } else { var cbor = CBORObject.FromArrayBackedObject( new CBORObject[] { - CBORObject.FromObject(expo), - CBORObject.FromObject(lv), + CBORObject.FromInt(expo), + CBORObject.FromInt64(lv), }); return cbor.WithTag(4); } @@ -270,7 +270,7 @@ internal static CBORObject ParseJSONNumber( CBORObject.FromObject(ed.Negate()) : CBORObject.FromObject(ed); } } else { - return ed.Exponent.IsZero ? CBORObject.FromObject(ed.Mantissa) : + return ed.Exponent.IsZero ? CBORObject.FromEInteger(ed.Mantissa) : CBORObject.FromObject(ed); } } else if (kind == JSONOptions.ConversionMode.Double) { @@ -303,7 +303,7 @@ internal static CBORObject ParseJSONNumber( long lb = ef.ToDoubleBits(); return (!CBORUtilities.IsBeyondSafeRange(lb) && CBORUtilities.IsIntegerValue(lb)) ? - CBORObject.FromObject(CBORUtilities.GetIntegerValue(lb)) : + CBORObject.FromInt64(CBORUtilities.GetIntegerValue(lb)) : CBORObject.FromFloatingPointBits(lb, 8); } else if (kind == JSONOptions.ConversionMode.IntOrFloat) { EContext ctx = EContext.Binary64.WithBlankFlags(); @@ -325,7 +325,7 @@ internal static CBORObject ParseJSONNumber( // Exact conversion; treat as ConversionMode.IntToFloatFromDouble return (!CBORUtilities.IsBeyondSafeRange(lb) && CBORUtilities.IsIntegerValue(lb)) ? - CBORObject.FromObject(CBORUtilities.GetIntegerValue(lb)) : + CBORObject.FromInt64(CBORUtilities.GetIntegerValue(lb)) : CBORObject.FromFloatingPointBits(lb, 8); } } else { diff --git a/CBOR/PeterO/Cbor/CBORDataUtilitiesCharArrayString.cs b/CBOR/PeterO/Cbor/CBORDataUtilitiesCharArrayString.cs index a5795a78..bb983f89 100644 --- a/CBOR/PeterO/Cbor/CBORDataUtilitiesCharArrayString.cs +++ b/CBOR/PeterO/Cbor/CBORDataUtilitiesCharArrayString.cs @@ -203,7 +203,7 @@ internal static CBORObject ParseJSONNumber( ? CBORObject.FromFloatingPointBits(EFloat.FromInt64(v).ToDoubleBits(), 8) : kind == JSONOptions.ConversionMode.Decimal128 ? -CBORObject.FromObject(EDecimal.FromInt64(v)) : CBORObject.FromObject(v); +CBORObject.FromObject(EDecimal.FromInt64(v)) : CBORObject.FromInt64(v); } } if (kind == JSONOptions.ConversionMode.Full) { @@ -211,7 +211,7 @@ internal static CBORObject ParseJSONNumber( EInteger ei = EInteger.FromSubstring(chars, initialOffset, endPos); return (preserveNegativeZero && ei.IsZero && negative) ? CBORObject.FromObject(EDecimal.NegativeZero) : -CBORObject.FromObject(ei); +CBORObject.FromEInteger(ei); } if (!haveExponent && haveDecimalPoint) { // No more than 18 digits plus one decimal point (which @@ -243,12 +243,12 @@ internal static CBORObject ParseJSONNumber( } if (digitCount >= 0 && (!negative || lv != 0)) { if (expo == 0) { - return CBORObject.FromObject(lv); + return CBORObject.FromInt64(lv); } else { var cbor = CBORObject.FromArrayBackedObject( new CBORObject[] { - CBORObject.FromObject(expo), - CBORObject.FromObject(lv), + CBORObject.FromInt(expo), + CBORObject.FromInt64(lv), }); return cbor.WithTag(4); } @@ -270,7 +270,7 @@ internal static CBORObject ParseJSONNumber( CBORObject.FromObject(ed.Negate()) : CBORObject.FromObject(ed); } } else { - return ed.Exponent.IsZero ? CBORObject.FromObject(ed.Mantissa) : + return ed.Exponent.IsZero ? CBORObject.FromEInteger(ed.Mantissa) : CBORObject.FromObject(ed); } } else if (kind == JSONOptions.ConversionMode.Double) { @@ -303,7 +303,7 @@ internal static CBORObject ParseJSONNumber( long lb = ef.ToDoubleBits(); return (!CBORUtilities.IsBeyondSafeRange(lb) && CBORUtilities.IsIntegerValue(lb)) ? - CBORObject.FromObject(CBORUtilities.GetIntegerValue(lb)) : + CBORObject.FromInt64(CBORUtilities.GetIntegerValue(lb)) : CBORObject.FromFloatingPointBits(lb, 8); } else if (kind == JSONOptions.ConversionMode.IntOrFloat) { EContext ctx = EContext.Binary64.WithBlankFlags(); @@ -325,7 +325,7 @@ internal static CBORObject ParseJSONNumber( // Exact conversion; treat as ConversionMode.IntToFloatFromDouble return (!CBORUtilities.IsBeyondSafeRange(lb) && CBORUtilities.IsIntegerValue(lb)) ? - CBORObject.FromObject(CBORUtilities.GetIntegerValue(lb)) : + CBORObject.FromInt64(CBORUtilities.GetIntegerValue(lb)) : CBORObject.FromFloatingPointBits(lb, 8); } } else { diff --git a/CBOR/PeterO/Cbor/CBORDataUtilitiesTextString.cs b/CBOR/PeterO/Cbor/CBORDataUtilitiesTextString.cs index 9027a24f..aefa0889 100644 --- a/CBOR/PeterO/Cbor/CBORDataUtilitiesTextString.cs +++ b/CBOR/PeterO/Cbor/CBORDataUtilitiesTextString.cs @@ -203,7 +203,7 @@ internal static CBORObject ParseJSONNumber( ? CBORObject.FromFloatingPointBits(EFloat.FromInt64(v).ToDoubleBits(), 8) : kind == JSONOptions.ConversionMode.Decimal128 ? -CBORObject.FromObject(EDecimal.FromInt64(v)) : CBORObject.FromObject(v); +CBORObject.FromObject(EDecimal.FromInt64(v)) : CBORObject.FromInt64(v); } } if (kind == JSONOptions.ConversionMode.Full) { @@ -211,7 +211,7 @@ internal static CBORObject ParseJSONNumber( EInteger ei = EInteger.FromSubstring(chars, initialOffset, endPos); return (preserveNegativeZero && ei.IsZero && negative) ? CBORObject.FromObject(EDecimal.NegativeZero) : -CBORObject.FromObject(ei); +CBORObject.FromEInteger(ei); } if (!haveExponent && haveDecimalPoint) { // No more than 18 digits plus one decimal point (which @@ -243,12 +243,12 @@ internal static CBORObject ParseJSONNumber( } if (digitCount >= 0 && (!negative || lv != 0)) { if (expo == 0) { - return CBORObject.FromObject(lv); + return CBORObject.FromInt64(lv); } else { var cbor = CBORObject.FromArrayBackedObject( new CBORObject[] { - CBORObject.FromObject(expo), - CBORObject.FromObject(lv), + CBORObject.FromInt(expo), + CBORObject.FromInt64(lv), }); return cbor.WithTag(4); } @@ -266,7 +266,7 @@ internal static CBORObject ParseJSONNumber( CBORObject.FromObject(0) : !preserveNegativeZero ? CBORObject.FromObject(ed.Negate()) : CBORObject.FromObject(ed) : - ed.Exponent.IsZero ? CBORObject.FromObject(ed.Mantissa) : + ed.Exponent.IsZero ? CBORObject.FromEInteger(ed.Mantissa) : CBORObject.FromObject(ed); } else if (kind == JSONOptions.ConversionMode.Double) { var ef = EFloat.FromString( @@ -298,7 +298,7 @@ internal static CBORObject ParseJSONNumber( long lb = ef.ToDoubleBits(); return (!CBORUtilities.IsBeyondSafeRange(lb) && CBORUtilities.IsIntegerValue(lb)) ? - CBORObject.FromObject(CBORUtilities.GetIntegerValue(lb)) : + CBORObject.FromInt64(CBORUtilities.GetIntegerValue(lb)) : CBORObject.FromFloatingPointBits(lb, 8); } else if (kind == JSONOptions.ConversionMode.IntOrFloat) { EContext ctx = EContext.Binary64.WithBlankFlags(); @@ -320,7 +320,7 @@ internal static CBORObject ParseJSONNumber( // Exact conversion; treat as ConversionMode.IntToFloatFromDouble return (!CBORUtilities.IsBeyondSafeRange(lb) && CBORUtilities.IsIntegerValue(lb)) ? - CBORObject.FromObject(CBORUtilities.GetIntegerValue(lb)) : + CBORObject.FromInt64(CBORUtilities.GetIntegerValue(lb)) : CBORObject.FromFloatingPointBits(lb, 8); } } else { diff --git a/CBOR/PeterO/Cbor/CBORDateConverter.cs b/CBOR/PeterO/Cbor/CBORDateConverter.cs index 87895833..6ce38788 100644 --- a/CBOR/PeterO/Cbor/CBORDateConverter.cs +++ b/CBOR/PeterO/Cbor/CBORDateConverter.cs @@ -284,7 +284,7 @@ private string TryGetDateTimeFieldsInternal( lesserFields); } else { EDecimal dec; - dec = (EDecimal)untagobj.ToObject(typeof(EDecimal)); + dec = untagobj.ToEDecimal(); CBORUtilities.BreakDownSecondsSinceEpoch( dec, outYear, @@ -394,7 +394,7 @@ public CBORObject DateTimeFieldsToCBORObject(EInteger bigYear, int[] { string str = CBORUtilities.ToAtomDateTimeString(bigYear, lesserFields); - return CBORObject.FromObjectAndTag(str, 0); + return CBORObject.FromString(str).WithTag(0); } case ConversionType.TaggedNumber: case ConversionType.UntaggedNumber: @@ -406,8 +406,8 @@ public CBORObject DateTimeFieldsToCBORObject(EInteger bigYear, int[] status); return status[0] == 0 ? this.Type == ConversionType.TaggedNumber ? - CBORObject.FromObjectAndTag(ef.ToEInteger(), 1) : - CBORObject.FromObject(ef.ToEInteger()) : status[0] == 1 ? + CBORObject.FromEInteger(ef.ToEInteger()).WithTag(1) : + CBORObject.FromEInteger(ef.ToEInteger()) : status[0] == 1 ? this.Type == ConversionType.TaggedNumber ? CBORObject.FromFloatingPointBits(ef.ToDoubleBits(), 8) .WithTag(1) : diff --git a/CBOR/PeterO/Cbor/CBORNumber.cs b/CBOR/PeterO/Cbor/CBORNumber.cs index 0a73e045..2a34a6d9 100644 --- a/CBOR/PeterO/Cbor/CBORNumber.cs +++ b/CBOR/PeterO/Cbor/CBORNumber.cs @@ -1477,7 +1477,7 @@ public CBORNumber Remainder(CBORNumber b) { /// if they receive a null argument rather than treating null as less /// or greater than any object.. public int CompareTo(int other) { - return this.CompareTo(CBORObject.FromObject(other).AsNumber()); + return this.CompareTo(CBORObject.FromInt(other).AsNumber()); } /// Compares this CBOR number with a 64-bit signed integer. In @@ -1497,7 +1497,7 @@ public int CompareTo(int other) { /// if they receive a null argument rather than treating null as less /// or greater than any object.. public int CompareTo(long other) { - return this.CompareTo(CBORObject.FromObject(other).AsNumber()); + return this.CompareTo(CBORObject.FromInt64(other).AsNumber()); } /// Compares this CBOR number with another. In this diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index 6cea23ac..49e3bc7d 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -527,7 +527,7 @@ public CBORObject this[int index] } if (this.Type == CBORType.Map) { IDictionary map = this.AsMap(); - var key = CBORObject.FromObject(index); + var key = CBORObject.FromInt(index); // TODO: In next major version, consider throwing an exception // instead if key does not exist. return PropertyMap.GetOrDefault(map, key, null); @@ -544,7 +544,7 @@ public CBORObject this[int index] list[index] = value ?? throw new ArgumentNullException(nameof(value)); } else if (this.Type == CBORType.Map) { IDictionary map = this.AsMap(); - var key = CBORObject.FromObject(index); + var key = CBORObject.FromInt(index); map[key] = value; } else { throw new InvalidOperationException("Not an array or map"); @@ -969,7 +969,7 @@ public static CBORObject DecodeFromBytes( // value with tag 0 string s = GetOptimizedStringIfShortAscii(data, 1); if (s != null) { - return new CBORObject(FromObject(s), 0, 0); + return new CBORObject(FromString(s), 0, 0); } } // For objects with variable length, @@ -1833,11 +1833,12 @@ internal object ToObject( /// The parameter is a /// 64-bit signed integer. /// A CBOR object. - public static CBORObject FromObject(long value) { + public static CBORObject FromInt64(long value) { return value >= 0L && value < 24L ? FixedObjects[(int)value] : (value >= -24L && value < 0L) ? FixedObjects[0x20 - (int)(value + 1L)] : new CBORObject(CBORObjectTypeInteger, value); } + // TODO: readd FromObject(long value) as deprecated /// Generates a CBOR object from a CBOR object. /// The parameter is a @@ -1989,12 +1990,12 @@ private long CalcEncodedSize(int depth) { /// The given number encoded as a CBOR object. Returns /// CBORObject.Null if is /// null. - public static CBORObject FromObject(EInteger bigintValue) { + public static CBORObject FromEInteger(EInteger bigintValue) { if (bigintValue == null) { return CBORObject.Null; } if (bigintValue.CanFitInInt64()) { - return CBORObject.FromObject(bigintValue.ToInt64Checked()); + return CBORObject.FromInt64(bigintValue.ToInt64Checked()); } else { EInteger bitLength = bigintValue.GetSignedBitLengthAsEInteger(); if (bitLength.CompareTo(64) <= 0) { @@ -2008,6 +2009,9 @@ public static CBORObject FromObject(EInteger bigintValue) { } } } + // public static CBORObject FromObject(EInteger bigintValue) { // TODO: reinstate as deprecated + // return FromEInteger(bigintValue); + // } /// Generates a CBOR object from an arbitrary-precision binary /// floating-point number. The CBOR object is generated as follows @@ -2044,24 +2048,24 @@ public static CBORObject FromObject(EFloat bigValue) { if (bigValue.IsSignalingNaN()) { options += 6; } - cbor = CBORObject.NewArray( - CBORObject.FromObject(bigValue.Exponent), - CBORObject.FromObject(bigValue.UnsignedMantissa), - CBORObject.FromObject(options)); + cbor = NewArray( + FromEInteger(bigValue.Exponent), + FromEInteger(bigValue.UnsignedMantissa), + FromInt(options)); tag = 269; } else { EInteger exponent = bigValue.Exponent; if (exponent.CanFitInInt64()) { tag = 5; - cbor = CBORObject.NewArray( - CBORObject.FromObject(exponent.ToInt64Checked()), - CBORObject.FromObject(bigValue.Mantissa)); + cbor = NewArray( + FromInt64(exponent.ToInt64Checked()), + FromEInteger(bigValue.Mantissa)); } else { tag = (exponent.GetSignedBitLengthAsInt64() > 64) ? 265 : 5; - cbor = CBORObject.NewArray( - CBORObject.FromObject(exponent), - CBORObject.FromObject(bigValue.Mantissa)); + cbor = NewArray( + FromEInteger(exponent), + FromEInteger(bigValue.Mantissa)); } } return cbor.WithTag(tag); @@ -2083,7 +2087,7 @@ public static CBORObject FromObject(EFloat bigValue) { /// CBORObject.Null if is null. public static CBORObject FromObject(ERational bigValue) { if (bigValue == null) { - return CBORObject.Null; + return Null; } CBORObject cbor; int tag; @@ -2118,16 +2122,16 @@ public static CBORObject FromObject(ERational bigValue) { } #endif - cbor = CBORObject.NewArray( - FromObject(bigValue.UnsignedNumerator), - FromObject(bigValue.Denominator), - FromObject(options)); + cbor = NewArray( + FromEInteger(bigValue.UnsignedNumerator), + FromEInteger(bigValue.Denominator), + FromInt(options)); tag = 270; } else { tag = 30; - cbor = CBORObject.NewArray( - CBORObject.FromObject(bigValue.Numerator), - CBORObject.FromObject(bigValue.Denominator)); + cbor = NewArray( + FromEInteger(bigValue.Numerator), + FromEInteger(bigValue.Denominator)); } return cbor.WithTag(tag); } @@ -2166,24 +2170,24 @@ public static CBORObject FromObject(EDecimal bigValue) { if (bigValue.IsSignalingNaN()) { options += 6; } - cbor = CBORObject.NewArray( - FromObject(bigValue.Exponent), - FromObject(bigValue.UnsignedMantissa), - FromObject(options)); + cbor = NewArray( + FromEInteger(bigValue.Exponent), + FromEInteger(bigValue.UnsignedMantissa), + CBORObject.FromInt(options)); tag = 268; } else { EInteger exponent = bigValue.Exponent; if (exponent.CanFitInInt64()) { tag = 4; - cbor = CBORObject.NewArray( - CBORObject.FromObject(exponent.ToInt64Checked()), - CBORObject.FromObject(bigValue.Mantissa)); + cbor = NewArray( + FromInt64(exponent.ToInt64Checked()), + FromEInteger(bigValue.Mantissa)); } else { tag = (exponent.GetSignedBitLengthAsInt64() > 64) ? 264 : 4; - cbor = CBORObject.NewArray( - CBORObject.FromObject(exponent), - CBORObject.FromObject(bigValue.Mantissa)); + cbor = NewArray( + FromEInteger(exponent), + FromEInteger(bigValue.Mantissa)); } } return cbor.WithTag(tag); @@ -2195,7 +2199,7 @@ public static CBORObject FromObject(EDecimal bigValue) { /// if stringValue is null. /// The string contains an unpaired /// surrogate code point. - public static CBORObject FromObject(string strValue) { + public static CBORObject FromString(string strValue) { if (strValue == null) { return CBORObject.Null; } @@ -2209,17 +2213,21 @@ public static CBORObject FromObject(string strValue) { strValue.Length == utf8Length ? CBORObjectTypeTextStringAscii : CBORObjectTypeTextString, strValue); } + // public static CBORObject FromObject(string strValue) { // TODO: reinstate as deprecated + // return FromString(strValue); + // } /// Generates a CBOR object from a 32-bit signed /// integer. /// The parameter is a /// 32-bit signed integer. /// A CBOR object. - public static CBORObject FromObject(int value) { + public static CBORObject FromInt(int value) { return value >= 0 && value < 24 ? FixedObjects[value] : (value >= -24 && value < 0) ? FixedObjects[0x20 - (value + 1)] : - FromObject((long)value); + FromInt64((long)value); } + // TODO: readd FromObject(int value) as deprecated /// Generates a CBOR object from a 16-bit signed /// integer. @@ -2229,7 +2237,7 @@ public static CBORObject FromObject(int value) { public static CBORObject FromObject(short value) { return value >= 0 && value < 24 ? FixedObjects[value] : (value >= -24 && value < 0) ? FixedObjects[0x20 - (value + 1)] : - FromObject((long)value); + FromInt64((long)value); } /// Returns the CBOR true value or false value, depending on @@ -2246,7 +2254,7 @@ public static CBORObject FromObject(bool value) { /// byte (from 0 to 255). /// A CBOR object generated from the given integer. public static CBORObject FromObject(byte value) { - return FromObject(value & 0xff); + return FromInt(value & 0xff); } /// Generates a CBOR object from a 32-bit floating-point @@ -2343,7 +2351,7 @@ public static CBORObject FromObject(int[] array) { IList list = new List(array.Length == int.MaxValue ? array.Length : (array.Length + 1)); foreach (int i in array) { - list.Add(FromObject(i)); + list.Add(FromInt(i)); } return new CBORObject(CBORObjectTypeArray, list); } @@ -2361,7 +2369,7 @@ public static CBORObject FromObject(long[] array) { IList list = new List(array.Length == int.MaxValue ? array.Length : (array.Length + 1)); foreach (long i in array) { - list.Add(FromObject(i)); + list.Add(FromInt64(i)); } return new CBORObject(CBORObjectTypeArray, list); } @@ -2703,13 +2711,13 @@ internal static CBORObject FromObject( return FromObject((string)obj); } if (obj is int) { - return FromObject((int)obj); + return FromInt((int)obj); } if (obj is long) { - return FromObject((long)obj); + return FromInt64((long)obj); } if (obj is EInteger eif) { - return FromObject(eif); + return FromEInteger(eif); } if (obj is EDecimal edf) { return FromObject(edf); @@ -2724,7 +2732,7 @@ internal static CBORObject FromObject( return FromObject((short)obj); } if (obj is char) { - return FromObject((int)(char)obj); + return FromInt((int)(char)obj); } if (obj is bool) { return FromObject((bool)obj); @@ -2739,13 +2747,13 @@ internal static CBORObject FromObject( return FromObject((sbyte)obj); } if (obj is ulong) { - return FromObject((ulong)obj); + return FromUInt64((ulong)obj); } if (obj is uint) { - return FromObject((uint)obj); + return FromUInt((uint)obj); } if (obj is ushort) { - return FromObject((ushort)obj); + return FromUShort((ushort)obj); } if (obj is decimal) { return FromObject((decimal)obj); @@ -3968,6 +3976,7 @@ public static void Write(CBORObject value, Stream stream) { /// The arbitrary object to be serialized. Can /// be null. /// A writable data stream. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static void Write(object objValue, Stream stream) { Write(objValue, stream, CBOREncodeOptions.Default); } @@ -4012,6 +4021,7 @@ public static void Write(object objValue, Stream stream) { /// supported. /// The parameter or is null. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static void Write( object objValue, Stream output, @@ -4076,6 +4086,7 @@ public static void Write( /// A writable data stream. /// The parameter is null. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static void WriteJSON(object obj, Stream outputStream) { if (outputStream == null) { throw new ArgumentNullException(nameof(outputStream)); @@ -4116,6 +4127,7 @@ public static void WriteJSON(object obj, Stream outputStream) { /// The parameter or has an unsupported /// type. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public CBORObject Add(object key, object valueOb) { if (this.Type == CBORType.Map) { CBORObject mapKey; @@ -4170,6 +4182,7 @@ public CBORObject Add(object key, object valueOb) { /// string")) .Add(CBORObject.FromObjectAndTag(9999, 1)); /// . /// + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public CBORObject Add(CBORObject obj) { if (this.Type == CBORType.Array) { IList list = this.AsList(); @@ -4202,6 +4215,7 @@ public CBORObject Add(CBORObject obj) { /// .Add("text string") .Add(CBORObject.FromObjectAndTag(9999, 1)); /// . /// + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public CBORObject Add(object obj) { if (this.Type == CBORType.Array) { IList list = this.AsList(); @@ -4708,6 +4722,7 @@ public int CompareToIgnoreTags(CBORObject other) { /// arbitrary object. /// true if the given key is found, or false if /// the given key is not found or this object is not a map. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public bool ContainsKey(object objKey) { return (this.Type == CBORType.Map) && this.ContainsKey(CBORObject.FromObject(objKey)); @@ -4737,8 +4752,7 @@ public bool ContainsKey(CBORObject key) { /// a map. public bool ContainsKey(string key) { if (this.Type == CBORType.Map) { - CBORObject ckey = key == null ? CBORObject.Null : - CBORObject.FromObject(key); + CBORObject ckey = key == null ? Null : FromString(key); IDictionary map = this.AsMap(); return map.ContainsKey(ckey); } @@ -6688,7 +6702,7 @@ internal static CBORObject GetFixedLengthObject( } else { int low = unchecked((int)(uadditional & 0xffffffffL)); int high = unchecked((int)((uadditional >> 32) & 0xffffffffL)); - return FromObject(LowHighToEInteger(low, high)); + return FromEInteger(LowHighToEInteger(low, high)); } case 1: if ((uadditional >> 63) == 0) { @@ -6703,7 +6717,7 @@ internal static CBORObject GetFixedLengthObject( EInteger bigintAdditional = LowHighToEInteger(low, high); EInteger minusOne = -EInteger.One; bigintAdditional = minusOne - bigintAdditional; - return FromObject(bigintAdditional); + return FromEInteger(bigintAdditional); } case 7: if (firstbyte >= 0xf9 && firstbyte <= 0xfb) { diff --git a/CBOR/PeterO/Cbor/CBORObjectExtra.cs b/CBOR/PeterO/Cbor/CBORObjectExtra.cs index 7aab8133..6f555514 100644 --- a/CBOR/PeterO/Cbor/CBORObjectExtra.cs +++ b/CBOR/PeterO/Cbor/CBORObjectExtra.cs @@ -243,10 +243,10 @@ public static void Write(ushort value, Stream stream) { /// 8-bit signed integer. /// A CBORObject object. [CLSCompliant(false)] - public static CBORObject FromObject(sbyte value) { - return FromObject((long)value); + public static CBORObject FromSbyte(sbyte value) { + return FromInt64((long)value); } - + // TODO: re-add FromObject(sbyte value) as deprecated private static EInteger UInt64ToEInteger(ulong value) { var data = new byte[9]; ulong uvalue = value; @@ -267,27 +267,30 @@ private static EInteger UInt64ToEInteger(ulong value) { /// A 64-bit unsigned integer. /// A CBORObject object. [CLSCompliant(false)] - public static CBORObject FromObject(ulong value) { - return CBORObject.FromObject(UInt64ToEInteger(value)); + public static CBORObject FromUInt64(ulong value) { + return CBORObject.FromEInteger(UInt64ToEInteger(value)); } + // TODO: re-add FromObject(ulong value) as deprecated /// Converts a 32-bit unsigned integer to a CBOR /// object. /// A 32-bit unsigned integer. /// A CBORObject object. [CLSCompliant(false)] - public static CBORObject FromObject(uint value) { - return FromObject((long)value); + public static CBORObject FromUInt(uint value) { + return FromInt64((long)value); } + // TODO: re-add FromObject(uint value) as deprecated /// Converts a 16-bit unsigned integer to a CBOR /// object. /// A 16-bit unsigned integer. /// A CBORObject object. [CLSCompliant(false)] - public static CBORObject FromObject(ushort value) { - return FromObject((long)value); + public static CBORObject FromUShort(ushort value) { + return FromInt64((long)value); } + // TODO re-add FromObject(ushort value) as deprecated /// Generates a CBOR object from this one, but gives the /// resulting object a tag in addition to its existing tags (the new @@ -330,6 +333,7 @@ public CBORObject WithTag(ulong tag) { /// . If "valueOb" is null, returns a version of CBORObject.Null with /// the given tag. [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)); } diff --git a/CBOR/PeterO/Cbor/CBORReader.cs b/CBOR/PeterO/Cbor/CBORReader.cs index 09c5206d..c5e0eeb8 100644 --- a/CBOR/PeterO/Cbor/CBORReader.cs +++ b/CBOR/PeterO/Cbor/CBORReader.cs @@ -303,12 +303,12 @@ public CBORObject ReadForFirstByte(int firstbyte) { type == 7); if (type == 0) { return (uadditional >> 63) != 0 ? - CBORObject.FromObject(ToUnsignedEInteger(uadditional)) : - CBORObject.FromObject(uadditional); + CBORObject.FromEInteger(ToUnsignedEInteger(uadditional)) : + CBORObject.FromInt64(uadditional); } else if (type == 1) { - return (uadditional >> 63) != 0 ? CBORObject.FromObject( + return (uadditional >> 63) != 0 ? CBORObject.FromEInteger( ToUnsignedEInteger(uadditional).Add(1).Negate()) : - CBORObject.FromObject((-uadditional) - 1L); + CBORObject.FromInt64((-uadditional) - 1L); } else if (type == 7) { if (additional < 24) { return CBORObject.FromSimpleValue(additional); diff --git a/CBORTest/BEncoding.cs b/CBORTest/BEncoding.cs index 057b5337..41d15ee6 100644 --- a/CBORTest/BEncoding.cs +++ b/CBORTest/BEncoding.cs @@ -67,7 +67,7 @@ private static CBORObject ReadInteger(Stream stream) { throw new CBORException("Invalid integer encoding") : s.Length >= 3 && s[0] == '-' && s[1] == '0' && s[2] == '0' ? throw new CBORException("Invalid integer encoding") : - CBORObject.FromObject( + CBORObject.FromEInteger( EInteger.FromString(s)); } diff --git a/CBORTest/CBORNumberTest.cs b/CBORTest/CBORNumberTest.cs index 3975ec32..c0476ff1 100644 --- a/CBORTest/CBORNumberTest.cs +++ b/CBORTest/CBORNumberTest.cs @@ -86,7 +86,7 @@ public void TestCanFitInUInt64() { Assert.IsTrue(CBORObject.FromObject(-0.0).AsNumber().CanFitInUInt64(), "-0.0"); bool - b = CBORObject.FromObject( + b = CBORObject.FromEInteger( EInteger.FromInt32(1).ShiftLeft(65)).AsNumber().CanFitInUInt64(); Assert.IsFalse(b); @@ -127,7 +127,7 @@ public void TestCanTruncatedIntFitInUInt64() { Assert.IsFalse( CBORObject.FromObject(-99).AsNumber().CanTruncatedIntFitInUInt64()); bool - b = CBORObject.FromObject(EInteger.FromInt32(1).ShiftLeft(65)).AsNumber() + b = CBORObject.FromEInteger(EInteger.FromInt32(1).ShiftLeft(65)).AsNumber() .CanTruncatedIntFitInUInt64(); Assert.IsFalse(b); diff --git a/CBORTest/CBORObjectTest.cs b/CBORTest/CBORObjectTest.cs index 35fa6833..926d4515 100644 --- a/CBORTest/CBORObjectTest.cs +++ b/CBORTest/CBORObjectTest.cs @@ -1234,19 +1234,19 @@ public void TestCanFitInInt64() { EInteger ei; ei = EInteger.FromString("9223372036854775807"); - Assert.IsTrue(CInt64(CBORObject.FromObject(ei)), ei.ToString()); + Assert.IsTrue(CInt64(CBORObject.FromEInteger(ei)), ei.ToString()); ei = EInteger.FromString("9223372036854775808"); - Assert.IsFalse(CInt64(CBORObject.FromObject(ei)), ei.ToString()); + Assert.IsFalse(CInt64(CBORObject.FromEInteger(ei)), ei.ToString()); ei = EInteger.FromString("-9223372036854775807"); - Assert.IsTrue(CInt64(CBORObject.FromObject(ei)), ei.ToString()); + Assert.IsTrue(CInt64(CBORObject.FromEInteger(ei)), ei.ToString()); ei = EInteger.FromString("-9223372036854775808"); - Assert.IsTrue(CInt64(CBORObject.FromObject(ei)), ei.ToString()); + Assert.IsTrue(CInt64(CBORObject.FromEInteger(ei)), ei.ToString()); ei = EInteger.FromString("-9223372036854775809"); - Assert.IsFalse(CInt64(CBORObject.FromObject(ei)), ei.ToString()); + Assert.IsFalse(CInt64(CBORObject.FromEInteger(ei)), ei.ToString()); ei = EInteger.FromString("-9223373136366403584"); - Assert.IsFalse(CInt64(CBORObject.FromObject(ei)), ei.ToString()); + Assert.IsFalse(CInt64(CBORObject.FromEInteger(ei)), ei.ToString()); ei = EInteger.FromString("9223373136366403584"); - Assert.IsFalse(CInt64(CBORObject.FromObject(ei)), ei.ToString()); + Assert.IsFalse(CInt64(CBORObject.FromEInteger(ei)), ei.ToString()); var strings = new string[] { "8000FFFFFFFF0000", "8000AAAAAAAA0000", @@ -1263,9 +1263,9 @@ public void TestCanFitInInt64() { }; foreach (string str in strings) { ei = EInteger.FromRadixString(str, 16); - Assert.IsFalse(CInt64(CBORObject.FromObject(ei))); + Assert.IsFalse(CInt64(CBORObject.FromEInteger(ei))); ei = ei.Negate(); - Assert.IsFalse(CInt64(CBORObject.FromObject(ei))); + Assert.IsFalse(CInt64(CBORObject.FromEInteger(ei))); } CBORObject numbers = GetNumberData(); diff --git a/CBORTest/CBORTest.cs b/CBORTest/CBORTest.cs index e89a8a2c..513f710d 100644 --- a/CBORTest/CBORTest.cs +++ b/CBORTest/CBORTest.cs @@ -1042,7 +1042,7 @@ public static bool TestEquivJSONNumberOne(byte[] bytes) { cbor3 = CBORObject.FromJSONString(str, jsoptions); cbored = (ed.Exponent.CompareTo(0) == 0 && !(ed.IsNegative && ed.Sign == 0)) ? - CBORObject.FromObject(ed.Mantissa) : CBORObject.FromObject(ed); + CBORObject.FromEInteger(ed.Mantissa) : CBORObject.FromObject(ed); Assert.AreEqual(cbor, cbor2, "[" + str + "] cbor2"); Assert.AreEqual(cbor, cbor3, "[" + str + "] cbor3"); Assert.AreEqual(cbor, cbored, "[" + str + "] cbored"); diff --git a/CBORTest/CBORTestCommon.cs b/CBORTest/CBORTestCommon.cs index 96f96807..420af356 100644 --- a/CBORTest/CBORTestCommon.cs +++ b/CBORTest/CBORTestCommon.cs @@ -65,7 +65,7 @@ public static CBORObject RandomNumber(IRandomGenExtended rand, bool int.MaxValue); return CBORObject.FromObject(o); case 2: - return CBORObject.FromObject( + return CBORObject.FromEInteger( RandomObjects.RandomEInteger(rand)); case 3: o = lowExponent ? RandomEFloatLowExponent(rand) : @@ -96,7 +96,7 @@ public static CBORObject RandomNumberOrRational(IRandomGenExtended rand) { int.MaxValue); return CBORObject.FromObject(o); case 2: - return CBORObject.FromObject( + return CBORObject.FromEInteger( RandomObjects.RandomEInteger(rand)); case 3: return CBORObject.FromObject( From ab98e97ab2380735ef7ffa47ab11fd1fae8466b7 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Mon, 9 Oct 2023 23:13:28 +0100 Subject: [PATCH 10/29] fix errors --- CBORTest/CBORExtraTest.cs | 8 ++++---- CBORTest/CBORObjectTest.cs | 28 ++++++++++++++-------------- CBORTest/CBORTest.cs | 4 ++-- CBORTest/JSONWithComments.cs | 2 +- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/CBORTest/CBORExtraTest.cs b/CBORTest/CBORExtraTest.cs index 2934475d..687bb178 100644 --- a/CBORTest/CBORExtraTest.cs +++ b/CBORTest/CBORExtraTest.cs @@ -6573,15 +6573,15 @@ public void TestToObjectNull() { [Test] public void TestNullable() { int? nvalue = 1; - var cbor = CBORObject.FromObject(nvalue); + var cbor = CBORObject.FromObject((object)nvalue); Assert.AreEqual(CBORObject.FromObject(1), cbor); nvalue = null; - _ = CBORObject.FromObject(nvalue); + _ = CBORObject.FromObject((object)nvalue); uint? unvalue = 1u; - cbor = CBORObject.FromObject(unvalue); + cbor = CBORObject.FromObject((object)unvalue); Assert.AreEqual(CBORObject.FromObject(1), cbor); unvalue = null; - cbor = CBORObject.FromObject(unvalue); + cbor = CBORObject.FromObject((object)unvalue); Assert.AreEqual(CBORObject.Null, cbor); Assert.AreEqual(null, CBORObject.Null.ToObject()); Assert.AreEqual(1, CBORObject.FromObject(1).ToObject()); diff --git a/CBORTest/CBORObjectTest.cs b/CBORTest/CBORObjectTest.cs index 926d4515..f81add90 100644 --- a/CBORTest/CBORObjectTest.cs +++ b/CBORTest/CBORObjectTest.cs @@ -1631,39 +1631,39 @@ public void TestCanTruncatedIntFitInInt64() { EInteger ei; ei = EInteger.FromString("9223372036854775807"); { - bool btemp = CBORObject.FromObject(ei) + bool btemp = CBORObject.FromEInteger(ei) .AsNumber().CanTruncatedIntFitInInt64(); Assert.IsTrue(btemp, ei.ToString()); } ei = EInteger.FromString("9223372036854775808"); Assert.IsFalse( - CBORObject.FromObject(ei).AsNumber().CanTruncatedIntFitInInt64(), + CBORObject.FromEInteger(ei).AsNumber().CanTruncatedIntFitInInt64(), ei.ToString()); ei = EInteger.FromString("-9223372036854775807"); Assert.IsTrue( - CBORObject.FromObject(ei).AsNumber().CanTruncatedIntFitInInt64(), + CBORObject.FromEInteger(ei).AsNumber().CanTruncatedIntFitInInt64(), ei.ToString()); ei = EInteger.FromString("-9223372036854775808"); Assert.IsTrue( - CBORObject.FromObject(ei).AsNumber().CanTruncatedIntFitInInt64(), + CBORObject.FromEInteger(ei).AsNumber().CanTruncatedIntFitInInt64(), ei.ToString()); ei = EInteger.FromString("-9223372036854775809"); Assert.IsFalse( - CBORObject.FromObject(ei).AsNumber().CanTruncatedIntFitInInt64(), + CBORObject.FromEInteger(ei).AsNumber().CanTruncatedIntFitInInt64(), ei.ToString()); ei = EInteger.FromString("-9223373136366403584"); Assert.IsFalse( - CBORObject.FromObject(ei).AsNumber().CanTruncatedIntFitInInt64(), + CBORObject.FromEInteger(ei).AsNumber().CanTruncatedIntFitInInt64(), ei.ToString()); ei = EInteger.FromString("9223373136366403584"); Assert.IsFalse( - CBORObject.FromObject(ei).AsNumber().CanTruncatedIntFitInInt64(), + CBORObject.FromEInteger(ei).AsNumber().CanTruncatedIntFitInInt64(), ei.ToString()); var strings = new string[] { "8000FFFFFFFF0000", @@ -1683,11 +1683,11 @@ public void TestCanTruncatedIntFitInInt64() { ei = EInteger.FromRadixString(str, 16); Assert.IsFalse( - CBORObject.FromObject(ei).AsNumber().CanTruncatedIntFitInInt64()); + CBORObject.FromEInteger(ei).AsNumber().CanTruncatedIntFitInInt64()); ei = ei.Negate(); Assert.IsFalse( - CBORObject.FromObject(ei).AsNumber().CanTruncatedIntFitInInt64()); + CBORObject.FromEInteger(ei).AsNumber().CanTruncatedIntFitInInt64()); } CBORObject numbers = GetNumberData(); @@ -1880,7 +1880,7 @@ private static string TrimStr(string str, int len) { [Test] public void CompareLongDouble() { var cbor1 = CBORObject.FromObject(3.5E-15); - var cbor2 = CBORObject.FromObject(281479271677953L); + var cbor2 = CBORObject.FromInt64(281479271677953L); TestCommon.CompareTestLess(cbor1.AsDouble(), cbor2.AsDouble()); } @@ -2602,7 +2602,7 @@ public void TestItem() { CBORObject.FromObject(9), CBORObject.True, CBORObject.FromObject(bytes), CBORObject.False, CBORObject.Null, CBORObject.FromObject("test"), - CBORObject.FromObject(99999), CBORObject.FromObject(-1), + CBORObject.FromInt(99999), CBORObject.FromObject(-1), }; foreach (CBORObject c2 in othercbor) { try { @@ -3032,7 +3032,7 @@ public void TestFromObject() { throw new InvalidOperationException(String.Empty, ex); } try { - _ = CBORObject.FromObject(CBORObject.NewArray().AsNumber().Sign); + _ = CBORObject.FromInt(CBORObject.NewArray().AsNumber().Sign); Assert.Fail("Should have failed"); } catch (InvalidOperationException) { // NOTE: Intentionally empty @@ -3041,7 +3041,7 @@ public void TestFromObject() { throw new InvalidOperationException(String.Empty, ex); } try { - _ = CBORObject.FromObject(CBORObject.NewMap().AsNumber().Sign); + _ = CBORObject.FromInt(CBORObject.NewMap().AsNumber().Sign); Assert.Fail("Should have failed"); } catch (InvalidOperationException) { // NOTE: Intentionally empty @@ -8044,7 +8044,7 @@ private static string DateTimeToString( private static void TestDateTimeStringNumberOne(string str, long num) { CBORObject dtstring = CBORObject.FromObject(str).WithTag(0); - CBORObject dtnum = CBORObject.FromObject(num).WithTag(1); + CBORObject dtnum = CBORObject.FromInt64(num).WithTag(1); TestDateTimeStringNumberOne(dtstring, dtnum); } private static void TestDateTimeStringNumberOne(string str, double num) { diff --git a/CBORTest/CBORTest.cs b/CBORTest/CBORTest.cs index 513f710d..89a5deaa 100644 --- a/CBORTest/CBORTest.cs +++ b/CBORTest/CBORTest.cs @@ -2454,7 +2454,7 @@ public static void TestUnsignedLongOne(long v, string expectedStr) { Assert.AreEqual( expectedStr, DataUtilities.ToLowerCaseAscii(ei.ToRadixString(16))); - var cbor = CBORObject.FromObject(ei); + var cbor = CBORObject.FromEInteger(ei); Assert.IsTrue(cbor.AsNumber().Sign >= 0); TestCommon.AssertEqualsHashCode( ei, @@ -4192,7 +4192,7 @@ public void TestBigNumberThresholds() { for (int i = 0; i < eints.Length; ++i) { CBORObject cbor; bool isNegative = eints[i].Sign < 0; - cbor = CBORObject.FromObject(eints[i]); + cbor = CBORObject.FromEInteger(eints[i]); Assert.IsTrue(cbor.IsNumber, cbor.ToString()); if (isPastCbor[i]) { if (isNegative) { diff --git a/CBORTest/JSONWithComments.cs b/CBORTest/JSONWithComments.cs index a3f0b477..ad411526 100644 --- a/CBORTest/JSONWithComments.cs +++ b/CBORTest/JSONWithComments.cs @@ -567,7 +567,7 @@ internal CBORObject ParseJSONArray(int depth) { // Situation like '[,0,1,2]' or '[0,,1]' this.RaiseError("Empty array element"); } - this.SetPointer(CBORObject.FromObject(arrayIndex)); + this.SetPointer(CBORObject.FromInt64(arrayIndex)); arrayIndex = checked(arrayIndex + 1); _ = myArrayList.Add( this.NextJSONValue( From 87cbc688a0535c729463f6369214c7be6b17dba4 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Tue, 10 Oct 2023 09:27:51 +0100 Subject: [PATCH 11/29] finish annotations --- CBOR.sln | 31 +-------------- CBOR/CBOR.csproj | 2 +- CBOR/PeterO/Cbor/CBORJson2.cs | 17 ++------ CBOR/PeterO/Cbor/CBORNumber.cs | 20 +++++++++- CBOR/PeterO/Cbor/CBORObject.cs | 56 ++++++++++++++------------- CBOR/PeterO/Cbor/CBORObjectExtra.cs | 19 +++------ CBOR/PeterO/Cbor/CBORReader.cs | 8 ++-- CBOR/PeterO/Cbor/CBORUriConverter.cs | 2 +- CBOR/PeterO/Cbor/CBORUuidConverter.cs | 2 +- CBOR/PeterO/Cbor/JSONPatch.cs | 6 +++ CBOR/PeterO/Cbor/JSONPointer.cs | 7 +++- CBOR/PeterO/Cbor/StringRefs.cs | 4 +- CBOR/PeterO/DebugUtility.cs | 2 + TrimTester/Program.cs | 4 -- TrimTester/TrimTester.csproj | 16 -------- 15 files changed, 80 insertions(+), 116 deletions(-) delete mode 100644 TrimTester/Program.cs delete mode 100644 TrimTester/TrimTester.csproj diff --git a/CBOR.sln b/CBOR.sln index a86f3253..8de59b80 100644 --- a/CBOR.sln +++ b/CBOR.sln @@ -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 @@ -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 diff --git a/CBOR/CBOR.csproj b/CBOR/CBOR.csproj index 67da3f6f..0d3f9bea 100644 --- a/CBOR/CBOR.csproj +++ b/CBOR/CBOR.csproj @@ -1,7 +1,7 @@  - netstandard1.0 + netstandard1.0; net7.0 True 5.0-alpha1 Peter Occil diff --git a/CBOR/PeterO/Cbor/CBORJson2.cs b/CBOR/PeterO/Cbor/CBORJson2.cs index 832b7482..8869cad4 100644 --- a/CBOR/PeterO/Cbor/CBORJson2.cs +++ b/CBOR/PeterO/Cbor/CBORJson2.cs @@ -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); @@ -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); } @@ -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 @@ -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); diff --git a/CBOR/PeterO/Cbor/CBORNumber.cs b/CBOR/PeterO/Cbor/CBORNumber.cs index 2a34a6d9..912db97f 100644 --- a/CBOR/PeterO/Cbor/CBORNumber.cs +++ b/CBOR/PeterO/Cbor/CBORNumber.cs @@ -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; } @@ -84,7 +84,23 @@ internal static ICBORNumber GetNumberInterface(NumberKind kind) { /// Converts this object's value to a CBOR object. /// A CBOR object that stores this object's value. 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()); } /// Gets this value's sign: -1 if nonzero and negative; 1 if diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index 49e3bc7d..187534c3 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -570,7 +570,7 @@ public CBORObject this[int index] /// CBORObject.Null ) if an item with the given key doesn't /// exist. [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) { @@ -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]; } @@ -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 map = this.AsMap(); map[objkey] = value; @@ -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); } } } @@ -2304,7 +2302,7 @@ public static CBORObject FromObject(double 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 CBORObject FromObject(byte[] bytes) { + public static CBORObject FromByteArray(byte[] bytes) { if (bytes == null) { return CBORObject.Null; } @@ -2312,6 +2310,7 @@ public static CBORObject FromObject(byte[] bytes) { Array.Copy(bytes, 0, newvalue, 0, bytes.Length); return new CBORObject(CBORObjectTypeByteString, bytes); } + // TODO: readd FromObject(byte[] bytes) as deprecated /// Generates a CBOR object from an array of CBOR /// objects. @@ -2510,7 +2509,7 @@ public static CBORObject FromObject( /// string. To create a CBOR byte string object from String /// , /// see the example given in . + /// cref='PeterO.Cbor.CBORObject.FromByteArray(byte[])'/>. /// In /// the.NET version, a nullable is converted to CBORObject.Null /// if the nullable's value is null @@ -2880,8 +2879,8 @@ public CBORObject WithTag(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). - /// The parameter is - /// an arbitrary object, which can be null. + /// The parameter is + /// a CBORObject. /// NOTE: 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 @@ -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( /// iana.org/assignments/cbor-tags ). - /// A CBOR object where the object - /// is converted to a CBOR object and given the tag . If is null, returns - /// a version of CBORObject.Null with the given tag. + /// A CBOR object where the object + /// is given the tag . /// The parameter is less than 0 or greater than /// 2^64-1. /// The parameter is null. - [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)) : @@ -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 + /// 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). @@ -2943,8 +2942,8 @@ public 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). - /// The parameter is an arbitrary object, which can be null. + /// The parameter is a CBORObject. /// NOTE: 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 @@ -2962,18 +2961,18 @@ public CBORObject WithTag(int smallTag) { /// ( /// iana.org/assignments/cbor-tags ). /// A CBOR object where the object is converted to a CBOR object and given the + /// name='cborObj'/> is given the /// tag . If "valueOb" is null, returns a /// version of CBORObject.Null with the given tag. /// The parameter is less than 0. - [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 /// Creates a CBOR object from a simple value /// number. @@ -4182,7 +4181,6 @@ public CBORObject Add(object key, object valueOb) { /// string")) .Add(CBORObject.FromObjectAndTag(9999, 1)); /// . /// - [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public CBORObject Add(CBORObject obj) { if (this.Type == CBORType.Array) { IList list = this.AsList(); @@ -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. + [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"); @@ -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. + [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); } @@ -5021,6 +5021,7 @@ public CBORObject AtJSONPointer(string pointer, CBORObject 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. + [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); } @@ -5376,6 +5377,7 @@ public bool HasTag(EInteger bigTagValue) { /// The parameter has an unsupported type; or is not a valid index into this array. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public CBORObject Insert(int index, object valueOb) { if (this.Type == CBORType.Array) { CBORObject mapValue; @@ -5426,6 +5428,7 @@ public void Clear() { /// name='obj'/> is null (as opposed to CBORObject.Null). /// The object is not an /// array or map. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public bool Remove(object obj) { return this.Remove(CBORObject.FromObject(obj)); } @@ -5497,6 +5500,7 @@ public bool Remove(CBORObject obj) { /// or this instance is a CBOR array and is less /// than 0, is the size of this array or greater, or is not a 32-bit /// signed integer ( int ). + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public CBORObject Set(object key, object valueOb) { if (this.Type == CBORType.Map) { CBORObject mapKey; diff --git a/CBOR/PeterO/Cbor/CBORObjectExtra.cs b/CBOR/PeterO/Cbor/CBORObjectExtra.cs index 6f555514..294dc173 100644 --- a/CBOR/PeterO/Cbor/CBORObjectExtra.cs +++ b/CBOR/PeterO/Cbor/CBORObjectExtra.cs @@ -306,22 +306,13 @@ public static CBORObject FromUShort(ushort value) { /// (the new tag is made the outermost tag). [CLSCompliant(false)] public CBORObject WithTag(ulong tag) { - return FromObjectAndTag(this, UInt64ToEInteger(tag)); + return FromCBORObjectAndTag(this, UInt64ToEInteger(tag)); } /// Generates a CBOR object from an arbitrary object and gives /// the resulting object a tag. /// The parameter 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, and whenever possible, the application - /// should limit this parameter's inputs to types specially handled by - /// this method (such as int or String ) 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.. + /// CBORObject. /// 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 @@ -329,13 +320,13 @@ public CBORObject WithTag(ulong tag) { /// registry maintained by the Internet Assigned Numbers Authority( /// iana.org/assignments/cbor-tags ). /// A CBOR object where the object is - /// converted to a CBOR object and given the tag + /// given the tag /// . If "valueOb" is null, returns a version of CBORObject.Null with /// the given tag. [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)); } /// diff --git a/CBOR/PeterO/Cbor/CBORReader.cs b/CBOR/PeterO/Cbor/CBORReader.cs index c5e0eeb8..843c1d70 100644 --- a/CBOR/PeterO/Cbor/CBORReader.cs +++ b/CBOR/PeterO/Cbor/CBORReader.cs @@ -67,7 +67,7 @@ private CBORObject ObjectFromByteArray(byte[] data, int lengthHint) { } private CBORObject ObjectFromUtf8Array(byte[] data, int lengthHint) { - CBORObject cbor = data.Length == 0 ? CBORObject.FromObject(String.Empty) : + CBORObject cbor = data.Length == 0 ? CBORObject.FromString(String.Empty) : CBORObject.FromRawUtf8(data); this.stringRefs?.AddStringIfNeeded(cbor, lengthHint); return cbor; @@ -495,7 +495,7 @@ public CBORObject ReadForFirstByte(int firstbyte) { newFirstByte) : this.ReadInternal(); --this.depth; if ((uadditional >> 63) != 0) { - return CBORObject.FromObjectAndTag(o, + return CBORObject.FromCBORObjectAndTag(o, ToUnsignedEInteger(uadditional)); } if (uadditional < 65536) { @@ -516,11 +516,11 @@ public CBORObject ReadForFirstByte(int firstbyte) { return this.stringRefs.GetString(o.AsEIntegerValue()); } } - return CBORObject.FromObjectAndTag( + return CBORObject.FromCBORObjectAndTag( o, (int)uadditional); } - return CBORObject.FromObjectAndTag( + return CBORObject.FromCBORObjectAndTag( o, (EInteger)uadditional); } diff --git a/CBOR/PeterO/Cbor/CBORUriConverter.cs b/CBOR/PeterO/Cbor/CBORUriConverter.cs index 7e962883..585ea4e4 100644 --- a/CBOR/PeterO/Cbor/CBORUriConverter.cs +++ b/CBOR/PeterO/Cbor/CBORUriConverter.cs @@ -61,7 +61,7 @@ public CBORObject ToCBORObject(Uri uri) { if (!URIUtility.HasScheme(uriString)) { tag = 267; } - return CBORObject.FromObjectAndTag(uriString, tag); + return CBORObject.FromString(uriString).WithTag(tag); } } } diff --git a/CBOR/PeterO/Cbor/CBORUuidConverter.cs b/CBOR/PeterO/Cbor/CBORUuidConverter.cs index e36148f7..55eb9668 100644 --- a/CBOR/PeterO/Cbor/CBORUuidConverter.cs +++ b/CBOR/PeterO/Cbor/CBORUuidConverter.cs @@ -26,7 +26,7 @@ private static CBORObject ValidateObject(CBORObject obj) { /// A CBORObject object. public CBORObject ToCBORObject(Guid obj) { byte[] bytes = PropertyMap.UUIDToBytes(obj); - return CBORObject.FromObjectAndTag(bytes, 37); + return CBORObject.FromByteArray(bytes).WithTag(37); } public Guid FromCBORObject(CBORObject obj) { diff --git a/CBOR/PeterO/Cbor/JSONPatch.cs b/CBOR/PeterO/Cbor/JSONPatch.cs index 7d04f3a8..f2f6e32b 100644 --- a/CBOR/PeterO/Cbor/JSONPatch.cs +++ b/CBOR/PeterO/Cbor/JSONPatch.cs @@ -7,9 +7,11 @@ licensed under Creative Commons Zero (CC0): */ using System; +using System.Diagnostics.CodeAnalysis; namespace PeterO.Cbor { internal static class JSONPatch { + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. private static CBORObject AddOperation( CBORObject o, string valueOpStr, @@ -54,6 +56,7 @@ private static CBORObject CloneCbor(CBORObject o) { } } + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. private static string GetString(CBORObject o, string str) { #if DEBUG if (o == null) { @@ -70,6 +73,7 @@ private static string GetString(CBORObject o, string str) { "\u0020text string type") : co.AsString(); } + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. public static CBORObject Patch(CBORObject o, CBORObject ptch) { // clone the object in case of failure if (o == null) { @@ -201,6 +205,7 @@ public static CBORObject Patch(CBORObject o, CBORObject ptch) { return o ?? CBORObject.Null; } + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. private static CBORObject RemoveOperation( CBORObject o, string valueOpStr, @@ -226,6 +231,7 @@ private static CBORObject RemoveOperation( } } + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. private static CBORObject ReplaceOperation( CBORObject o, string valueOpStr, diff --git a/CBOR/PeterO/Cbor/JSONPointer.cs b/CBOR/PeterO/Cbor/JSONPointer.cs index 5e8af94b..b66ce6f8 100644 --- a/CBOR/PeterO/Cbor/JSONPointer.cs +++ b/CBOR/PeterO/Cbor/JSONPointer.cs @@ -9,6 +9,7 @@ licensed under Creative Commons Zero (CC0): using System; using System.Collections.Generic; using System.Text; +using System.Diagnostics.CodeAnalysis; using PeterO.Numbers; namespace PeterO.Cbor { @@ -17,6 +18,7 @@ internal sealed class JSONPointer { private readonly bool isRoot; private readonly CBORObject jsonobj; + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. public static JSONPointer FromPointer(CBORObject obj, string pointer) { var index = 0; if (pointer == null) { @@ -134,7 +136,7 @@ public static JSONPointer FromPointer(CBORObject obj, string pointer) { } } } - + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. public static CBORObject GetObject( CBORObject obj, string pointer, @@ -269,6 +271,7 @@ public CBORObject GetParent() { return this.jsonobj; } + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. public CBORObject GetValue() { if (this.isRoot) { // Root always exists @@ -404,7 +407,7 @@ private static void GetPointersWithKey( // and remove the key from the object // if necessary if (remove) { - _ = rootObj.Remove(CBORObject.FromObject(keyToFind)); + _ = rootObj.Remove(CBORObject.FromString(keyToFind)); } } // Search the key's values diff --git a/CBOR/PeterO/Cbor/StringRefs.cs b/CBOR/PeterO/Cbor/StringRefs.cs index 243ce4de..e8106bce 100644 --- a/CBOR/PeterO/Cbor/StringRefs.cs +++ b/CBOR/PeterO/Cbor/StringRefs.cs @@ -87,7 +87,7 @@ public CBORObject GetString(long smallIndex) { CBORObject ret = lastList[index]; // Byte strings are mutable, so make a copy return (ret.Type == CBORType.ByteString) ? - CBORObject.FromObject(ret.GetByteString()) : ret; + CBORObject.FromByteArray(ret.GetByteString()) : ret; } public CBORObject GetString(EInteger bigIndex) { @@ -106,7 +106,7 @@ public CBORObject GetString(EInteger bigIndex) { CBORObject ret = lastList[index]; // Byte strings are mutable, so make a copy return (ret.Type == CBORType.ByteString) ? - CBORObject.FromObject(ret.GetByteString()) : ret; + CBORObject.FromByteArray(ret.GetByteString()) : ret; } } } diff --git a/CBOR/PeterO/DebugUtility.cs b/CBOR/PeterO/DebugUtility.cs index c7c031bf..1e67d212 100644 --- a/CBOR/PeterO/DebugUtility.cs +++ b/CBOR/PeterO/DebugUtility.cs @@ -38,6 +38,7 @@ private static MethodInfo GetTypeMethod( #endif } + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static void Log(string str) { var type = Type.GetType("System.Console"); if (type == null) { @@ -73,6 +74,7 @@ public static void Log(string str) { } [System.Diagnostics.Conditional("DEBUG")] + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static void Log(string format, params object[] args) { Log(String.Format( System.Globalization.CultureInfo.CurrentCulture, diff --git a/TrimTester/Program.cs b/TrimTester/Program.cs deleted file mode 100644 index a491f431..00000000 --- a/TrimTester/Program.cs +++ /dev/null @@ -1,4 +0,0 @@ -using System; - -// See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); \ No newline at end of file diff --git a/TrimTester/TrimTester.csproj b/TrimTester/TrimTester.csproj deleted file mode 100644 index e98516d2..00000000 --- a/TrimTester/TrimTester.csproj +++ /dev/null @@ -1,16 +0,0 @@ - - - Exe - net7.0 - true - - link - - - - - - - - - From 93d430bdc1f87a6679268c2a3d85d72fcc77a8cc Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Tue, 10 Oct 2023 19:17:52 +0100 Subject: [PATCH 12/29] fix build --- CBOR/PeterO/Cbor/CBORObject.cs | 86 +++++++++++++++++++++++++++------- CBOR/docs.xml | 18 +++---- CBORTest/CBORObjectTest.cs | 70 +++++++++++++-------------- CBORTest/CBORTest.cs | 80 +++++++++++++++---------------- CBORTest/CBORTestCommon.cs | 6 +-- CBORTest/ToObjectTest.cs | 34 +++++++------- 6 files changed, 172 insertions(+), 122 deletions(-) diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index 187534c3..634b5132 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -555,7 +555,7 @@ public CBORObject this[int index] /// 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. - /// An arbitrary object. If this is a CBOR map, this + /// An arbitrary CBORObject. 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 @@ -569,26 +569,76 @@ public CBORObject this[int index] /// or map. If this is a CBOR map, returns null (not /// CBORObject.Null ) if an item with the given key doesn't /// exist. - [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] - 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. + public CBORObject GetOrDefault(CBORObject cborkey, CBORObject defaultValue) { if (this.Type == CBORType.Array) { - int index; - if (key is int) { - index = (int)key; - } else { - var cborkey = CBORObject.FromObject(key); - if (!cborkey.IsNumber || !cborkey.AsNumber().CanFitInInt32()) { + if (!cborkey.IsNumber || !cborkey.AsNumber().CanFitInInt32()) { return defaultValue; - } - index = cborkey.AsNumber().ToInt32Checked(); } + int index = cborkey.AsNumber().ToInt32Checked(); IList list = this.AsList(); return (index < 0 || index >= list.Count) ? defaultValue : list[index]; } if (this.Type == CBORType.Map) { IDictionary map = this.AsMap(); - var ckey = CBORObject.FromObject(key); + var ckey = cborkey; + return PropertyMap.GetOrDefault(map, ckey, defaultValue); + } + return defaultValue; + } + + /// Gets the value of a CBOR object by integer index in this + /// array, or a default value if that + /// value is not found. + /// 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. + /// 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. + /// 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. + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] + public CBORObject GetOrDefault(int key, CBORObject defaultValue) { + if (this.Type == CBORType.Array) { + int index = key; + IList list = this.AsList(); + return (index < 0 || index >= list.Count) ? defaultValue : + list[index]; + } + if (this.Type == CBORType.Map) { + IDictionary map = this.AsMap(); + var ckey = FromInt(key); + return PropertyMap.GetOrDefault(map, ckey, defaultValue); + } + return defaultValue; + } + + /// Gets the value of a CBOR object by string key in a map, or a default value if that + /// value is not found. + /// An arbitrary string. 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, defaultValue is returned. + /// A value to return if an item with the + /// given key doesn't exist, or if the CBOR object is an array. + /// 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 CBORObject GetOrDefault(string key, CBORObject defaultValue) { + if (this.Type == CBORType.Array) { + return defaultValue; + } + if (this.Type == CBORType.Map) { + IDictionary map = this.AsMap(); + var ckey = FromString(key); return PropertyMap.GetOrDefault(map, ckey, defaultValue); } return defaultValue; @@ -4110,7 +4160,7 @@ public static void WriteJSON(object obj, Stream outputStream) { /// 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 + /// CBORObject.FromCBORObjectAndTag method and pass the CBOR object /// and the desired tag number to that method. /// An object representing the key, which will be /// converted to a CBORObject. Can be null, in which case this value is @@ -4163,7 +4213,7 @@ public CBORObject Add(object key, object valueOb) { /// 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 + /// CBORObject.FromCBORObjectAndTag /// method and pass the CBOR object /// and the desired tag number to that method. /// @@ -4178,7 +4228,7 @@ public CBORObject Add(object key, object valueOb) { /// 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)); + /// string")) .Add(CBORObject.FromCBORObjectAndTag(9999, 1)); /// . /// public CBORObject Add(CBORObject obj) { @@ -4194,7 +4244,7 @@ public CBORObject Add(CBORObject obj) { /// 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 + /// with a given tag, call the CBORObject.FromCBORObjectAndTag /// method and pass the CBOR object and the desired tag number to that /// method. /// @@ -4210,7 +4260,7 @@ public CBORObject Add(CBORObject obj) { /// 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)); + /// .Add("text string") .Add(CBORObject.FromCBORObjectAndTag(9999, 1)); /// . /// [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] @@ -5617,7 +5667,7 @@ public string ToJSONString() { /// 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 + /// tag, call the CBORObject.FromCBORObjectAndTag /// method and pass /// the CBOR object and the desired tag number to that method.) /// Rational numbers will be converted to their exact form, if diff --git a/CBOR/docs.xml b/CBOR/docs.xml index 2854cd84..d725435c 100644 --- a/CBOR/docs.xml +++ b/CBOR/docs.xml @@ -1904,7 +1904,7 @@ 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 + CBORObject.FromCBORObjectAndTag method and pass the CBOR object and the desired tag number to that method. The parameter is a CBOR @@ -1917,7 +1917,7 @@ 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)); . + string")) .Add(CBORObject.FromCBORObjectAndTag(9999, 1)); . @@ -1927,7 +1927,7 @@ 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 + with a given tag, call the CBORObject.FromCBORObjectAndTag method and pass the CBOR object and the desired tag number to that method. A CBOR object (or an object convertible to a CBOR @@ -1940,7 +1940,7 @@ 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)); . + .Add("text string") .Add(CBORObject.FromCBORObjectAndTag(9999, 1)); . @@ -1950,7 +1950,7 @@ 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 + CBORObject.FromCBORObjectAndTag method and pass the CBOR object and the desired tag number to that method. An object representing the key, which will be converted to a CBORObject. Can be null, in which case this value is @@ -3871,7 +3871,7 @@ A CBORObject object. - + Generates a CBOR object from an arbitrary object and gives the resulting object a tag in addition to its existing tags (the @@ -3901,7 +3901,7 @@ The parameter is null. - + Generates a CBOR object from an arbitrary object and gives the resulting object a tag in addition to its existing tags (the @@ -3929,7 +3929,7 @@ The parameter is less than 0. - + Generates a CBOR object from an arbitrary object and gives the resulting object a tag. @@ -4770,7 +4770,7 @@ 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 + tag, call the CBORObject.FromCBORObjectAndTag 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 diff --git a/CBORTest/CBORObjectTest.cs b/CBORTest/CBORObjectTest.cs index f81add90..2a1eac96 100644 --- a/CBORTest/CBORObjectTest.cs +++ b/CBORTest/CBORObjectTest.cs @@ -2913,7 +2913,7 @@ public void TestFromJSONString() { [Test] public void TestTagArray() { - var obj = CBORObject.FromObjectAndTag("test", 999); + var obj = CBORObject.FromCBORObjectAndTag(CBORObject.FromString("test"), 999); EInteger[] etags = obj.GetAllTags(); Assert.AreEqual(1, etags.Length); Assert.AreEqual(999, etags[0].ToInt32Checked()); @@ -3204,8 +3204,8 @@ public PODClass PropValue { public void TestBase64Extras() { // Base64 tests CBORObject o; - o = CBORObject.FromObjectAndTag( - new byte[] { 0x9a, 0xd6, 0xf0, 0xe8 }, + o = CBORObject.FromCBORObjectAndTag( + CBORObject.FromByteArray(new byte[] { 0x9a, 0xd6, 0xf0, 0xe8 }), 23); { string stringTemp = o.ToJSONString(); @@ -3224,8 +3224,8 @@ public void TestBase64Extras() { "\"mtb_6A\"", stringTemp); } - o = CBORObject.FromObjectAndTag( - new byte[] { 0x9a, 0xd6, 0xff, 0xe8 }, + o = CBORObject.FromCBORObjectAndTag( + CBORObject.FromByteArray(new byte[] { 0x9a, 0xd6, 0xff, 0xe8 }), 22); // Encode with Base64 { @@ -3246,8 +3246,8 @@ public void TestBase64Extras() { "\"mtb_6A\"", stringTemp); } - o = CBORObject.FromObjectAndTag( - new byte[] { 0x9a, 0xd6, 0xff, 0xe8 }, + o = CBORObject.FromCBORObjectAndTag( + CBORObject.FromByteArray(new byte[] { 0x9a, 0xd6, 0xff, 0xe8 }), 22); // Encode with Base64 { @@ -3348,7 +3348,7 @@ public void TestFromObject_PODOptions() { public void TestFromObjectAndTag() { var bigvalue = EInteger.FromString("99999999999999999999999999999"); try { - _ = CBORObject.FromObjectAndTag(2, bigvalue); + _ = CBORObject.FromCBORObjectAndTag(CBORObject.FromInt(2), bigvalue); Assert.Fail("Should have failed"); } catch (ArgumentException) { // NOTE: Intentionally empty @@ -3357,7 +3357,7 @@ public void TestFromObjectAndTag() { throw new InvalidOperationException(String.Empty, ex); } try { - _ = CBORObject.FromObjectAndTag(2, -1); + _ = CBORObject.FromCBORObjectAndTag(CBORObject.FromInt(2), -1); Assert.Fail("Should have failed"); } catch (ArgumentException) { // NOTE: Intentionally empty @@ -3366,7 +3366,7 @@ public void TestFromObjectAndTag() { throw new InvalidOperationException(String.Empty, ex); } try { - _ = CBORObject.FromObjectAndTag(CBORObject.Null, -1); + _ = CBORObject.FromCBORObjectAndTag(CBORObject.Null, -1); Assert.Fail("Should have failed"); } catch (ArgumentException) { // NOTE: Intentionally empty @@ -3375,14 +3375,14 @@ public void TestFromObjectAndTag() { throw new InvalidOperationException(String.Empty, ex); } try { - _ = CBORObject.FromObjectAndTag(CBORObject.Null, 999999); + _ = CBORObject.FromCBORObjectAndTag(CBORObject.Null, 999999); } catch (Exception ex) { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } EInteger eintNull = null; try { - _ = CBORObject.FromObjectAndTag(2, eintNull); + _ = CBORObject.FromCBORObjectAndTag(CBORObject.FromInt(2), eintNull); Assert.Fail("Should have failed"); } catch (ArgumentNullException) { // NOTE: Intentionally empty @@ -3391,7 +3391,7 @@ public void TestFromObjectAndTag() { throw new InvalidOperationException(String.Empty, ex); } try { - _ = CBORObject.FromObjectAndTag(2, EInteger.FromString("-1")); + _ = CBORObject.FromCBORObjectAndTag(CBORObject.FromInt(2), EInteger.FromString("-1")); Assert.Fail("Should have failed"); } catch (ArgumentException) { // NOTE: Intentionally empty @@ -4243,8 +4243,8 @@ public void TestOperatorSubtraction() { [Test] public void TestMostOuterTag() { - _ = CBORObject.FromObjectAndTag(CBORObject.True, 999); - var cbor = CBORObject.FromObjectAndTag(CBORObject.True, 1000); + _ = CBORObject.FromCBORObjectAndTag(CBORObject.True, 999); + var cbor = CBORObject.FromCBORObjectAndTag(CBORObject.True, 1000); Assert.AreEqual(EInteger.FromString("1000"), cbor.MostOuterTag); cbor = CBORObject.True; Assert.AreEqual(EInteger.FromString("-1"), cbor.MostOuterTag); @@ -6287,7 +6287,7 @@ public void TestToJSONString_DuplicateKeys() { throw new InvalidOperationException(String.Empty, ex); } cbor = CBORObject.NewMap().Add("9999-01-01T00:00:00Z", 1) - .Add(CBORObject.FromObjectAndTag("9999-01-01T00:00:00Z", 0), 1); + .Add(CBORObject.FromCBORObjectAndTag(CBORObject.FromString("9999-01-01T00:00:00Z"), 0), 1); try { _ = cbor.ToJSONString(); Assert.Fail("Should have failed"); @@ -6409,8 +6409,8 @@ public void TestToFloatingPointBitsSingle() { public void TestToJSONString_ByteArray_Padding() { CBORObject o; var options = new JSONOptions(String.Empty); - o = CBORObject.FromObjectAndTag( - new byte[] { 0x9a, 0xd6, 0xf0, 0xe8 }, 22); + o = CBORObject.FromCBORObjectAndTag( + CBORObject.FromByteArray(new byte[] { 0x9a, 0xd6, 0xf0, 0xe8 }), 22); { string stringTemp = o.ToJSONString(options); Assert.AreEqual( @@ -6429,8 +6429,8 @@ public void TestToJSONString_ByteArray_Padding() { stringTemp); } // tagged 23, so base16 - o = CBORObject.FromObjectAndTag( - new byte[] { 0x9a, 0xd6, 0xf0, 0xe8 }, + o = CBORObject.FromCBORObjectAndTag( + CBORObject.FromByteArray(new byte[] { 0x9a, 0xd6, 0xf0, 0xe8 }), 23); { string stringTemp = o.ToJSONString(options); @@ -6646,7 +6646,7 @@ public void TestType() { Assert.AreEqual( CBORType.Boolean, cbor.Type); - cbor = CBORObject.FromObjectAndTag(CBORObject.True, 999); + cbor = CBORObject.FromCBORObjectAndTag(CBORObject.True, 999); Assert.AreEqual( CBORType.Boolean, cbor.Type); @@ -6670,7 +6670,7 @@ public void TestType() { [Test] public void TestUntag() { - var o = CBORObject.FromObjectAndTag("test", 999); + var o = CBORObject.FromCBORObjectAndTag(CBORObject.FromString("test"), 999); Assert.AreEqual(EInteger.FromString("999"), o.MostInnerTag); o = o.Untag(); Assert.AreEqual(EInteger.FromString("-1"), o.MostInnerTag); @@ -8766,7 +8766,7 @@ public void TestDateTime() { dateList.Add(dtstr); } foreach (string dtstr in dateList) { - var cbor = CBORObject.FromObjectAndTag(dtstr, 0); + var cbor = CBORObject.FromCBORObjectAndTag(CBORObject.FromString(dtstr), 0); var dt = (DateTime)cbor.ToObject(typeof(DateTime)); _ = ToObjectTest.TestToFromObjectRoundTrip(dt); } @@ -8778,10 +8778,10 @@ public static void TestDateTimeTag1One(string str, long timeValue) { public static void TestDateTimeTag1One(string str, EInteger ei) { CBORObject cbornum; - cbornum = CBORObject.FromObjectAndTag(str, 0); + cbornum = CBORObject.FromCBORObjectAndTag(CBORObject.FromString(str), 0); var dtx = (DateTime)cbornum.ToObject(typeof(DateTime)); _ = ToObjectTest.TestToFromObjectRoundTrip(dtx); - cbornum = CBORObject.FromObjectAndTag(ei, 1); + cbornum = CBORObject.FromCBORObjectAndTag(CBORObject.FromEInteger(ei), 1); var dtx2 = (DateTime)cbornum.ToObject(typeof(DateTime)); _ = ToObjectTest.TestToFromObjectRoundTrip(dtx2); TestCommon.AssertEqualsHashCode(dtx, dtx2); @@ -8789,7 +8789,7 @@ public static void TestDateTimeTag1One(string str, EInteger ei) { throw new ArgumentNullException(nameof(ei)); } if (ei.CanFitInInt64()) { - cbornum = CBORObject.FromObjectAndTag(ei.ToInt64Checked(), 1); + cbornum = CBORObject.FromCBORObjectAndTag(CBORObject.FromInt64(ei.ToInt64Checked()), 1); dtx2 = (DateTime)cbornum.ToObject(typeof(DateTime)); TestCommon.AssertEqualsHashCode(dtx, dtx2); _ = ToObjectTest.TestToFromObjectRoundTrip(dtx2); @@ -8797,11 +8797,11 @@ public static void TestDateTimeTag1One(string str, EInteger ei) { EFloat ef1 = EFloat.FromEInteger(ei).Plus(EContext.Binary64); var ef2 = EFloat.FromEInteger(ei); if (ef1.CompareTo(ef2) == 0) { - cbornum = CBORObject.FromObjectAndTag(ef1, 1); + cbornum = CBORObject.FromCBORObjectAndTag(CBORObject.FromObject(ef1), 1); dtx2 = (DateTime)cbornum.ToObject(typeof(DateTime)); TestCommon.AssertEqualsHashCode(dtx, dtx2); _ = ToObjectTest.TestToFromObjectRoundTrip(dtx2); - cbornum = CBORObject.FromObjectAndTag(ef1.ToDouble(), 1); + cbornum = CBORObject.FromCBORObjectAndTag(CBORObject.FromObject(ef1.ToDouble()), 1); dtx2 = (DateTime)cbornum.ToObject(typeof(DateTime)); TestCommon.AssertEqualsHashCode(dtx, dtx2); _ = ToObjectTest.TestToFromObjectRoundTrip(dtx2); @@ -8810,10 +8810,10 @@ public static void TestDateTimeTag1One(string str, EInteger ei) { public static void TestDateTimeTag1One(string str, double dbl) { CBORObject cbornum; - cbornum = CBORObject.FromObjectAndTag(str, 0); + cbornum = CBORObject.FromCBORObjectAndTag(CBORObject.FromString(str), 0); var dtx = (DateTime)cbornum.ToObject(typeof(DateTime)); _ = ToObjectTest.TestToFromObjectRoundTrip(dtx); - cbornum = CBORObject.FromObjectAndTag(dbl, 1); + cbornum = CBORObject.FromCBORObjectAndTag(CBORObject.FromObject(dbl), 1); var dtx2 = (DateTime)cbornum.ToObject(typeof(DateTime)); _ = ToObjectTest.TestToFromObjectRoundTrip(dtx2); TestCommon.AssertEqualsHashCode(dtx, dtx2); @@ -8824,7 +8824,7 @@ public static void TestDateTimeTag1One(string str, double dbl) { public void TestDateTimeTag1Specific1() { // Test speed var ei = EInteger.FromString("-14261178672295354872"); - var cbornum = CBORObject.FromObjectAndTag(ei, 1); + var cbornum = CBORObject.FromCBORObjectAndTag(CBORObject.FromEInteger(ei), 1); try { var dtx = (DateTime)cbornum.ToObject(typeof(DateTime)); _ = ToObjectTest.TestToFromObjectRoundTrip(dtx); @@ -9125,7 +9125,7 @@ public void TestDateTimeTag1() { DateTime dt, dt2; for (int i = 0; i < 1000; ++i) { EInteger ei = CBORTestCommon.RandomEIntegerMajorType0Or1(rg); - cbornum = CBORObject.FromObjectAndTag(ei, 1); + cbornum = CBORObject.FromCBORObjectAndTag(CBORObject.FromEInteger(ei), 1); try { var dtx = (DateTime)cbornum.ToObject(typeof(DateTime)); _ = ToObjectTest.TestToFromObjectRoundTrip(dtx); @@ -9135,7 +9135,7 @@ public void TestDateTimeTag1() { } for (int i = 0; i < 1000; ++i) { double dbl = RandomObjects.RandomFiniteDouble(rg); - cbornum = CBORObject.FromObjectAndTag(dbl, 1); + cbornum = CBORObject.FromCBORObjectAndTag(CBORObject.FromObject(dbl), 1); try { var dtx = (DateTime)cbornum.ToObject(typeof(DateTime)); _ = ToObjectTest.TestToFromObjectRoundTrip(dtx); @@ -9144,9 +9144,9 @@ public void TestDateTimeTag1() { } } string dateStr = "1970-01-01T00:00:00.000Z"; - var cbor = CBORObject.FromObjectAndTag(dateStr, 0); + var cbor = CBORObject.FromCBORObjectAndTag(CBORObject.FromString(dateStr), 0); dt = (DateTime)cbor.ToObject(typeof(DateTime)); - _ = CBORObject.FromObjectAndTag(0, 1); + _ = CBORObject.FromCBORObjectAndTag(CBORObject.FromInt(0), 1); dt2 = (DateTime)cbor.ToObject(typeof(DateTime)); Assert.AreEqual(dt2, dt); } diff --git a/CBORTest/CBORTest.cs b/CBORTest/CBORTest.cs index 89a5deaa..3fb27f21 100644 --- a/CBORTest/CBORTest.cs +++ b/CBORTest/CBORTest.cs @@ -1321,23 +1321,23 @@ public void TestCompareB() { }); AddSubCompare(objectTemp, objectTemp2); } - var cbor = CBORObject.FromObjectAndTag( - double.NegativeInfinity, + var cbor = CBORObject.FromCBORObjectAndTag( + CBORObject.FromObject(double.NegativeInfinity), 1956611); CBORTestCommon.AssertRoundTrip(cbor); - cbor = CBORObject.FromObjectAndTag( + cbor = CBORObject.FromCBORObjectAndTag( ToObjectTest.TestToFromObjectRoundTrip(double.NegativeInfinity), 1956611); CBORTestCommon.AssertRoundTrip(cbor); - cbor = CBORObject.FromObjectAndTag( + cbor = CBORObject.FromCBORObjectAndTag( ToObjectTest.TestToFromObjectRoundTrip(CBORTestCommon.FloatNegInf), 1956611); CBORTestCommon.AssertRoundTrip(cbor); - cbor = CBORObject.FromObjectAndTag( + cbor = CBORObject.FromCBORObjectAndTag( ToObjectTest.TestToFromObjectRoundTrip(CBORTestCommon.DecNegInf), 1956611); CBORTestCommon.AssertRoundTrip(cbor); - cbor = CBORObject.FromObjectAndTag( + cbor = CBORObject.FromCBORObjectAndTag( ToObjectTest.TestToFromObjectRoundTrip(CBORTestCommon.FloatNegInf), 1956611); CBORTestCommon.AssertRoundTrip(cbor); @@ -1656,10 +1656,10 @@ public void TestTag268() { CBORObject cbortag; for (int tag = 268; tag <= 269; ++tag) { cbor = CBORObject.NewArray().Add(-3).Add(99999).Add(0); - cbortag = CBORObject.FromObjectAndTag(cbor, tag); + cbortag = CBORObject.FromCBORObjectAndTag(cbor, tag); Assert.IsFalse(cbortag.AsNumber().IsNegative()); cbor = CBORObject.NewArray().Add(-3).Add(99999); - cbortag = CBORObject.FromObjectAndTag(cbor, tag); + cbortag = CBORObject.FromCBORObjectAndTag(cbor, tag); try { Console.WriteLine(String.Empty + cbortag.ToObject(typeof(EDecimal))); Assert.Fail("Should have failed " + cbortag.ToString()); @@ -1670,10 +1670,10 @@ public void TestTag268() { throw new InvalidOperationException(String.Empty, ex); } cbor = CBORObject.NewArray().Add(-3).Add(99999).Add(1); - cbortag = CBORObject.FromObjectAndTag(cbor, tag); + cbortag = CBORObject.FromCBORObjectAndTag(cbor, tag); Assert.IsTrue(cbortag.AsNumber().IsNegative()); cbor = CBORObject.NewArray().Add(-3).Add(99999).Add(-1); - cbortag = CBORObject.FromObjectAndTag(cbor, tag); + cbortag = CBORObject.FromCBORObjectAndTag(cbor, tag); try { Console.WriteLine(String.Empty + cbortag.ToObject(typeof(EDecimal))); Assert.Fail("Should have failed " + cbortag.ToString()); @@ -1684,7 +1684,7 @@ public void TestTag268() { throw new InvalidOperationException(String.Empty, ex); } cbor = CBORObject.NewArray().Add(-3).Add(99999).Add(2); - cbortag = CBORObject.FromObjectAndTag(cbor, tag); + cbortag = CBORObject.FromCBORObjectAndTag(cbor, tag); try { Console.WriteLine(String.Empty + cbortag.ToObject(typeof(EDecimal))); Assert.Fail("Should have failed " + cbortag.ToString()); @@ -1695,13 +1695,13 @@ public void TestTag268() { throw new InvalidOperationException(String.Empty, ex); } cbor = CBORObject.NewArray().Add(0).Add(0).Add(2); - cbortag = CBORObject.FromObjectAndTag(cbor, tag); + cbortag = CBORObject.FromCBORObjectAndTag(cbor, tag); Assert.IsFalse(cbortag.AsNumber().IsNegative()); cbor = CBORObject.NewArray().Add(0).Add(0).Add(3); - cbortag = CBORObject.FromObjectAndTag(cbor, tag); + cbortag = CBORObject.FromCBORObjectAndTag(cbor, tag); Assert.IsTrue(cbortag.AsNumber().IsNegative()); cbor = CBORObject.NewArray().Add(-3).Add(99999).Add(8); - cbortag = CBORObject.FromObjectAndTag(cbor, tag); + cbortag = CBORObject.FromCBORObjectAndTag(cbor, tag); try { Console.WriteLine(String.Empty + cbortag.ToObject(typeof(EDecimal))); Assert.Fail("Should have failed " + cbortag.ToString()); @@ -1765,7 +1765,7 @@ public void TestPlist() { CBORObject o; o = CBORObject.FromJSONString("[1,2,null,true,false,\"\"]"); _ = o.Add(new byte[] { 32, 33, 44, 55 }); - _ = o.Add(CBORObject.FromObjectAndTag(9999, 1)); + _ = o.Add(CBORObject.FromCBORObjectAndTag(CBORObject.FromInt(9999), 1)); Console.WriteLine(o.ToJSONString()); Console.WriteLine(CBORPlistWriter.ToPlistString(o)); } @@ -2087,16 +2087,16 @@ public static CBORObject ReferenceTestObject(int nests) { CBORObject arr = CBORObject.NewArray().Add("xxx").Add("yyy"); _ = arr.Add("zzz") .Add("wwww").Add("iiiiiii").Add("aaa").Add("bbb").Add("ccc"); - arr = CBORObject.FromObjectAndTag(arr, 28); + arr = CBORObject.FromCBORObjectAndTag(arr, 28); _ = root.Add(arr); CBORObject refobj; for (int i = 0; i <= nests; ++i) { - refobj = CBORObject.FromObjectAndTag(i, 29); + refobj = CBORObject.FromCBORObjectAndTag(CBORObject.FromInt(i), 29); arr = CBORObject.FromObject(new CBORObject[] { refobj, refobj, refobj, refobj, refobj, refobj, refobj, refobj, refobj, }); - arr = CBORObject.FromObjectAndTag(arr, 28); + arr = CBORObject.FromCBORObjectAndTag(arr, 28); _ = root.Add(arr); } return root; @@ -2952,62 +2952,62 @@ public void TestTaggedUntagged() { for (int i = 200; i < 1000; ++i) { CBORObject o, o2; o = ToObjectTest.TestToFromObjectRoundTrip(0); - o2 = CBORObject.FromObjectAndTag(o, i); + o2 = CBORObject.FromCBORObjectAndTag(o, i); TestCommon.AssertEqualsHashCode(o, o2); - o = CBORObject.FromObjectAndTag(o, i + 1); + o = CBORObject.FromCBORObjectAndTag(o, i + 1); TestCommon.AssertEqualsHashCode(o, o2); o = ToObjectTest.TestToFromObjectRoundTrip(EInteger.FromString( "999999999999999999999999999999999")); - o2 = CBORObject.FromObjectAndTag(o, i); + o2 = CBORObject.FromCBORObjectAndTag(o, i); TestCommon.AssertEqualsHashCode(o, o2); - o = CBORObject.FromObjectAndTag(o, i + 1); + o = CBORObject.FromCBORObjectAndTag(o, i + 1); TestCommon.AssertEqualsHashCode(o, o2); o = ToObjectTest.TestToFromObjectRoundTrip(new byte[] { 1, 2, 3 }); - o2 = CBORObject.FromObjectAndTag(o, i); + o2 = CBORObject.FromCBORObjectAndTag(o, i); TestCommon.AssertEqualsHashCode(o, o2); - o = CBORObject.FromObjectAndTag(o, i + 1); + o = CBORObject.FromCBORObjectAndTag(o, i + 1); TestCommon.AssertEqualsHashCode(o, o2); o = CBORObject.NewArray(); _ = o.Add(ToObjectTest.TestToFromObjectRoundTrip(0)); _ = o.Add(ToObjectTest.TestToFromObjectRoundTrip(1)); _ = o.Add(ToObjectTest.TestToFromObjectRoundTrip(2)); - o2 = CBORObject.FromObjectAndTag(o, i); + o2 = CBORObject.FromCBORObjectAndTag(o, i); TestCommon.AssertEqualsHashCode(o, o2); - o = CBORObject.FromObjectAndTag(o, i + 1); + o = CBORObject.FromCBORObjectAndTag(o, i + 1); TestCommon.AssertEqualsHashCode(o, o2); o = CBORObject.NewMap(); _ = o.Add("a", ToObjectTest.TestToFromObjectRoundTrip(0)); _ = o.Add("b", ToObjectTest.TestToFromObjectRoundTrip(1)); _ = o.Add("c", ToObjectTest.TestToFromObjectRoundTrip(2)); - o2 = CBORObject.FromObjectAndTag(o, i); + o2 = CBORObject.FromCBORObjectAndTag(o, i); TestCommon.AssertEqualsHashCode(o, o2); - o = CBORObject.FromObjectAndTag(o, i + 1); + o = CBORObject.FromCBORObjectAndTag(o, i + 1); TestCommon.AssertEqualsHashCode(o, o2); o = ToObjectTest.TestToFromObjectRoundTrip("a"); - o2 = CBORObject.FromObjectAndTag(o, i); + o2 = CBORObject.FromCBORObjectAndTag(o, i); TestCommon.AssertEqualsHashCode(o, o2); - o = CBORObject.FromObjectAndTag(o, i + 1); + o = CBORObject.FromCBORObjectAndTag(o, i + 1); TestCommon.AssertEqualsHashCode(o, o2); o = CBORObject.False; - o2 = CBORObject.FromObjectAndTag(o, i); + o2 = CBORObject.FromCBORObjectAndTag(o, i); TestCommon.AssertEqualsHashCode(o, o2); - o = CBORObject.FromObjectAndTag(o, i + 1); + o = CBORObject.FromCBORObjectAndTag(o, i + 1); TestCommon.AssertEqualsHashCode(o, o2); o = CBORObject.True; - o2 = CBORObject.FromObjectAndTag(o, i); + o2 = CBORObject.FromCBORObjectAndTag(o, i); TestCommon.AssertEqualsHashCode(o, o2); - o = CBORObject.FromObjectAndTag(o, i + 1); + o = CBORObject.FromCBORObjectAndTag(o, i + 1); TestCommon.AssertEqualsHashCode(o, o2); o = CBORObject.Null; - o2 = CBORObject.FromObjectAndTag(o, i); + o2 = CBORObject.FromCBORObjectAndTag(o, i); TestCommon.AssertEqualsHashCode(o, o2); - o = CBORObject.FromObjectAndTag(o, i + 1); + o = CBORObject.FromCBORObjectAndTag(o, i + 1); TestCommon.AssertEqualsHashCode(o, o2); o = CBORObject.Undefined; - o2 = CBORObject.FromObjectAndTag(o, i); + o2 = CBORObject.FromCBORObjectAndTag(o, i); TestCommon.AssertEqualsHashCode(o, o2); - o = CBORObject.FromObjectAndTag(o, i + 1); + o = CBORObject.FromCBORObjectAndTag(o, i + 1); TestCommon.AssertEqualsHashCode(o, o2); } } @@ -3073,7 +3073,7 @@ public void TestTags() { continue; } } - var obj = CBORObject.FromObjectAndTag(0, bigintTemp); + var obj = CBORObject.FromCBORObjectAndTag(CBORObject.FromInt(0), bigintTemp); if (!obj.IsTagged) { Assert.Fail("obj not tagged"); } @@ -3095,7 +3095,7 @@ public void TestTags() { if (!bigintTemp.Equals(maxuint)) { EInteger bigintNew = bigintNext; // Test multiple tags - var obj2 = CBORObject.FromObjectAndTag(obj, bigintNew); + var obj2 = CBORObject.FromCBORObjectAndTag(obj, bigintNew); EInteger[] bi = obj2.GetAllTags(); if (bi.Length != 2) { { diff --git a/CBORTest/CBORTestCommon.cs b/CBORTest/CBORTestCommon.cs index 420af356..36648f94 100644 --- a/CBORTest/CBORTestCommon.cs +++ b/CBORTest/CBORTestCommon.cs @@ -161,9 +161,9 @@ public static CBORObject RandomCBORTaggedObject( tag = tagselection[rand.GetInt32(tagselection.Length)]; } else { return rand.GetInt32(100) < 90 ? - CBORObject.FromObjectAndTag( + CBORObject.FromCBORObjectAndTag( RandomCBORObject(rand, depth + 1), - rand.GetInt32(0x100000)) : CBORObject.FromObjectAndTag( + rand.GetInt32(0x100000)) : CBORObject.FromCBORObjectAndTag( RandomCBORObject(rand, depth + 1), RandomEIntegerMajorType0(rand)); } @@ -198,7 +198,7 @@ public static CBORObject RandomCBORTaggedObject( } else { cbor = RandomCBORObject(rand, depth + 1); } - return CBORObject.FromObjectAndTag(cbor, tag); + return CBORObject.FromCBORObjectAndTag(cbor, tag); } } diff --git a/CBORTest/ToObjectTest.cs b/CBORTest/ToObjectTest.cs index 84e4c632..2a778afc 100644 --- a/CBORTest/ToObjectTest.cs +++ b/CBORTest/ToObjectTest.cs @@ -1241,8 +1241,8 @@ public void TestToObject() { if (iintDict["b"] != 2) { Assert.Fail(); } - co = CBORObject.FromObjectAndTag( - "2000-01-01T00:00:00Z", + co = CBORObject.FromCBORObjectAndTag( + CBORObject.FromString("2000-01-01T00:00:00Z"), 0); try { _ = co.ToObject(typeof(DateTime)); @@ -1304,7 +1304,7 @@ public void TestDateRoundTrip() { var rand = new RandomGenerator(); for (int i = 0; i < 5000; ++i) { string s = RandomDate(rand); - var cbor = CBORObject.FromObjectAndTag(s, 0); + var cbor = CBORObject.FromCBORObjectAndTag(CBORObject.FromString(s), 0); var dtime = (DateTime)cbor.ToObject(typeof(DateTime)); var cbor2 = CBORObject.FromObject(dtime); Assert.AreEqual(s, cbor2.AsString()); @@ -1320,7 +1320,7 @@ public void TestDateRoundTripNumber() { CBORDateConverter.TaggedNumber); for (int i = 0; i < 5000; ++i) { string s = RandomDate(rand); - var cbor = CBORObject.FromObjectAndTag(s, 0); + var cbor = CBORObject.FromCBORObjectAndTag(CBORObject.FromString(s), 0); var dtime = (DateTime)cbor.ToObject(typeof(DateTime)); var cbor2 = CBORObject.FromObject(dtime); Assert.AreEqual(s, cbor2.AsString()); @@ -1345,7 +1345,7 @@ public void TestDateRoundTripUntaggedNumber() { CBORDateConverter.UntaggedNumber); for (int i = 0; i < 5000; ++i) { string s = RandomDate(rand); - var cbor = CBORObject.FromObjectAndTag(s, 0); + var cbor = CBORObject.FromCBORObjectAndTag(CBORObject.FromString(s), 0); var dtime = (DateTime)cbor.ToObject(typeof(DateTime)); var cbor2 = CBORObject.FromObject(dtime); Assert.AreEqual(s, cbor2.AsString()); @@ -1364,8 +1364,8 @@ public void TestDateRoundTripUntaggedNumber() { [Test] public void TestBadDate() { - var cbor = CBORObject.FromObjectAndTag( - "2000-1-01T00:00:00Z", + var cbor = CBORObject.FromCBORObjectAndTag( + CBORObject.FromString("2000-1-01T00:00:00Z"), 0); try { _ = cbor.ToObject(typeof(DateTime)); @@ -1376,8 +1376,8 @@ public void TestBadDate() { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } - cbor = CBORObject.FromObjectAndTag( - "2000-01-1T00:00:00Z", + cbor = CBORObject.FromCBORObjectAndTag( + CBORObject.FromString("2000-01-1T00:00:00Z"), 0); try { _ = cbor.ToObject(typeof(DateTime)); @@ -1388,8 +1388,8 @@ public void TestBadDate() { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } - cbor = CBORObject.FromObjectAndTag( - "2000-01-01T0:00:00Z", + cbor = CBORObject.FromCBORObjectAndTag( + CBORObject.FromString("2000-01-01T0:00:00Z"), 0); try { _ = cbor.ToObject(typeof(DateTime)); @@ -1400,8 +1400,8 @@ public void TestBadDate() { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } - cbor = CBORObject.FromObjectAndTag( - "2000-01-01T00:0:00Z", + cbor = CBORObject.FromCBORObjectAndTag( + CBORObject.FromString("2000-01-01T00:0:00Z"), 0); try { _ = cbor.ToObject(typeof(DateTime)); @@ -1412,8 +1412,8 @@ public void TestBadDate() { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } - cbor = CBORObject.FromObjectAndTag( - "2000-01-01T00:00:0Z", + cbor = CBORObject.FromCBORObjectAndTag( + CBORObject.FromString("2000-01-01T00:00:0Z"), 0); try { _ = cbor.ToObject(typeof(DateTime)); @@ -1424,8 +1424,8 @@ public void TestBadDate() { Assert.Fail(ex.ToString()); throw new InvalidOperationException(String.Empty, ex); } - cbor = CBORObject.FromObjectAndTag( - "T01:01:01Z", + cbor = CBORObject.FromCBORObjectAndTag( + CBORObject.FromString("T01:01:01Z"), 0); try { _ = cbor.ToObject(typeof(DateTime)); From a889b033578016580372a4d8b9d2472d21f0b02f Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Tue, 10 Oct 2023 19:20:22 +0100 Subject: [PATCH 13/29] remove unneeded annotation --- CBOR/PeterO/Cbor/CBORObject.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index 634b5132..0c5d5a05 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -604,7 +604,6 @@ public CBORObject GetOrDefault(CBORObject cborkey, CBORObject defaultValue) { /// or map. If this is a CBOR map, returns null (not /// CBORObject.Null ) if an item with the given key doesn't /// exist. - [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public CBORObject GetOrDefault(int key, CBORObject defaultValue) { if (this.Type == CBORType.Array) { int index = key; From cd99c3ddcb1ea385819c5bce03a42cc37047f14c Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Tue, 10 Oct 2023 20:58:21 +0100 Subject: [PATCH 14/29] re-add as obsolete --- CBOR/PeterO/Cbor/CBORObjectExtra.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CBOR/PeterO/Cbor/CBORObjectExtra.cs b/CBOR/PeterO/Cbor/CBORObjectExtra.cs index 294dc173..8fc8f3d4 100644 --- a/CBOR/PeterO/Cbor/CBORObjectExtra.cs +++ b/CBOR/PeterO/Cbor/CBORObjectExtra.cs @@ -246,7 +246,14 @@ public static void Write(ushort value, Stream stream) { public static CBORObject FromSbyte(sbyte value) { return FromInt64((long)value); } - // TODO: re-add FromObject(sbyte value) as deprecated + + /// Converts a signed 8-bit integer to a CBOR object. + /// The input. + /// A CBORObject object. + [Obsolete("Use FromSbyte instead")] + [CLSCompliant(false)] + public static CBORObject FromObject(sbyte value) => FromSbyte(value); + private static EInteger UInt64ToEInteger(ulong value) { var data = new byte[9]; ulong uvalue = value; From 2acc6713ab4f1d8c5f25465905ced6f55157c8c8 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Tue, 10 Oct 2023 20:58:33 +0100 Subject: [PATCH 15/29] done --- CBOR/PeterO/Cbor/CBORDateConverter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CBOR/PeterO/Cbor/CBORDateConverter.cs b/CBOR/PeterO/Cbor/CBORDateConverter.cs index 6ce38788..d552f2a7 100644 --- a/CBOR/PeterO/Cbor/CBORDateConverter.cs +++ b/CBOR/PeterO/Cbor/CBORDateConverter.cs @@ -127,7 +127,7 @@ public CBORDateConverter(ConversionType convType) { /// The format of the CBOR /// object is not supported, or another error occurred in /// conversion. - public DateTime FromCBORObject(CBORObject obj) { // TODO: fix this method to only use type-safe code + public DateTime FromCBORObject(CBORObject obj) { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } From d2942a0be1aca922ab12e8b7c83c6f25b3a87825 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Tue, 10 Oct 2023 20:59:01 +0100 Subject: [PATCH 16/29] done --- CBOR/PeterO/Cbor/CBORNumber.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CBOR/PeterO/Cbor/CBORNumber.cs b/CBOR/PeterO/Cbor/CBORNumber.cs index 912db97f..89a1901b 100644 --- a/CBOR/PeterO/Cbor/CBORNumber.cs +++ b/CBOR/PeterO/Cbor/CBORNumber.cs @@ -83,7 +83,7 @@ internal static ICBORNumber GetNumberInterface(NumberKind kind) { /// Converts this object's value to a CBOR object. /// A CBOR object that stores this object's value. - public CBORObject ToCBORObject() { // TODO: use a safe method to get this + public CBORObject ToCBORObject() { object obj = this.value; if (obj is long l) { return CBORObject.FromInt64(l); From b0fd1537243197afd6b86943e9ef2a00e56115ef Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Wed, 11 Oct 2023 12:32:53 +0100 Subject: [PATCH 17/29] add obsolete methods --- .../Cbor/CBORDataUtilitiesByteArrayString.cs | 6 +- .../Cbor/CBORDataUtilitiesCharArrayString.cs | 6 +- .../Cbor/CBORDataUtilitiesTextString.cs | 6 +- CBOR/PeterO/Cbor/CBORObject.cs | 135 ++++++++++++++++-- CBOR/PeterO/Cbor/CBORObjectExtra.cs | 27 +++- CBOR/PeterO/Cbor/JSONPatch.cs | 11 +- CBOR/PeterO/Cbor/JSONPointer.cs | 3 - 7 files changed, 156 insertions(+), 38 deletions(-) diff --git a/CBOR/PeterO/Cbor/CBORDataUtilitiesByteArrayString.cs b/CBOR/PeterO/Cbor/CBORDataUtilitiesByteArrayString.cs index 7a6be86a..8438af13 100644 --- a/CBOR/PeterO/Cbor/CBORDataUtilitiesByteArrayString.cs +++ b/CBOR/PeterO/Cbor/CBORDataUtilitiesByteArrayString.cs @@ -161,7 +161,7 @@ internal static CBORObject ParseJSONNumber( CBORObject.FromFloatingPointBits(0x8000, 2); } else if (kind == JSONOptions.ConversionMode.IntOrFloatFromDouble || kind == JSONOptions.ConversionMode.IntOrFloat) { - return CBORObject.FromObject(0); + return CBORObject.FromInt(0); } } else if (negativeExp) { // underflow @@ -170,7 +170,7 @@ internal static CBORObject ParseJSONNumber( return !negative ? CBORObject.FromFloatingPointBits(0, 2) : CBORObject.FromFloatingPointBits(0x8000, 2); } else if (kind == JSONOptions.ConversionMode.IntOrFloatFromDouble) { - return CBORObject.FromObject(0); + return CBORObject.FromInt(0); } } else { // overflow @@ -264,7 +264,7 @@ internal static CBORObject ParseJSONNumber( if (ed.Exponent.IsZero) { return preserveNegativeZero ? CBORObject.FromObject(EDecimal.NegativeZero) : - CBORObject.FromObject(0); + CBORObject.FromInt(0); } else { return !preserveNegativeZero ? CBORObject.FromObject(ed.Negate()) : CBORObject.FromObject(ed); diff --git a/CBOR/PeterO/Cbor/CBORDataUtilitiesCharArrayString.cs b/CBOR/PeterO/Cbor/CBORDataUtilitiesCharArrayString.cs index bb983f89..0172fccb 100644 --- a/CBOR/PeterO/Cbor/CBORDataUtilitiesCharArrayString.cs +++ b/CBOR/PeterO/Cbor/CBORDataUtilitiesCharArrayString.cs @@ -161,7 +161,7 @@ internal static CBORObject ParseJSONNumber( CBORObject.FromFloatingPointBits(0x8000, 2); } else if (kind == JSONOptions.ConversionMode.IntOrFloatFromDouble || kind == JSONOptions.ConversionMode.IntOrFloat) { - return CBORObject.FromObject(0); + return CBORObject.FromInt(0); } } else if (negativeExp) { // underflow @@ -170,7 +170,7 @@ internal static CBORObject ParseJSONNumber( return !negative ? CBORObject.FromFloatingPointBits(0, 2) : CBORObject.FromFloatingPointBits(0x8000, 2); } else if (kind == JSONOptions.ConversionMode.IntOrFloatFromDouble) { - return CBORObject.FromObject(0); + return CBORObject.FromInt(0); } } else { // overflow @@ -264,7 +264,7 @@ internal static CBORObject ParseJSONNumber( if (ed.Exponent.IsZero) { return preserveNegativeZero ? CBORObject.FromObject(EDecimal.NegativeZero) : - CBORObject.FromObject(0); + CBORObject.FromInt(0); } else { return !preserveNegativeZero ? CBORObject.FromObject(ed.Negate()) : CBORObject.FromObject(ed); diff --git a/CBOR/PeterO/Cbor/CBORDataUtilitiesTextString.cs b/CBOR/PeterO/Cbor/CBORDataUtilitiesTextString.cs index aefa0889..dbbaa90d 100644 --- a/CBOR/PeterO/Cbor/CBORDataUtilitiesTextString.cs +++ b/CBOR/PeterO/Cbor/CBORDataUtilitiesTextString.cs @@ -161,7 +161,7 @@ internal static CBORObject ParseJSONNumber( CBORObject.FromFloatingPointBits(0x8000, 2); } else if (kind == JSONOptions.ConversionMode.IntOrFloatFromDouble || kind == JSONOptions.ConversionMode.IntOrFloat) { - return CBORObject.FromObject(0); + return CBORObject.FromInt(0); } } else if (negativeExp) { // underflow @@ -170,7 +170,7 @@ internal static CBORObject ParseJSONNumber( return !negative ? CBORObject.FromFloatingPointBits(0, 2) : CBORObject.FromFloatingPointBits(0x8000, 2); } else if (kind == JSONOptions.ConversionMode.IntOrFloatFromDouble) { - return CBORObject.FromObject(0); + return CBORObject.FromInt(0); } } else { // overflow @@ -263,7 +263,7 @@ internal static CBORObject ParseJSONNumber( return ed.IsZero && negative ? ed.Exponent.IsZero ? preserveNegativeZero ? CBORObject.FromObject(EDecimal.NegativeZero) : - CBORObject.FromObject(0) : + CBORObject.FromInt(0) : !preserveNegativeZero ? CBORObject.FromObject(ed.Negate()) : CBORObject.FromObject(ed) : ed.Exponent.IsZero ? CBORObject.FromEInteger(ed.Mantissa) : diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index 0c5d5a05..b247f2b3 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -1887,7 +1887,14 @@ public static CBORObject FromInt64(long value) { (value >= -24L && value < 0L) ? FixedObjects[0x20 - (int)(value + 1L)] : new CBORObject(CBORObjectTypeInteger, value); } - // TODO: readd FromObject(long value) as deprecated + + /// Generates a CBOR object from a 64-bit signed + /// integer. + /// The parameter is a + /// 64-bit signed integer. + /// A CBOR object. + [Obsolete("Use FromInt64 instead.")] + public static CBORObject FromObject(long value) => FromInt64(value); /// Generates a CBOR object from a CBOR object. /// The parameter is a @@ -2056,9 +2063,16 @@ public static CBORObject FromEInteger(EInteger bigintValue) { } } } - // public static CBORObject FromObject(EInteger bigintValue) { // TODO: reinstate as deprecated - // return FromEInteger(bigintValue); - // } + + /// Generates a CBOR object from an arbitrary-precision + /// integer. + /// An arbitrary-precision integer. Can be + /// null. + /// The given number encoded as a CBOR object. Returns + /// CBORObject.Null if is + /// null. + [Obsolete("Use FromEInteger instead.")] + public static CBORObject FromObject(EInteger bigintValue) => FromEInteger(bigintValue); /// Generates a CBOR object from an arbitrary-precision binary /// floating-point number. The CBOR object is generated as follows @@ -2260,9 +2274,13 @@ public static CBORObject FromString(string strValue) { strValue.Length == utf8Length ? CBORObjectTypeTextStringAscii : CBORObjectTypeTextString, strValue); } - // public static CBORObject FromObject(string strValue) { // TODO: reinstate as deprecated - // return FromString(strValue); - // } + + /// Generates a CBOR object from a text string. + /// A text string value. Can be null. + /// A CBOR object representing the string, or CBORObject.Null + /// if stringValue is null. + [Obsolete("Use FromString instead.")] + public static CBORObject FromObject(string strValue) => FromString(strValue); /// Generates a CBOR object from a 32-bit signed /// integer. @@ -2274,7 +2292,14 @@ public static CBORObject FromInt(int value) { (value >= -24 && value < 0) ? FixedObjects[0x20 - (value + 1)] : FromInt64((long)value); } - // TODO: readd FromObject(int value) as deprecated + + /// Generates a CBOR object from a 32-bit signed + /// integer. + /// The parameter is a + /// 32-bit signed integer. + /// A CBOR object. + [Obsolete("Use FromInt instead.")] + public static CBORObject FromObject(int value) => FromInt(value); /// Generates a CBOR object from a 16-bit signed /// integer. @@ -2359,7 +2384,14 @@ public static CBORObject FromByteArray(byte[] bytes) { Array.Copy(bytes, 0, newvalue, 0, bytes.Length); return new CBORObject(CBORObjectTypeByteString, bytes); } - // TODO: readd FromObject(byte[] bytes) as deprecated + + /// Generates a CBOR object from an array of 8-bit bytes. + /// An array of 8-bit bytes; can be null. + /// 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. + [Obsolete("Use FromByteArray instead.")] + public static CBORObject FromObject(byte[] bytes) => FromByteArray(bytes); /// Generates a CBOR object from an array of CBOR /// objects. @@ -2756,7 +2788,7 @@ internal static CBORObject FromObject( } } if (obj is string) { - return FromObject((string)obj); + return FromString((string)obj); } if (obj is int) { return FromInt((int)obj); @@ -2792,7 +2824,7 @@ internal static CBORObject FromObject( return FromObject((float)obj); } if (obj is sbyte) { - return FromObject((sbyte)obj); + return FromSbyte((sbyte)obj); } if (obj is ulong) { return FromUInt64((ulong)obj); @@ -2810,7 +2842,7 @@ internal static CBORObject FromObject( return FromObject((double)obj); } if (obj is byte[] bytearr) { - return FromObject(bytearr); + return FromByteArray(bytearr); } if (obj is System.Collections.IDictionary) { // IDictionary appears first because IDictionary includes IEnumerable @@ -5005,7 +5037,6 @@ 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. - [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"); @@ -5038,7 +5069,6 @@ 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. - [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); } @@ -5070,7 +5100,6 @@ public CBORObject AtJSONPointer(string pointer, CBORObject 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. - [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); } @@ -5427,6 +5456,7 @@ public bool HasTag(EInteger bigTagValue) { /// name='valueOb'/> has an unsupported type; or is not a valid index into this array. [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] + [Obsolete("Use the CBORObject overload instead.")] public CBORObject Insert(int index, object valueOb) { if (this.Type == CBORType.Array) { CBORObject mapValue; @@ -5449,6 +5479,30 @@ public CBORObject Insert(int index, object valueOb) { return this; } + /// Inserts a CBORObject at the specified position in this CBOR + /// array. + /// Index starting at 0 to insert at. + /// A CBORObject representing the value. + /// This instance. + /// This object is not an + /// array. + /// is not a valid index into this array. + public CBORObject Insert(int index, CBORObject cborObj) { + if (this.Type == CBORType.Array) { + IList list = this.AsList(); + if (index < 0 || index > list.Count) { + throw new ArgumentOutOfRangeException(nameof(index)); + } + list.Insert( + index, + cborObj); + } else { + throw new InvalidOperationException("Not an array"); + } + return this; + } + /// Removes all items from this CBOR array or all keys and /// values from this CBOR map. /// This object is not a @@ -5550,6 +5604,7 @@ public bool Remove(CBORObject obj) { /// than 0, is the size of this array or greater, or is not a 32-bit /// signed integer ( int ). [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] + [Obsolete("Use the CBORObject overload instead.")] public CBORObject Set(object key, object valueOb) { if (this.Type == CBORType.Map) { CBORObject mapKey; @@ -5592,6 +5647,56 @@ public CBORObject Set(object key, object valueOb) { return this; } + /// Maps an object to a key in this CBOR map, or adds the + /// value if the key doesn't exist. + /// 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. + /// A CBORObject representing the value, which should be of type CBORType.Map. + /// This instance. + /// This object is not a + /// map. + /// The parameter + /// or this instance is a CBOR array. + public CBORObject Set(CBORObject mapKey, CBORObject mapValue) { + if (this.Type == CBORType.Map) { + IDictionary map = this.AsMap(); + map[mapKey] = mapValue; + } else if (this.Type == CBORType.Array) { + throw new ArgumentException("mapValue is an array, but key is not int"); + } else { + throw new ArgumentException("mapValue i not a map or array"); + } + return this; + } + + /// Sets the value of a CBORObject of type Array at the given index to the given value. + /// This parameter must be a 32-bit signed integer( + /// int ) identifying the index (starting from 0) of the item to + /// set in the array. + /// An CBORObject representing the value. + /// This instance. + /// mapValue is not a + /// an array. + public CBORObject Set(int key, CBORObject mapValue) { + if (this.Type == CBORType.Map) { + CBORObject mapKey = CBORObject.FromInt(key); + IDictionary map = this.AsMap(); + map[mapKey] = mapValue; + } else if (this.Type == CBORType.Array) { + IList list = this.AsList(); + var index = (int)key; + if (index < 0 || index >= this.Count) { + throw new ArgumentOutOfRangeException(nameof(key)); + } + list[index] = mapValue; + } else { + throw new InvalidOperationException("Not an array"); + } + return this; + } + /// 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. diff --git a/CBOR/PeterO/Cbor/CBORObjectExtra.cs b/CBOR/PeterO/Cbor/CBORObjectExtra.cs index 8fc8f3d4..dc3ccdf8 100644 --- a/CBOR/PeterO/Cbor/CBORObjectExtra.cs +++ b/CBOR/PeterO/Cbor/CBORObjectExtra.cs @@ -277,7 +277,14 @@ private static EInteger UInt64ToEInteger(ulong value) { public static CBORObject FromUInt64(ulong value) { return CBORObject.FromEInteger(UInt64ToEInteger(value)); } - // TODO: re-add FromObject(ulong value) as deprecated + + /// Converts a 64-bit unsigned integer to a CBOR + /// object. + /// A 64-bit unsigned integer. + /// A CBORObject object. + [CLSCompliant(false)] + [Obsolete("Use FromUInt64 instead")] + public static CBORObject FromObject(ulong value) => FromUInt64(value); /// Converts a 32-bit unsigned integer to a CBOR /// object. @@ -287,7 +294,14 @@ public static CBORObject FromUInt64(ulong value) { public static CBORObject FromUInt(uint value) { return FromInt64((long)value); } - // TODO: re-add FromObject(uint value) as deprecated + + /// Converts a 32-bit unsigned integer to a CBOR + /// object. + /// A 32-bit unsigned integer. + /// A CBORObject object. + [CLSCompliant(false)] + [Obsolete("Use FromUInt instead")] + public static CBORObject FromObject(uint value) => FromUInt(value); /// Converts a 16-bit unsigned integer to a CBOR /// object. @@ -297,7 +311,14 @@ public static CBORObject FromUInt(uint value) { public static CBORObject FromUShort(ushort value) { return FromInt64((long)value); } - // TODO re-add FromObject(ushort value) as deprecated + + /// Converts a 16-bit unsigned integer to a CBOR + /// object. + /// A 16-bit unsigned integer. + /// A CBORObject object. + [CLSCompliant(false)] + [Obsolete("Use FromUShort instead")] + public static CBORObject FromObject(ushort value) => FromInt64((long)value); /// Generates a CBOR object from this one, but gives the /// resulting object a tag in addition to its existing tags (the new diff --git a/CBOR/PeterO/Cbor/JSONPatch.cs b/CBOR/PeterO/Cbor/JSONPatch.cs index f2f6e32b..60ac7336 100644 --- a/CBOR/PeterO/Cbor/JSONPatch.cs +++ b/CBOR/PeterO/Cbor/JSONPatch.cs @@ -11,7 +11,6 @@ licensed under Creative Commons Zero (CC0): namespace PeterO.Cbor { internal static class JSONPatch { - [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. private static CBORObject AddOperation( CBORObject o, string valueOpStr, @@ -38,7 +37,7 @@ private static CBORObject AddOperation( // DebugUtility.Log("after "+parent+""); } else if (pointer.GetParent().Type == CBORType.Map) { string key = pointer.GetKey(); - _ = parent.Set(key, value); + _ = parent.Set(CBORObject.FromString(key), value); } else { throw new CBORException("Patch " + valueOpStr + " path"); } @@ -56,7 +55,6 @@ private static CBORObject CloneCbor(CBORObject o) { } } - [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. private static string GetString(CBORObject o, string str) { #if DEBUG if (o == null) { @@ -73,7 +71,6 @@ private static string GetString(CBORObject o, string str) { "\u0020text string type") : co.AsString(); } - [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. public static CBORObject Patch(CBORObject o, CBORObject ptch) { // clone the object in case of failure if (o == null) { @@ -205,7 +202,6 @@ public static CBORObject Patch(CBORObject o, CBORObject ptch) { return o ?? CBORObject.Null; } - [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. private static CBORObject RemoveOperation( CBORObject o, string valueOpStr, @@ -225,13 +221,12 @@ private static CBORObject RemoveOperation( _ = pointer.GetParent().RemoveAt(pointer.GetIndex()); } else if (pointer.GetParent().Type == CBORType.Map) { _ = pointer.GetParent().Remove( - CBORObject.FromObject(pointer.GetKey())); + CBORObject.FromString(pointer.GetKey())); } return o; } } - [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. private static CBORObject ReplaceOperation( CBORObject o, string valueOpStr, @@ -261,7 +256,7 @@ private static CBORObject ReplaceOperation( pointer.GetParent().Set(index, value); } else if (pointer.GetParent().Type == CBORType.Map) { string key = pointer.GetKey(); - pointer.GetParent().Set(key, value); + pointer.GetParent().Set(CBORObject.FromString(key), value); } else { throw new CBORException("Patch " + valueOpStr + " path"); } diff --git a/CBOR/PeterO/Cbor/JSONPointer.cs b/CBOR/PeterO/Cbor/JSONPointer.cs index b66ce6f8..d4150488 100644 --- a/CBOR/PeterO/Cbor/JSONPointer.cs +++ b/CBOR/PeterO/Cbor/JSONPointer.cs @@ -18,7 +18,6 @@ internal sealed class JSONPointer { private readonly bool isRoot; private readonly CBORObject jsonobj; - [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. public static JSONPointer FromPointer(CBORObject obj, string pointer) { var index = 0; if (pointer == null) { @@ -136,7 +135,6 @@ public static JSONPointer FromPointer(CBORObject obj, string pointer) { } } } - [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. public static CBORObject GetObject( CBORObject obj, string pointer, @@ -271,7 +269,6 @@ public CBORObject GetParent() { return this.jsonobj; } - [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // This uses GetOrDefault. TODO: reassess when GetOrDefault is adjusted. public CBORObject GetValue() { if (this.isRoot) { // Root always exists From 3f32688abf7884b8f3dfa90d0f97b841b5280e59 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Wed, 11 Oct 2023 19:09:57 +0100 Subject: [PATCH 18/29] more explicit static members and obsoletes --- CBOR/PeterO/Cbor/CBORDataUtilities.cs | 4 +- .../Cbor/CBORDataUtilitiesByteArrayString.cs | 14 +-- .../Cbor/CBORDataUtilitiesCharArrayString.cs | 14 +-- .../Cbor/CBORDataUtilitiesTextString.cs | 16 ++-- CBOR/PeterO/Cbor/CBORNumber.cs | 6 +- CBOR/PeterO/Cbor/CBORObject.cs | 69 ++++++++++----- CBOR/PeterO/Cbor/CBORObjectExtra.cs | 10 ++- CBORTest/BEncoding.cs | 2 +- CBORTest/CBORExtraTest.cs | 20 ++--- CBORTest/CBORNumberTest.cs | 52 +++++------ CBORTest/CBORSupplementTest.cs | 2 +- CBORTest/CBORTest.cs | 86 +++++++++---------- CBORTest/CBORTestCommon.cs | 6 +- CBORTest/JSONWithComments.cs | 2 +- 14 files changed, 168 insertions(+), 135 deletions(-) diff --git a/CBOR/PeterO/Cbor/CBORDataUtilities.cs b/CBOR/PeterO/Cbor/CBORDataUtilities.cs index 03627b79..8d753f3a 100644 --- a/CBOR/PeterO/Cbor/CBORDataUtilities.cs +++ b/CBOR/PeterO/Cbor/CBORDataUtilities.cs @@ -301,7 +301,7 @@ internal static CBORObject ParseSmallNumberAsNegative( 8); } else if (options != null && options.NumberConversion == JSONOptions.ConversionMode.Decimal128) { - return CBORObject.FromObject(EDecimal.FromInt32(-digit)); + return CBORObject.FromEDecimal(EDecimal.FromInt32(-digit)); } else { // NOTE: Assumes digit is greater than zero, so PreserveNegativeZeros is // irrelevant @@ -325,7 +325,7 @@ internal static CBORObject ParseSmallNumber(int digit, JSONOptions 8); } else if (options != null && options.NumberConversion == JSONOptions.ConversionMode.Decimal128) { - return CBORObject.FromObject(EDecimal.FromInt32(digit)); + return CBORObject.FromEDecimal(EDecimal.FromInt32(digit)); } else { // NOTE: Assumes digit is nonnegative, so PreserveNegativeZeros is irrelevant return CBORObject.FromInt(digit); diff --git a/CBOR/PeterO/Cbor/CBORDataUtilitiesByteArrayString.cs b/CBOR/PeterO/Cbor/CBORDataUtilitiesByteArrayString.cs index 8438af13..f332bcc1 100644 --- a/CBOR/PeterO/Cbor/CBORDataUtilitiesByteArrayString.cs +++ b/CBOR/PeterO/Cbor/CBORDataUtilitiesByteArrayString.cs @@ -181,7 +181,7 @@ internal static CBORObject ParseJSONNumber( negative ? DoubleNegInfinity : DoublePosInfinity, 8); } else if (kind == JSONOptions.ConversionMode.Decimal128) { - return CBORObject.FromObject(negative ? + return CBORObject.FromEDecimal(negative ? EDecimal.NegativeInfinity : EDecimal.PositiveInfinity); } } @@ -203,14 +203,14 @@ internal static CBORObject ParseJSONNumber( ? CBORObject.FromFloatingPointBits(EFloat.FromInt64(v).ToDoubleBits(), 8) : kind == JSONOptions.ConversionMode.Decimal128 ? -CBORObject.FromObject(EDecimal.FromInt64(v)) : CBORObject.FromInt64(v); +CBORObject.FromEDecimal(EDecimal.FromInt64(v)) : CBORObject.FromInt64(v); } } if (kind == JSONOptions.ConversionMode.Full) { if (!haveDecimalPoint && !haveExponent) { EInteger ei = EInteger.FromSubstring(chars, initialOffset, endPos); return (preserveNegativeZero && ei.IsZero && negative) ? -CBORObject.FromObject(EDecimal.NegativeZero) : +CBORObject.FromEDecimal(EDecimal.NegativeZero) : CBORObject.FromEInteger(ei); } if (!haveExponent && haveDecimalPoint) { @@ -263,15 +263,15 @@ internal static CBORObject ParseJSONNumber( if (ed.IsZero && negative) { if (ed.Exponent.IsZero) { return preserveNegativeZero ? - CBORObject.FromObject(EDecimal.NegativeZero) : + CBORObject.FromEDecimal(EDecimal.NegativeZero) : CBORObject.FromInt(0); } else { return !preserveNegativeZero ? -CBORObject.FromObject(ed.Negate()) : CBORObject.FromObject(ed); +CBORObject.FromEDecimal(ed.Negate()) : CBORObject.FromEDecimal(ed); } } else { return ed.Exponent.IsZero ? CBORObject.FromEInteger(ed.Mantissa) : - CBORObject.FromObject(ed); + CBORObject.FromEDecimal(ed); } } else if (kind == JSONOptions.ConversionMode.Double) { var ef = EFloat.FromString( @@ -293,7 +293,7 @@ internal static CBORObject ParseJSONNumber( if (!preserveNegativeZero && ed.IsNegative && ed.IsZero) { ed = ed.Negate(); } - return CBORObject.FromObject(ed); + return CBORObject.FromEDecimal(ed); } else if (kind == JSONOptions.ConversionMode.IntOrFloatFromDouble) { var ef = EFloat.FromString( chars, diff --git a/CBOR/PeterO/Cbor/CBORDataUtilitiesCharArrayString.cs b/CBOR/PeterO/Cbor/CBORDataUtilitiesCharArrayString.cs index 0172fccb..a06b7028 100644 --- a/CBOR/PeterO/Cbor/CBORDataUtilitiesCharArrayString.cs +++ b/CBOR/PeterO/Cbor/CBORDataUtilitiesCharArrayString.cs @@ -181,7 +181,7 @@ internal static CBORObject ParseJSONNumber( negative ? DoubleNegInfinity : DoublePosInfinity, 8); } else if (kind == JSONOptions.ConversionMode.Decimal128) { - return CBORObject.FromObject(negative ? + return CBORObject.FromEDecimal(negative ? EDecimal.NegativeInfinity : EDecimal.PositiveInfinity); } } @@ -203,14 +203,14 @@ internal static CBORObject ParseJSONNumber( ? CBORObject.FromFloatingPointBits(EFloat.FromInt64(v).ToDoubleBits(), 8) : kind == JSONOptions.ConversionMode.Decimal128 ? -CBORObject.FromObject(EDecimal.FromInt64(v)) : CBORObject.FromInt64(v); +CBORObject.FromEDecimal(EDecimal.FromInt64(v)) : CBORObject.FromInt64(v); } } if (kind == JSONOptions.ConversionMode.Full) { if (!haveDecimalPoint && !haveExponent) { EInteger ei = EInteger.FromSubstring(chars, initialOffset, endPos); return (preserveNegativeZero && ei.IsZero && negative) ? -CBORObject.FromObject(EDecimal.NegativeZero) : +CBORObject.FromEDecimal(EDecimal.NegativeZero) : CBORObject.FromEInteger(ei); } if (!haveExponent && haveDecimalPoint) { @@ -263,15 +263,15 @@ internal static CBORObject ParseJSONNumber( if (ed.IsZero && negative) { if (ed.Exponent.IsZero) { return preserveNegativeZero ? - CBORObject.FromObject(EDecimal.NegativeZero) : + CBORObject.FromEDecimal(EDecimal.NegativeZero) : CBORObject.FromInt(0); } else { return !preserveNegativeZero ? -CBORObject.FromObject(ed.Negate()) : CBORObject.FromObject(ed); +CBORObject.FromEDecimal(ed.Negate()) : CBORObject.FromEDecimal(ed); } } else { return ed.Exponent.IsZero ? CBORObject.FromEInteger(ed.Mantissa) : - CBORObject.FromObject(ed); + CBORObject.FromEDecimal(ed); } } else if (kind == JSONOptions.ConversionMode.Double) { var ef = EFloat.FromString( @@ -293,7 +293,7 @@ internal static CBORObject ParseJSONNumber( if (!preserveNegativeZero && ed.IsNegative && ed.IsZero) { ed = ed.Negate(); } - return CBORObject.FromObject(ed); + return CBORObject.FromEDecimal(ed); } else if (kind == JSONOptions.ConversionMode.IntOrFloatFromDouble) { var ef = EFloat.FromString( chars, diff --git a/CBOR/PeterO/Cbor/CBORDataUtilitiesTextString.cs b/CBOR/PeterO/Cbor/CBORDataUtilitiesTextString.cs index dbbaa90d..9ace61bb 100644 --- a/CBOR/PeterO/Cbor/CBORDataUtilitiesTextString.cs +++ b/CBOR/PeterO/Cbor/CBORDataUtilitiesTextString.cs @@ -181,7 +181,7 @@ internal static CBORObject ParseJSONNumber( negative ? DoubleNegInfinity : DoublePosInfinity, 8); } else if (kind == JSONOptions.ConversionMode.Decimal128) { - return CBORObject.FromObject(negative ? + return CBORObject.FromEDecimal(negative ? EDecimal.NegativeInfinity : EDecimal.PositiveInfinity); } } @@ -203,14 +203,14 @@ internal static CBORObject ParseJSONNumber( ? CBORObject.FromFloatingPointBits(EFloat.FromInt64(v).ToDoubleBits(), 8) : kind == JSONOptions.ConversionMode.Decimal128 ? -CBORObject.FromObject(EDecimal.FromInt64(v)) : CBORObject.FromInt64(v); +CBORObject.FromEDecimal(EDecimal.FromInt64(v)) : CBORObject.FromInt64(v); } } if (kind == JSONOptions.ConversionMode.Full) { if (!haveDecimalPoint && !haveExponent) { EInteger ei = EInteger.FromSubstring(chars, initialOffset, endPos); return (preserveNegativeZero && ei.IsZero && negative) ? -CBORObject.FromObject(EDecimal.NegativeZero) : +CBORObject.FromEDecimal(EDecimal.NegativeZero) : CBORObject.FromEInteger(ei); } if (!haveExponent && haveDecimalPoint) { @@ -262,12 +262,12 @@ internal static CBORObject ParseJSONNumber( endPos - initialOffset); return ed.IsZero && negative ? ed.Exponent.IsZero ? preserveNegativeZero ? - CBORObject.FromObject(EDecimal.NegativeZero) : + CBORObject.FromEDecimal(EDecimal.NegativeZero) : CBORObject.FromInt(0) : - !preserveNegativeZero ? CBORObject.FromObject(ed.Negate()) : -CBORObject.FromObject(ed) : + !preserveNegativeZero ? CBORObject.FromEDecimal(ed.Negate()) : +CBORObject.FromEDecimal(ed) : ed.Exponent.IsZero ? CBORObject.FromEInteger(ed.Mantissa) : - CBORObject.FromObject(ed); + CBORObject.FromEDecimal(ed); } else if (kind == JSONOptions.ConversionMode.Double) { var ef = EFloat.FromString( chars, @@ -288,7 +288,7 @@ internal static CBORObject ParseJSONNumber( if (!preserveNegativeZero && ed.IsNegative && ed.IsZero) { ed = ed.Negate(); } - return CBORObject.FromObject(ed); + return CBORObject.FromEDecimal(ed); } else if (kind == JSONOptions.ConversionMode.IntOrFloatFromDouble) { var ef = EFloat.FromString( chars, diff --git a/CBOR/PeterO/Cbor/CBORNumber.cs b/CBOR/PeterO/Cbor/CBORNumber.cs index 89a1901b..44bf2546 100644 --- a/CBOR/PeterO/Cbor/CBORNumber.cs +++ b/CBOR/PeterO/Cbor/CBORNumber.cs @@ -92,13 +92,13 @@ public CBORObject ToCBORObject() { return CBORObject.FromEInteger(eif); } if (obj is EDecimal edf) { - return CBORObject.FromObject(edf); + return CBORObject.FromEDecimal(edf); } if (obj is EFloat eff) { - return CBORObject.FromObject(eff); + return CBORObject.FromEFloat(eff); } if (obj is ERational erf) { - return CBORObject.FromObject(erf); + return CBORObject.FromERational(erf); } throw new Exception("Unexpected type: " + obj.GetType()); } diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index b247f2b3..38d61ee4 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -130,11 +130,11 @@ private static CBORObject ConstructIntegerValue(int v) { CBORObject.ConstructSimpleValue(20); /// A not-a-number value. - public static readonly CBORObject NaN = CBORObject.FromObject(double.NaN); + public static readonly CBORObject NaN = FromDouble(double.NaN); /// The value negative infinity. public static readonly CBORObject NegativeInfinity = - CBORObject.FromObject(double.NegativeInfinity); + FromDouble(double.NegativeInfinity); /// Represents the value null. #if CODE_ANALYSIS @@ -148,7 +148,7 @@ private static CBORObject ConstructIntegerValue(int v) { /// The value positive infinity. public static readonly CBORObject PositiveInfinity = - CBORObject.FromObject(double.PositiveInfinity); + FromDouble(double.PositiveInfinity); /// Represents the value true. #if CODE_ANALYSIS @@ -1901,8 +1901,9 @@ public static CBORObject FromInt64(long value) { /// CBOR object. /// Same as , or "CBORObject.Null" is /// is null. + [Obsolete("Don't use a function and use Nullable Reference Types to guard against nulls.")] public static CBORObject FromObject(CBORObject value) { - return value ?? CBORObject.Null; + return value ?? Null; } private static int IntegerByteLength(int intValue) { @@ -2091,9 +2092,9 @@ public static CBORObject FromEInteger(EInteger bigintValue) { /// number. Can be null. /// The given number encoded as a CBOR object. Returns /// CBORObject.Null if is null. - public static CBORObject FromObject(EFloat bigValue) { + public static CBORObject FromEFloat(EFloat bigValue) { if (bigValue == null) { - return CBORObject.Null; + return Null; } CBORObject cbor; int tag; @@ -2132,6 +2133,14 @@ public static CBORObject FromObject(EFloat bigValue) { return cbor.WithTag(tag); } + /// Generates a CBOR object from an arbitrary-precision binary + /// floating-point number. + /// An arbitrary-precision binary floating-point + /// number. + /// The given number encoded as a CBOR object. + [Obsolete("Use FromEFloat instead.")] + public static CBORObject FromObject(EFloat bigValue) => FromEFloat(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): @@ -2146,7 +2155,7 @@ public static CBORObject FromObject(EFloat bigValue) { /// be null. /// The given number encoded as a CBOR object. Returns /// CBORObject.Null if is null. - public static CBORObject FromObject(ERational bigValue) { + public static CBORObject FromERational(ERational bigValue) { if (bigValue == null) { return Null; } @@ -2197,6 +2206,13 @@ public static CBORObject FromObject(ERational bigValue) { return cbor.WithTag(tag); } + /// Generates a CBOR object from an arbitrary-precision + /// rational number. + /// An arbitrary-precision rational number. + /// The given number encoded as a CBOR object. + [Obsolete("Use FromERational instead.")] + public static CBORObject FromObject(ERational bigValue) => FromERational(bigValue); + /// Generates a CBOR object from a decimal number. The CBOR /// object is generated as follows (this is a change in version 4.0): /// @@ -2213,9 +2229,9 @@ public static CBORObject FromObject(ERational bigValue) { /// be null. /// The given number encoded as a CBOR object. Returns /// CBORObject.Null if is null. - public static CBORObject FromObject(EDecimal bigValue) { + public static CBORObject FromEDecimal(EDecimal bigValue) { if (bigValue == null) { - return CBORObject.Null; + return Null; } CBORObject cbor; int tag; @@ -2234,7 +2250,7 @@ public static CBORObject FromObject(EDecimal bigValue) { cbor = NewArray( FromEInteger(bigValue.Exponent), FromEInteger(bigValue.UnsignedMantissa), - CBORObject.FromInt(options)); + FromInt(options)); tag = 268; } else { EInteger exponent = bigValue.Exponent; @@ -2254,6 +2270,12 @@ public static CBORObject FromObject(EDecimal bigValue) { return cbor.WithTag(tag); } + /// Generates a CBOR object from a decimal number. + /// An arbitrary-precision decimal number. + /// The given number encoded as a CBOR object. + [Obsolete("Use FromEDecimal instead.")] + public static CBORObject FromObject(EDecimal bigValue) => FromEDecimal(bigValue); + /// Generates a CBOR object from a text string. /// A text string value. Can be null. /// A CBOR object representing the string, or CBORObject.Null @@ -2363,11 +2385,18 @@ public static CBORObject FromObject(float value) { /// The parameter is a /// 64-bit floating-point number. /// A CBOR object generated from the given number. - public static CBORObject FromObject(double value) { + public static CBORObject FromDouble(double value) { long doubleBits = CBORUtilities.DoubleToInt64Bits(value); return new CBORObject(CBORObjectTypeDouble, doubleBits); } + /// Generates a CBOR object from a 64-bit floating-point + /// number. + /// A 64-bit floating-point number. + /// A CBOR object generated from the given number. + [Obsolete("Use FromDouble instead.")] + public static CBORObject FromObject(double value) => FromDouble(value); + /// 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 @@ -2775,10 +2804,10 @@ internal static CBORObject FromObject( throw new CBORException("Nesting depth too high"); } if (obj == null) { - return CBORObject.Null; + return Null; } if (obj is CBORObject) { - return FromObject((CBORObject)obj); + return (CBORObject)obj; } CBORObject objret; if (mapper != null) { @@ -2800,13 +2829,13 @@ internal static CBORObject FromObject( return FromEInteger(eif); } if (obj is EDecimal edf) { - return FromObject(edf); + return FromEDecimal(edf); } if (obj is EFloat eff) { - return FromObject(eff); + return FromEFloat(eff); } if (obj is ERational erf) { - return FromObject(erf); + return FromERational(erf); } if (obj is short) { return FromObject((short)obj); @@ -2839,7 +2868,7 @@ internal static CBORObject FromObject( return FromObject((decimal)obj); } if (obj is double) { - return FromObject((double)obj); + return FromDouble((double)obj); } if (obj is byte[] bytearr) { return FromByteArray(bytearr); @@ -3671,7 +3700,7 @@ public static void Write(EFloat bignum, Stream stream) { } if ((bignum.IsZero && bignum.IsNegative) || bignum.IsInfinity() || bignum.IsNaN()) { - Write(CBORObject.FromObject(bignum), stream); + Write(FromEFloat(bignum), stream); return; } EInteger exponent = bignum.Exponent; @@ -3712,7 +3741,7 @@ public static void Write(ERational rational, Stream stream) { return; } if (!rational.IsFinite || (rational.IsNegative && rational.IsZero)) { - Write(CBORObject.FromObject(rational), stream); + Write(FromERational(rational), stream); return; } stream.WriteByte(0xd8); // tag 30 @@ -3743,7 +3772,7 @@ public static void Write(EDecimal bignum, Stream stream) { return; } if (!bignum.IsFinite || (bignum.IsNegative && bignum.IsZero)) { - Write(CBORObject.FromObject(bignum), stream); + Write(FromEDecimal(bignum), stream); return; } EInteger exponent = bignum.Exponent; diff --git a/CBOR/PeterO/Cbor/CBORObjectExtra.cs b/CBOR/PeterO/Cbor/CBORObjectExtra.cs index dc3ccdf8..0b8ba80b 100644 --- a/CBOR/PeterO/Cbor/CBORObjectExtra.cs +++ b/CBOR/PeterO/Cbor/CBORObjectExtra.cs @@ -215,9 +215,13 @@ public static void Write(ulong value, Stream stream) { /// Decimal object. /// A CBORObject object with the same value as the.NET /// decimal. - public static CBORObject FromObject(decimal value) { - return FromObject((EDecimal)value); - } + public static CBORObject FromDecimal(decimal value) => FromEDecimal((EDecimal)value); + + /// Converts a.NET decimal to a CBOR object. + /// A Decimal. + /// A CBORObject object. + [Obsolete("Use FromDecimal instead")] + public static CBORObject FromObject(decimal value) => FromDecimal(value); /// Writes a 32-bit unsigned integer in CBOR format to a data /// stream. diff --git a/CBORTest/BEncoding.cs b/CBORTest/BEncoding.cs index 41d15ee6..eb2b1a4b 100644 --- a/CBORTest/BEncoding.cs +++ b/CBORTest/BEncoding.cs @@ -143,7 +143,7 @@ private static CBORObject ReadString(Stream stream, char firstChar) { false) switch { -2 => throw new CBORException("Premature end of data"), -1 => throw new CBORException("Invalid UTF-8"), - _ => CBORObject.FromObject(builder.ToString()), + _ => CBORObject.FromString(builder.ToString()), }; } diff --git a/CBORTest/CBORExtraTest.cs b/CBORTest/CBORExtraTest.cs index 687bb178..09b7762b 100644 --- a/CBORTest/CBORExtraTest.cs +++ b/CBORTest/CBORExtraTest.cs @@ -63,7 +63,7 @@ public void TestCBORObjectDecimal() { } } try { - _ = CBORObject.FromObject(EDecimal.NaN).ToObject(); + _ = CBORObject.FromEDecimal(EDecimal.NaN).ToObject(); Assert.Fail("Should have failed"); } catch (OverflowException) { // NOTE: Intentionally empty @@ -72,7 +72,7 @@ public void TestCBORObjectDecimal() { throw new InvalidOperationException(String.Empty, ex); } try { - _ = CBORObject.FromObject( + _ = CBORObject.FromEDecimal( EDecimal.SignalingNaN).ToObject(); Assert.Fail("Should have failed"); } catch (OverflowException) { @@ -112,7 +112,7 @@ public void TestCBORObjectDecimal() { throw new InvalidOperationException(String.Empty, ex); } try { - _ = CBORObject.FromObject(EFloat.SignalingNaN).ToObject(); + _ = CBORObject.FromEFloat(EFloat.SignalingNaN).ToObject(); Assert.Fail("Should have failed"); } catch (OverflowException) { // NOTE: Intentionally empty @@ -366,11 +366,11 @@ public System.Collections.Generic.IEnumerator [Test] public void TestCustomFlagsEnum() { var cbor = CBORObject.FromObject(CustomBits.A | CustomBits.B); - Assert.AreEqual(CBORObject.FromObject(3), cbor); + Assert.AreEqual(CBORObject.FromInt(3), cbor); CustomBits cfe = cbor.ToObject(); Assert.AreEqual(CustomBits.A | CustomBits.B, cfe); cbor = CBORObject.FromObject(CustomBits.A); - Assert.AreEqual(CBORObject.FromObject(1), cbor); + Assert.AreEqual(CBORObject.FromInt(1), cbor); cfe = cbor.ToObject(); Assert.AreEqual(CustomBits.A, cfe); } @@ -6574,21 +6574,21 @@ public void TestToObjectNull() { public void TestNullable() { int? nvalue = 1; var cbor = CBORObject.FromObject((object)nvalue); - Assert.AreEqual(CBORObject.FromObject(1), cbor); + Assert.AreEqual(CBORObject.FromInt(1), cbor); nvalue = null; _ = CBORObject.FromObject((object)nvalue); uint? unvalue = 1u; cbor = CBORObject.FromObject((object)unvalue); - Assert.AreEqual(CBORObject.FromObject(1), cbor); + Assert.AreEqual(CBORObject.FromInt(1), cbor); unvalue = null; cbor = CBORObject.FromObject((object)unvalue); Assert.AreEqual(CBORObject.Null, cbor); Assert.AreEqual(null, CBORObject.Null.ToObject()); - Assert.AreEqual(1, CBORObject.FromObject(1).ToObject()); + Assert.AreEqual(1, CBORObject.FromInt(1).ToObject()); Assert.AreEqual(null, CBORObject.Null.ToObject()); - Assert.AreEqual(1u, CBORObject.FromObject(1).ToObject()); + Assert.AreEqual(1u, CBORObject.FromInt(1).ToObject()); Assert.AreEqual(null, CBORObject.Null.ToObject()); - if (CBORObject.FromObject(3.5).ToObject() != 3.5) { + if (CBORObject.FromDouble(3.5).ToObject() != 3.5) { Assert.Fail(); } ExoticStruct? es = null; diff --git a/CBORTest/CBORNumberTest.cs b/CBORTest/CBORNumberTest.cs index c0476ff1..0071176c 100644 --- a/CBORTest/CBORNumberTest.cs +++ b/CBORTest/CBORNumberTest.cs @@ -62,8 +62,8 @@ public void TestCanFitInInt32() { [Test] public void TestCanFitInInt64() { - Assert.IsTrue(CBORObject.FromObject(0).AsNumber().CanFitInInt64()); - Assert.IsTrue(CBORObject.FromObject(99).AsNumber().CanFitInInt64()); + Assert.IsTrue(CBORObject.FromInt(0).AsNumber().CanFitInInt64()); + Assert.IsTrue(CBORObject.FromInt(99).AsNumber().CanFitInInt64()); Assert.IsFalse(CBORObject.PositiveInfinity.AsNumber().CanFitInInt64()); Assert.IsFalse(CBORObject.NegativeInfinity.AsNumber().CanFitInInt64()); Assert.IsFalse(CBORObject.NaN.AsNumber().CanFitInInt64()); @@ -71,19 +71,19 @@ public void TestCanFitInInt64() { [Test] public void TestCanFitInUInt64() { - Assert.IsTrue(CBORObject.FromObject(0).AsNumber().CanFitInUInt64(), "0"); + Assert.IsTrue(CBORObject.FromInt(0).AsNumber().CanFitInUInt64(), "0"); Assert.IsTrue( - CBORObject.FromObject(99).AsNumber().CanFitInUInt64(), + CBORObject.FromInt(99).AsNumber().CanFitInUInt64(), "99"); - Assert.IsTrue(CBORObject.FromObject(99.0).AsNumber().CanFitInUInt64(), + Assert.IsTrue(CBORObject.FromDouble(99.0).AsNumber().CanFitInUInt64(), "99.0"); Assert.IsTrue( - CBORObject.FromObject(1.0).AsNumber().CanFitInUInt64(), + CBORObject.FromDouble(1.0).AsNumber().CanFitInUInt64(), "99.0"); - Assert.IsTrue(CBORObject.FromObject(-0.0).AsNumber().CanFitInUInt64(), + Assert.IsTrue(CBORObject.FromDouble(-0.0).AsNumber().CanFitInUInt64(), "-0.0"); bool b = CBORObject.FromEInteger( @@ -91,18 +91,18 @@ public void TestCanFitInUInt64() { Assert.IsFalse(b); Assert.IsFalse( - CBORObject.FromObject(-99).AsNumber().CanFitInUInt64(), + CBORObject.FromInt(-99).AsNumber().CanFitInUInt64(), "-99"); - Assert.IsFalse(CBORObject.FromObject(-99.0).AsNumber().CanFitInUInt64(), + Assert.IsFalse(CBORObject.FromDouble(-99.0).AsNumber().CanFitInUInt64(), "-99.0"); Assert.IsFalse( - CBORObject.FromObject(0.1).AsNumber().CanFitInUInt64(), + CBORObject.FromDouble(0.1).AsNumber().CanFitInUInt64(), "0.1"); - Assert.IsFalse(CBORObject.FromObject(-0.1).AsNumber().CanFitInUInt64()); - Assert.IsFalse(CBORObject.FromObject(99.1).AsNumber().CanFitInUInt64()); - Assert.IsFalse(CBORObject.FromObject(-99.1).AsNumber().CanFitInUInt64()); + Assert.IsFalse(CBORObject.FromDouble(-0.1).AsNumber().CanFitInUInt64()); + Assert.IsFalse(CBORObject.FromDouble(99.1).AsNumber().CanFitInUInt64()); + Assert.IsFalse(CBORObject.FromDouble(-99.1).AsNumber().CanFitInUInt64()); Assert.IsFalse(CBORObject.PositiveInfinity.AsNumber().CanFitInUInt64()); Assert.IsFalse(CBORObject.NegativeInfinity.AsNumber().CanFitInUInt64()); Assert.IsFalse(CBORObject.NaN.AsNumber().CanFitInUInt64(), "NaN"); @@ -111,35 +111,35 @@ public void TestCanFitInUInt64() { [Test] public void TestCanTruncatedIntFitInUInt64() { Assert.IsTrue( - CBORObject.FromObject(0).AsNumber().CanTruncatedIntFitInUInt64(), + CBORObject.FromInt(0).AsNumber().CanTruncatedIntFitInUInt64(), "0"); Assert.IsTrue( - CBORObject.FromObject(99).AsNumber().CanTruncatedIntFitInUInt64(), + CBORObject.FromInt(99).AsNumber().CanTruncatedIntFitInUInt64(), "99"); Assert.IsTrue( - CBORObject.FromObject(99.0).AsNumber().CanTruncatedIntFitInUInt64(), + CBORObject.FromDouble(99.0).AsNumber().CanTruncatedIntFitInUInt64(), "99.0"); - Assert.IsTrue(CBORObject.FromObject( + Assert.IsTrue(CBORObject.FromDouble( -0.0).AsNumber().CanTruncatedIntFitInUInt64()); Assert.IsFalse( - CBORObject.FromObject(-99).AsNumber().CanTruncatedIntFitInUInt64()); + CBORObject.FromInt(-99).AsNumber().CanTruncatedIntFitInUInt64()); bool b = CBORObject.FromEInteger(EInteger.FromInt32(1).ShiftLeft(65)).AsNumber() .CanTruncatedIntFitInUInt64(); Assert.IsFalse(b); Assert.IsFalse( - CBORObject.FromObject( + CBORObject.FromDouble( -99.0).AsNumber().CanTruncatedIntFitInUInt64()); Assert.IsTrue( - CBORObject.FromObject(0.1).AsNumber().CanTruncatedIntFitInUInt64()); - Assert.IsTrue(CBORObject.FromObject( + CBORObject.FromDouble(0.1).AsNumber().CanTruncatedIntFitInUInt64()); + Assert.IsTrue(CBORObject.FromDouble( -0.1).AsNumber().CanTruncatedIntFitInUInt64()); - Assert.IsTrue(CBORObject.FromObject( + Assert.IsTrue(CBORObject.FromDouble( 99.1).AsNumber().CanTruncatedIntFitInUInt64()); Assert.IsFalse( @@ -154,8 +154,8 @@ public void TestCanTruncatedIntFitInUInt64() { [Test] public void TestIsInfinity() { - Assert.IsFalse(CBORObject.FromObject(0).AsNumber().IsInfinity()); - Assert.IsFalse(CBORObject.FromObject(99).AsNumber().IsInfinity()); + Assert.IsFalse(CBORObject.FromInt(0).AsNumber().IsInfinity()); + Assert.IsFalse(CBORObject.FromInt(99).AsNumber().IsInfinity()); Assert.IsTrue(CBORObject.PositiveInfinity.AsNumber().IsInfinity()); Assert.IsTrue(CBORObject.NegativeInfinity.AsNumber().IsInfinity()); Assert.IsFalse(CBORObject.NaN.AsNumber().IsInfinity()); @@ -163,8 +163,8 @@ public void TestIsInfinity() { [Test] public void TestIsNaN() { - Assert.IsFalse(CBORObject.FromObject(0).AsNumber().IsNaN()); - Assert.IsFalse(CBORObject.FromObject(99).AsNumber().IsNaN()); + Assert.IsFalse(CBORObject.FromInt(0).AsNumber().IsNaN()); + Assert.IsFalse(CBORObject.FromInt(99).AsNumber().IsNaN()); Assert.IsFalse(CBORObject.PositiveInfinity.AsNumber().IsNaN()); Assert.IsFalse(CBORObject.NegativeInfinity.AsNumber().IsNaN()); Assert.IsTrue(CBORObject.NaN.AsNumber().IsNaN()); diff --git a/CBORTest/CBORSupplementTest.cs b/CBORTest/CBORSupplementTest.cs index d2fb66da..5648dd0e 100644 --- a/CBORTest/CBORSupplementTest.cs +++ b/CBORTest/CBORSupplementTest.cs @@ -831,7 +831,7 @@ public void TestPodCompareTo() { cbor2 = CBORObject.NewMap().Add("aa", "Gg").Add("bb", "Jj").Add("cc", "Hh"); TestCommon.CompareTestEqual(cbor, cbor2); - cbor2 = CBORObject.FromObject(100); + cbor2 = CBORObject.FromInt(100); TestCommon.CompareTestGreater(cbor, cbor2); cbor2 = CBORObject.FromSimpleValue(10); TestCommon.CompareTestLess(cbor, cbor2); diff --git a/CBORTest/CBORTest.cs b/CBORTest/CBORTest.cs index 3fb27f21..93ce1975 100644 --- a/CBORTest/CBORTest.cs +++ b/CBORTest/CBORTest.cs @@ -304,7 +304,7 @@ public void TestCBORMapAdd() { Assert.IsTrue(cbor.ContainsKey(ToObjectTest.TestToFromObjectRoundTrip( "hello"))); Assert.AreEqual(2, cbor["hello"].AsInt32Value()); - _ = cbor.Set(1, 3); + _ = cbor.Set(1, CBORObject.FromInt(3)); CBORObject cborone = ToObjectTest.TestToFromObjectRoundTrip(1); Assert.IsTrue(cbor.ContainsKey(cborone)); Assert.AreEqual(3, cbor[cborone].AsInt32Value()); @@ -1042,7 +1042,7 @@ public static bool TestEquivJSONNumberOne(byte[] bytes) { cbor3 = CBORObject.FromJSONString(str, jsoptions); cbored = (ed.Exponent.CompareTo(0) == 0 && !(ed.IsNegative && ed.Sign == 0)) ? - CBORObject.FromEInteger(ed.Mantissa) : CBORObject.FromObject(ed); + CBORObject.FromEInteger(ed.Mantissa) : CBORObject.FromEDecimal(ed); Assert.AreEqual(cbor, cbor2, "[" + str + "] cbor2"); Assert.AreEqual(cbor, cbor3, "[" + str + "] cbor3"); Assert.AreEqual(cbor, cbored, "[" + str + "] cbored"); @@ -1071,7 +1071,7 @@ public static bool TestEquivJSONNumberDecimal128One(byte[] bytes) { cbor = CBORObject.FromJSONBytes(bytes, jsoptions); cbor2 = CBORDataUtilities.ParseJSONNumber(str, jsoptions); cbor3 = CBORObject.FromJSONString(str, jsoptions); - cbored = CBORObject.FromObject(ed); + cbored = CBORObject.FromEDecimal(ed); Assert.AreEqual(cbor, cbor2, "[" + str + "] cbor2"); Assert.AreEqual(cbor, cbor3, "[" + str + "] cbor3"); Assert.AreEqual(cbor, cbored, "[" + str + "] cbored"); @@ -1322,7 +1322,7 @@ public void TestCompareB() { AddSubCompare(objectTemp, objectTemp2); } var cbor = CBORObject.FromCBORObjectAndTag( - CBORObject.FromObject(double.NegativeInfinity), + CBORObject.FromDouble(double.NegativeInfinity), 1956611); CBORTestCommon.AssertRoundTrip(cbor); cbor = CBORObject.FromCBORObjectAndTag( @@ -1488,8 +1488,8 @@ public void TestDivide() { { ERational objectTemp = er; ERational objectTemp2; - CBORNumber cn = CBORObject.FromObject(o1).AsNumber() - .Divide(CBORObject.FromObject(o2).AsNumber()); + CBORNumber cn = o1.AsNumber() + .Divide(o2.AsNumber()); objectTemp2 = cn.ToERational(); TestCommon.CompareTestEqual(objectTemp, objectTemp2); } @@ -1498,11 +1498,11 @@ public void TestDivide() { [Test] public void TestCBORCompareTo() { - int cmp = CBORObject.FromObject(0).CompareTo(null); + int cmp = CBORObject.FromInt(0).CompareTo(null); if (cmp <= 0) { Assert.Fail(); } - cmp = CBORObject.FromObject(0).AsNumber().CompareTo(null); + cmp = CBORObject.FromInt(0).AsNumber().CompareTo(null); if (cmp <= 0) { Assert.Fail(); } @@ -2924,26 +2924,26 @@ public void TestOrderedMap() { list.Add(obj); } Assert.AreEqual(3, list.Count); - TestCommon.AssertEqualsHashCode(CBORObject.FromObject("a"), list[0]); - TestCommon.AssertEqualsHashCode(CBORObject.FromObject("b"), list[1]); - TestCommon.AssertEqualsHashCode(CBORObject.FromObject("c"), list[2]); + TestCommon.AssertEqualsHashCode(CBORObject.FromString("a"), list[0]); + TestCommon.AssertEqualsHashCode(CBORObject.FromString("b"), list[1]); + TestCommon.AssertEqualsHashCode(CBORObject.FromString("c"), list[2]); cbor = CBORObject.NewOrderedMap().Add("c", 1).Add("a", 2).Add("vv", 3); list = new List(); foreach (CBORObject obj in cbor.Keys) { list.Add(obj); } Assert.AreEqual(3, list.Count); - TestCommon.AssertEqualsHashCode(CBORObject.FromObject("c"), list[0]); - TestCommon.AssertEqualsHashCode(CBORObject.FromObject("a"), list[1]); - TestCommon.AssertEqualsHashCode(CBORObject.FromObject("vv"), list[2]); + TestCommon.AssertEqualsHashCode(CBORObject.FromString("c"), list[0]); + TestCommon.AssertEqualsHashCode(CBORObject.FromString("a"), list[1]); + TestCommon.AssertEqualsHashCode(CBORObject.FromString("vv"), list[2]); list = new List(); foreach (CBORObject obj in cbor.Values) { list.Add(obj); } Assert.AreEqual(3, list.Count); - TestCommon.AssertEqualsHashCode(CBORObject.FromObject(1), list[0]); - TestCommon.AssertEqualsHashCode(CBORObject.FromObject(2), list[1]); - TestCommon.AssertEqualsHashCode(CBORObject.FromObject(3), list[2]); + TestCommon.AssertEqualsHashCode(CBORObject.FromInt(1), list[0]); + TestCommon.AssertEqualsHashCode(CBORObject.FromInt(2), list[1]); + TestCommon.AssertEqualsHashCode(CBORObject.FromInt(3), list[2]); } [Test] @@ -4088,34 +4088,34 @@ public void TestIntegerFloatingEquivalence() { CBORObject cbor; // 0 versus 0.0 cbor = CBORObject.NewMap(); - _ = cbor.Set(0, CBORObject.FromObject("testzero")); - _ = cbor.Set(0.0, CBORObject.FromObject("testpointzero")); + _ = cbor.Set(0, CBORObject.FromString("testzero")); + _ = cbor.Set(CBORObject.FromDouble(0.0), CBORObject.FromString("testpointzero")); Assert.AreEqual(2, cbor.Count); { - string stringTemp = cbor[CBORObject.FromObject(0)].AsString(); + string stringTemp = cbor[CBORObject.FromInt(0)].AsString(); Assert.AreEqual( "testzero", stringTemp); } { - string stringTemp = cbor[CBORObject.FromObject( + string stringTemp = cbor[CBORObject.FromDouble( (double)0.0)].AsString(); Assert.AreEqual( "testpointzero", stringTemp); } cbor = CBORObject.NewMap(); - _ = cbor.Set(0.0, CBORObject.FromObject("testpointzero")); - _ = cbor.Set(0, CBORObject.FromObject("testzero")); + _ = cbor.Set(CBORObject.FromDouble(0.0), CBORObject.FromString("testpointzero")); + _ = cbor.Set(0, CBORObject.FromString("testzero")); Assert.AreEqual(2, cbor.Count); { - string stringTemp = cbor[CBORObject.FromObject(0)].AsString(); + string stringTemp = cbor[CBORObject.FromInt(0)].AsString(); Assert.AreEqual( "testzero", stringTemp); } { - string stringTemp = cbor[CBORObject.FromObject( + string stringTemp = cbor[CBORObject.FromDouble( (double)0.0)].AsString(); Assert.AreEqual( "testpointzero", @@ -4123,34 +4123,34 @@ public void TestIntegerFloatingEquivalence() { } // 3 versus 3.0 cbor = CBORObject.NewMap(); - _ = cbor.Set(3, CBORObject.FromObject("testzero")); - _ = cbor.Set(3.0, CBORObject.FromObject("testpointzero")); + _ = cbor.Set(3, CBORObject.FromString("testzero")); + _ = cbor.Set(CBORObject.FromDouble(3.0), CBORObject.FromString("testpointzero")); Assert.AreEqual(2, cbor.Count); { - string stringTemp = cbor[CBORObject.FromObject(3)].AsString(); + string stringTemp = cbor[CBORObject.FromInt(3)].AsString(); Assert.AreEqual( "testzero", stringTemp); } { - string stringTemp = cbor[CBORObject.FromObject( + string stringTemp = cbor[CBORObject.FromDouble( (double)3.0)].AsString(); Assert.AreEqual( "testpointzero", stringTemp); } cbor = CBORObject.NewMap(); - _ = cbor.Set(3.0, CBORObject.FromObject("testpointzero")); - _ = cbor.Set(3, CBORObject.FromObject("testzero")); + _ = cbor.Set(CBORObject.FromDouble(3.0), CBORObject.FromString("testpointzero")); + _ = cbor.Set(3, CBORObject.FromString("testzero")); Assert.AreEqual(2, cbor.Count); { - string stringTemp = cbor[CBORObject.FromObject(3)].AsString(); + string stringTemp = cbor[CBORObject.FromInt(3)].AsString(); Assert.AreEqual( "testzero", stringTemp); } { - string stringTemp = cbor[CBORObject.FromObject( + string stringTemp = cbor[CBORObject.FromDouble( (double)3.0)].AsString(); Assert.AreEqual( "testpointzero", @@ -4205,7 +4205,7 @@ public void TestBigNumberThresholds() { Assert.AreEqual(0, cbor.TagCount); } var ef = EFloat.Create(EInteger.One, eints[i]); - cbor = CBORObject.FromObject(ef); + cbor = CBORObject.FromEFloat(ef); Assert.IsTrue(cbor.IsNumber, cbor.ToString()); if (isPastCbor[i]) { Assert.IsTrue(cbor.HasOneTag(265)); @@ -4238,7 +4238,7 @@ public void TestBigNumberThresholds() { } } var ed = EDecimal.Create(EInteger.One, eints[i]); - cbor = CBORObject.FromObject(ed); + cbor = CBORObject.FromEDecimal(ed); Assert.IsTrue(cbor.IsNumber, cbor.ToString()); if (isPastCbor[i]) { Assert.IsTrue(cbor.HasOneTag(264)); @@ -4279,21 +4279,21 @@ public void TestRationalJSONSpecificA() { var er = ERational.FromString("1088692579850251977918382727683876451288883451475551838663907953515213777772897669/734154292316019508508581520803142368704146796235662433292652"); - _ = CBORObject.FromObject(er).ToJSONString(); + _ = CBORObject.FromERational(er).ToJSONString(); } [Test] public void TestRationalJSONSpecificB() { var er2 = ERational.FromString("1117037884940373468269515037592447741921166676191625235424/13699696515096285881634845839085271311137"); - _ = CBORObject.FromObject(er2).ToJSONString(); + _ = CBORObject.FromERational(er2).ToJSONString(); } [Test] public void TestRationalJSONSpecificC() { var er2 = ERational.FromString("42595158956667/1216724793801972483341765319799605241541780250657492435"); - _ = CBORObject.FromObject(er2).ToJSONString(); + _ = CBORObject.FromERational(er2).ToJSONString(); } [Test] @@ -4652,7 +4652,7 @@ public void TestRationalJsonString() { var er = ERational.Create( EInteger.FromString(s1), EInteger.FromString(s2)); - var cbor = CBORObject.FromObject(er); + var cbor = CBORObject.FromERational(er); _ = cbor.ToJSONString(); } @@ -4675,16 +4675,16 @@ public static bool CheckUtf16(string str) { [Test] public void TestWriteBasic() { var jsonop1 = new JSONOptions("writebasic=true"); - string json = CBORObject.FromObject("\uD800\uDC00").ToJSONString(jsonop1); + string json = CBORObject.FromString("\uD800\uDC00").ToJSONString(jsonop1); Assert.AreEqual("\"\\uD800\\uDC00\"", json); - json = CBORObject.FromObject("\u0800\u0C00").ToJSONString(jsonop1); + json = CBORObject.FromString("\u0800\u0C00").ToJSONString(jsonop1); Assert.AreEqual("\"\\u0800\\u0C00\"", json); - json = CBORObject.FromObject("\u0085\uFFFF").ToJSONString(jsonop1); + json = CBORObject.FromString("\u0085\uFFFF").ToJSONString(jsonop1); Assert.AreEqual("\"\\u0085\\uFFFF\"", json); var rg = new RandomGenerator(); for (int i = 0; i < 1000; ++i) { string rts = RandomObjects.RandomTextString(rg); - var cbor = CBORObject.FromObject(rts); + var cbor = CBORObject.FromString(rts); json = cbor.ToJSONString(jsonop1); // Check that the JSON contains only ASCII code points for (int j = 0; j < json.Length; ++j) { diff --git a/CBORTest/CBORTestCommon.cs b/CBORTest/CBORTestCommon.cs index 36648f94..2bfad891 100644 --- a/CBORTest/CBORTestCommon.cs +++ b/CBORTest/CBORTestCommon.cs @@ -99,7 +99,7 @@ public static CBORObject RandomNumberOrRational(IRandomGenExtended rand) { return CBORObject.FromEInteger( RandomObjects.RandomEInteger(rand)); case 3: - return CBORObject.FromObject( + return CBORObject.FromEFloat( RandomObjects.RandomEFloat(rand)); case 4: o = RandomObjects.RandomEDecimal(rand); @@ -225,9 +225,9 @@ public static CBORObject RandomCBORObject(IRandomGenExtended rand, int 4 => rand.GetInt32(2) == 0 ? CBORObject.True : CBORObject.False, 5 => rand.GetInt32(2) == 0 ? CBORObject.Null : CBORObject.Undefined, - 6 => CBORObject.FromObject( + 6 => CBORObject.FromString( RandomObjects.RandomTextString(rand)), - 7 => CBORObject.FromObject( + 7 => CBORObject.FromByteArray( RandomObjects.RandomByteString(rand)), 8 => RandomCBORArray(rand, depth), 9 => RandomCBORMap(rand, depth), diff --git a/CBORTest/JSONWithComments.cs b/CBORTest/JSONWithComments.cs index ad411526..e41b30e6 100644 --- a/CBORTest/JSONWithComments.cs +++ b/CBORTest/JSONWithComments.cs @@ -58,7 +58,7 @@ private CBORObject NextJSONString() { this.index = idx; return escaped ? CBORObject.FromJSONString(js[(startIndex - 1)..endIndex]) : - CBORObject.FromObject(js[startIndex..(endIndex - 1)]); + CBORObject.FromString(js[startIndex..(endIndex - 1)]); } else if (c == '\\') { this.index = idx++; escaped = true; From c38a76518186f666a2dc56408262fa12f40289f3 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Wed, 11 Oct 2023 19:18:26 +0100 Subject: [PATCH 19/29] clean up internal naming --- CBOR/PeterO/Cbor/CBORNumber.cs | 73 +++++++++++++++++----------------- CBOR/PeterO/Cbor/CBORObject.cs | 4 +- 2 files changed, 38 insertions(+), 39 deletions(-) diff --git a/CBOR/PeterO/Cbor/CBORNumber.cs b/CBOR/PeterO/Cbor/CBORNumber.cs index 44bf2546..6ce161e2 100644 --- a/CBOR/PeterO/Cbor/CBORNumber.cs +++ b/CBOR/PeterO/Cbor/CBORNumber.cs @@ -246,7 +246,7 @@ private static CBORNumber RationalToNumber( default: return null; // "Invalid options"); } } - return CBORNumber.FromObject(erat); + return CBORNumber.FromERational(erat); } private static bool CheckRationalToNumber( @@ -455,8 +455,7 @@ private static CBORNumber BigFracToNumber( default: return null; // "Invalid options"); } } - return isdec ? CBORNumber.FromObject(edec) : -CBORNumber.FromObject(efloat); + return isdec ? FromEDecimal(edec) : FromEFloat(efloat); } /// Gets the underlying form of this CBOR number @@ -648,7 +647,7 @@ public byte ToByteIfExact() { /// number. public static CBORNumber FromByte(byte inputByte) { int val = inputByte & 0xff; - return FromObject((long)val); + return FromInt64((long)val); } /// Converts this number's value to a 16-bit signed integer if @@ -695,7 +694,7 @@ public short ToInt16IfExact() { /// number. public static CBORNumber FromInt16(short inputInt16) { int val = inputInt16; - return FromObject((long)val); + return FromInt64((long)val); } /// Converts this number's value to a 32-bit signed integer if @@ -902,25 +901,25 @@ internal string ToJSONString() { } } - internal static CBORNumber FromObject(int intValue) { + internal static CBORNumber FromInt(int intValue) { return new CBORNumber(NumberKind.Integer, (long)intValue); } - internal static CBORNumber FromObject(long longValue) { + internal static CBORNumber FromInt64(long longValue) { return new CBORNumber(NumberKind.Integer, longValue); } internal static CBORNumber FromDoubleBits(long doubleBits) { return new CBORNumber(NumberKind.Double, doubleBits); } - internal static CBORNumber FromObject(EInteger eivalue) { + internal static CBORNumber FromEInteger(EInteger eivalue) { return new CBORNumber(NumberKind.EInteger, eivalue); } - internal static CBORNumber FromObject(EFloat value) { + internal static CBORNumber FromEFloat(EFloat value) { return new CBORNumber(NumberKind.EFloat, value); } - internal static CBORNumber FromObject(EDecimal value) { + internal static CBORNumber FromEDecimal(EDecimal value) { return new CBORNumber(NumberKind.EDecimal, value); } - internal static CBORNumber FromObject(ERational value) { + internal static CBORNumber FromERational(ERational value) { return new CBORNumber(NumberKind.ERational, value); } @@ -1020,7 +1019,7 @@ public CBORNumber Abs() { { var longValue = (long)this.value; return longValue == long.MinValue ? - FromObject(EInteger.FromInt64(longValue).Negate()) : + FromEInteger(EInteger.FromInt64(longValue).Negate()) : longValue >= 0 ? this : new CBORNumber( this.Kind, Math.Abs(longValue)); @@ -1028,7 +1027,7 @@ public CBORNumber Abs() { case NumberKind.EInteger: { var eivalue = (EInteger)this.value; - return eivalue.Sign >= 0 ? this : FromObject(eivalue.Abs()); + return eivalue.Sign >= 0 ? this : FromEInteger(eivalue.Abs()); } default: return new CBORNumber(this.Kind, @@ -1045,16 +1044,16 @@ public CBORNumber Negate() { case NumberKind.Integer: { var longValue = (long)this.value; - return longValue == 0 ? FromObject(EDecimal.NegativeZero) : + return longValue == 0 ? FromEDecimal(EDecimal.NegativeZero) : longValue == long.MinValue ? -FromObject(EInteger.FromInt64(longValue).Negate()) : new +FromEInteger(EInteger.FromInt64(longValue).Negate()) : new CBORNumber(this.Kind, -longValue); } case NumberKind.EInteger: { var eiValue = (EInteger)this.value; - return eiValue.IsZero ? FromObject(EDecimal.NegativeZero) : -FromObject(eiValue.Negate()); + return eiValue.IsZero ? FromEDecimal(EDecimal.NegativeZero) : +FromEInteger(eiValue.Negate()); } default: return new CBORNumber(this.Kind, @@ -1133,7 +1132,7 @@ public CBORNumber Add(CBORNumber b) { if ((valueA < 0 && valueB < long.MinValue - valueA) || (valueA > 0 && valueB > long.MaxValue - valueA)) { // would overflow, convert to EInteger - return CBORNumber.FromObject( + return CBORNumber.FromEInteger( EInteger.FromInt64(valueA).Add(EInteger.FromInt64(valueB))); } return new CBORNumber(NumberKind.Integer, valueA + valueB); @@ -1207,7 +1206,7 @@ public CBORNumber Subtract(CBORNumber b) { if ((valueB < 0 && long.MaxValue + valueB < valueA) || (valueB > 0 && long.MinValue + valueB > valueA)) { // would overflow, convert to EInteger - return CBORNumber.FromObject( + return CBORNumber.FromEInteger( EInteger.FromInt64(valueA).Subtract(EInteger.FromInt64( valueB))); } @@ -1283,20 +1282,20 @@ public CBORNumber Multiply(CBORNumber b) { // would overflow, convert to EInteger var bvalueA = (EInteger)valueA; var bvalueB = (EInteger)valueB; - return CBORNumber.FromObject(bvalueA * bvalueB); + return CBORNumber.FromEInteger(bvalueA * bvalueB); } - return CBORNumber.FromObject(valueA * valueB); + return CBORNumber.FromInt64(valueA * valueB); } NumberKind convertKind = GetConvertKind(a, b); if (convertKind == NumberKind.ERational) { ERational e1 = GetNumberInterface(typeA).AsERational(objA); ERational e2 = GetNumberInterface(typeB).AsERational(objB); - return CBORNumber.FromObject(CheckOverflow(e1, e2, e1.Multiply(e2))); + return CBORNumber.FromERational(CheckOverflow(e1, e2, e1.Multiply(e2))); } if (convertKind == NumberKind.EDecimal) { EDecimal e1 = GetNumberInterface(typeA).AsEDecimal(objA); EDecimal e2 = GetNumberInterface(typeB).AsEDecimal(objB); - return CBORNumber.FromObject(CheckOverflow(e1, e2, e1.Multiply(e2))); + return CBORNumber.FromEDecimal(CheckOverflow(e1, e2, e1.Multiply(e2))); } if (convertKind == NumberKind.EFloat) { EFloat e1 = GetNumberInterface(typeA).AsEFloat(objA); @@ -1338,10 +1337,10 @@ public CBORNumber Divide(CBORNumber b) { var valueA = (long)objA; var valueB = (long)objB; if (valueB == 0) { - return (valueA == 0) ? CBORNumber.FromObject(EDecimal.NaN) : - ((valueA < 0) ? CBORNumber.FromObject(EDecimal.NegativeInfinity) : + return (valueA == 0) ? FromEDecimal(EDecimal.NaN) : + ((valueA < 0) ? FromEDecimal(EDecimal.NegativeInfinity) : - CBORNumber.FromObject(EDecimal.PositiveInfinity)); + FromEDecimal(EDecimal.PositiveInfinity)); } if (valueA == long.MinValue && valueB == -1) { return new CBORNumber(NumberKind.Integer, valueA).Negate(); @@ -1388,13 +1387,13 @@ public CBORNumber Divide(CBORNumber b) { EFloat e1 = GetNumberInterface(typeA).AsEFloat(objA); EFloat e2 = GetNumberInterface(typeB).AsEFloat(objB); if (e1.IsZero && e2.IsZero) { - return CBORNumber.FromObject(EDecimal.NaN); + return CBORNumber.FromEDecimal(EDecimal.NaN); } EFloat eret = e1.Divide(e2, null); // If either operand is infinity or NaN, the result // is already exact. Likewise if the result is a finite number. if (!e1.IsFinite || !e2.IsFinite || eret.IsFinite) { - return CBORNumber.FromObject(eret); + return CBORNumber.FromEFloat(eret); } ERational er1 = GetNumberInterface(typeA).AsERational(objA); ERational er2 = GetNumberInterface(typeB).AsERational(objB); @@ -1409,9 +1408,9 @@ public CBORNumber Divide(CBORNumber b) { EInteger b1 = GetNumberInterface(typeA).AsEInteger(objA); EInteger b2 = GetNumberInterface(typeB).AsEInteger(objB); if (b2.IsZero) { - return b1.IsZero ? CBORNumber.FromObject(EDecimal.NaN) : ((b1.Sign < - 0) ? CBORNumber.FromObject(EDecimal.NegativeInfinity) : - CBORNumber.FromObject(EDecimal.PositiveInfinity)); + return b1.IsZero ? CBORNumber.FromEDecimal(EDecimal.NaN) : ((b1.Sign < + 0) ? CBORNumber.FromEDecimal(EDecimal.NegativeInfinity) : + CBORNumber.FromEDecimal(EDecimal.PositiveInfinity)); } EInteger bigrem; EInteger bigquo; @@ -1420,7 +1419,7 @@ public CBORNumber Divide(CBORNumber b) { bigquo = divrem[0]; bigrem = divrem[1]; } - return bigrem.IsZero ? CBORNumber.FromObject(bigquo) : + return bigrem.IsZero ? CBORNumber.FromEInteger(bigquo) : new CBORNumber(NumberKind.ERational, ERational.Create(b1, b2)); } } @@ -1448,31 +1447,31 @@ public CBORNumber Remainder(CBORNumber b) { var valueA = (long)objA; var valueB = (long)objB; return (valueA == long.MinValue && valueB == -1) ? - CBORNumber.FromObject(0) : CBORNumber.FromObject(valueA % valueB); + CBORNumber.FromInt(0) : CBORNumber.FromInt64(valueA % valueB); } NumberKind convertKind = GetConvertKind(this, b); if (convertKind == NumberKind.ERational) { ERational e1 = GetNumberInterface(typeA).AsERational(objA); ERational e2 = GetNumberInterface(typeB).AsERational(objB); - return CBORNumber.FromObject(CheckOverflow(e1, e2, e1.Remainder(e2))); + return CBORNumber.FromERational(CheckOverflow(e1, e2, e1.Remainder(e2))); } if (convertKind == NumberKind.EDecimal) { EDecimal e1 = GetNumberInterface(typeA).AsEDecimal(objA); EDecimal e2 = GetNumberInterface(typeB).AsEDecimal(objB); - return CBORNumber.FromObject(CheckOverflow(e1, e2, e1.Remainder(e2, + return CBORNumber.FromEDecimal(CheckOverflow(e1, e2, e1.Remainder(e2, null))); } if (convertKind == NumberKind.EFloat) { EFloat e1 = GetNumberInterface(typeA).AsEFloat(objA); EFloat e2 = GetNumberInterface(typeB).AsEFloat(objB); - return CBORNumber.FromObject(CheckOverflow(e1, e2, e1.Remainder(e2, + return CBORNumber.FromEFloat(CheckOverflow(e1, e2, e1.Remainder(e2, null))); } else { // DebugUtility.Log("type=" + typeA + "/" + typeB + " finite=" + // (// this.IsFinite()) + "/" + (b.IsFinite())); EInteger b1 = GetNumberInterface(typeA).AsEInteger(objA); EInteger b2 = GetNumberInterface(typeB).AsEInteger(objB); - return CBORNumber.FromObject(b1 % b2); + return CBORNumber.FromEInteger(b1 % b2); } } diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index 38d61ee4..c56d921b 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -36,7 +36,7 @@ namespace PeterO.Cbor { /// 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 + /// CBORObject.From[Type] 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 @@ -2865,7 +2865,7 @@ internal static CBORObject FromObject( return FromUShort((ushort)obj); } if (obj is decimal) { - return FromObject((decimal)obj); + return FromDecimal((decimal)obj); } if (obj is double) { return FromDouble((double)obj); From 63b87c93e1d23748b646a5145210908ebd2d1c31 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Wed, 11 Oct 2023 23:19:15 +0100 Subject: [PATCH 20/29] re-add FromObjectAndTag as obsolete --- CBOR/PeterO/Cbor/CBORObject.cs | 111 ++++++++++++++++++++++++--------- 1 file changed, 83 insertions(+), 28 deletions(-) diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index c56d921b..92b49ed6 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -2328,37 +2328,58 @@ public static CBORObject FromInt(int value) { /// The parameter is a /// 16-bit signed integer. /// A CBOR object generated from the given integer. - public static CBORObject FromObject(short value) { + public static CBORObject FromInt16(short value) { return value >= 0 && value < 24 ? FixedObjects[value] : (value >= -24 && value < 0) ? FixedObjects[0x20 - (value + 1)] : FromInt64((long)value); } + /// Generates a CBOR object from a 16-bit signed + /// integer. + /// A 16-bit signed integer. + /// A CBOR object generated from the given integer. + [Obsolete("Use FromInt16 instead.")] + public static CBORObject FromObject(short value) => FromInt16(value); + /// Returns the CBOR true value or false value, depending on /// "value". /// Either true or false. /// CBORObject.True if value is true; otherwise /// CBORObject.False. - public static CBORObject FromObject(bool value) { - return value ? CBORObject.True : CBORObject.False; + public static CBORObject FromBool(bool value) { + return value ? True : False; } + /// Returns the CBOR true value or false value, depending on + /// "value". + /// Either true or false. + /// CBORObject.True if value is true; otherwise + /// CBORObject.False. + [Obsolete("Use FromBool instead.")] + public static CBORObject FromObject(bool value) => FromBool(value); + /// Generates a CBOR object from a byte (0 to 255). /// The parameter is a /// byte (from 0 to 255). /// A CBOR object generated from the given integer. - public static CBORObject FromObject(byte value) { + public static CBORObject FromByte(byte value) { return FromInt(value & 0xff); } + /// Generates a CBOR object from a byte. + /// A byte. + /// A CBOR object. + [Obsolete("Use FromByte instead.")] + public static CBORObject FromObject(byte value) => FromByte(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 + /// CBORObject.FromSingle(Single.NaN) or + /// CBORObject.FromFloat(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 @@ -2366,18 +2387,25 @@ public static CBORObject FromObject(byte value) { /// The parameter is a /// 32-bit floating-point number. /// A CBOR object generated from the given number. - public static CBORObject FromObject(float value) { + public static CBORObject FromFloat(float value) { long doubleBits = CBORUtilities.SingleToDoublePrecision( CBORUtilities.SingleToInt32Bits(value)); return new CBORObject(CBORObjectTypeDouble, doubleBits); } + /// Generates a CBOR object from a 32-bit floating-point + /// number. + /// A 32-bit floating-point number. + /// A CBOR object. + [Obsolete("Use FromFloat instead.")] + public static CBORObject FromObject(float value) => FromFloat(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 + /// CBORObject.FromDouble(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 @@ -2428,9 +2456,9 @@ public static CBORObject FromByteArray(byte[] bytes) { /// 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 CBORObject FromObject(CBORObject[] array) { + public static CBORObject FromCBORArray(CBORObject[] array) { if (array == null) { - return CBORObject.Null; + return Null; } IList list = new List(); foreach (CBORObject cbor in array) { @@ -2439,6 +2467,13 @@ public static CBORObject FromObject(CBORObject[] array) { return new CBORObject(CBORObjectTypeArray, list); } + /// Generates a CBOR object from an array of CBOR + /// objects. + /// An array of CBOR objects. + /// A CBOR object. + [Obsolete("Use FromCBORArray instead.")] + public static CBORObject FromObject(CBORObject[] array) => FromCBORArray(array); + internal static CBORObject FromArrayBackedObject(CBORObject[] array) { if (array == null) { return CBORObject.Null; @@ -2838,19 +2873,19 @@ internal static CBORObject FromObject( return FromERational(erf); } if (obj is short) { - return FromObject((short)obj); + return FromInt16((short)obj); } if (obj is char) { return FromInt((int)(char)obj); } if (obj is bool) { - return FromObject((bool)obj); + return FromBool((bool)obj); } if (obj is byte) { - return FromObject((byte)obj); + return FromByte((byte)obj); } if (obj is float) { - return FromObject((float)obj); + return FromFloat((float)obj); } if (obj is sbyte) { return FromSbyte((sbyte)obj); @@ -3027,7 +3062,16 @@ public static CBORObject FromCBORObjectAndTag( cborObj.WithTag(bigintTag); } - // TODO: reAdd FromObjectAndTag as deprecated + /// 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). + /// An arbitrary object, which can be null. + /// Tag number. + /// A CBOR object where the object + /// is given the tag . + [Obsolete("Use FromCBORObjectAndTag instead.")] + public static CBORObject FromObjectAndTag(object valueObValue, EInteger bigintTag) => + FromCBORObjectAndTag(FromObject(valueObValue), bigintTag); /// Generates a CBOR object from an arbitrary object and gives /// the resulting object a tag in addition to its existing tags (the @@ -3082,7 +3126,19 @@ public static CBORObject FromCBORObjectAndTag( return smallTag < 0 ? throw new ArgumentException("smallTag(" + smallTag + ") is less than 0") : cborObj.WithTag(smallTag); } - // TODO: reAdd FromObjectAndTag as deprecated + + /// 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). + /// The parameter, an arbitrary object, which can be null. + /// A 32-bit integer that specifies a tag number. + /// A CBOR object where the object is given the + /// tag . + /// The parameter is less than 0. + [Obsolete("Use FromCBORObjectAndTag instead.")] + public static CBORObject FromObjectAndTag(object valueObValue, int smallTag) => + FromCBORObjectAndTag(FromObject(valueObValue), smallTag); /// Creates a CBOR object from a simple value /// number. @@ -3682,7 +3738,7 @@ public static void Write( /// 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. + /// CBORObject.FromEFloat(EFloat) and then written out. /// An arbitrary-precision binary floating-point /// number. Can be null. /// A writable data stream. @@ -3724,7 +3780,7 @@ public static void Write(EFloat bignum, 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. + /// CBORObject.FromERational(ERational) and then written out. /// An arbitrary-precision rational number. Can /// be null. /// A writable data stream. @@ -3755,7 +3811,7 @@ public static void Write(ERational rational, 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. + /// CBORObject.FromEDecimal(EDecimal) and then written out. /// The arbitrary-precision decimal number to /// write. Can be null. /// Stream to write to. @@ -4110,8 +4166,7 @@ public static void Write(object objValue, Stream stream) { /// 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. + /// Any object accepted by the FromObject method. /// The arbitrary object to be serialized. Can /// be null. /// NOTE: For security reasons, whenever possible, an @@ -4245,13 +4300,13 @@ public CBORObject Add(object key, object valueOb) { mapKey = CBORObject.Null; } else { mapKey = key as CBORObject; - mapKey = mapKey ?? CBORObject.FromObject(key); + mapKey = mapKey ?? FromObject(key); } if (valueOb == null) { - mapValue = CBORObject.Null; + mapValue = Null; } else { mapValue = valueOb as CBORObject; - mapValue = mapValue ?? CBORObject.FromObject(valueOb); + mapValue = mapValue ?? FromObject(valueOb); } IDictionary map = this.AsMap(); if (map.ContainsKey(mapKey)) { @@ -4327,7 +4382,7 @@ public CBORObject Add(CBORObject obj) { public CBORObject Add(object obj) { if (this.Type == CBORType.Array) { IList list = this.AsList(); - list.Add(CBORObject.FromObject(obj)); + list.Add(FromObject(obj)); return this; } throw new InvalidOperationException("Not an array"); @@ -4583,11 +4638,11 @@ public float AsSingle() { /// conversion, use the following idiom (originally written in C# for /// the.NET version): (cbor == null || cbor.IsNull) ? null : /// cbor.AsString(). - /// This method is not the "reverse" of the FromObject - /// method in the sense that FromObject can take either a text string + /// This method is not the "reverse" of the FromString + /// method in the sense that FromString 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: + /// FromString 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 From c821cdecde90024110890384bf440d192c7acf94 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Wed, 11 Oct 2023 23:23:00 +0100 Subject: [PATCH 21/29] FromCBORArray --- CBORTest/CBORTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CBORTest/CBORTest.cs b/CBORTest/CBORTest.cs index 93ce1975..276170c0 100644 --- a/CBORTest/CBORTest.cs +++ b/CBORTest/CBORTest.cs @@ -2092,7 +2092,7 @@ public static CBORObject ReferenceTestObject(int nests) { CBORObject refobj; for (int i = 0; i <= nests; ++i) { refobj = CBORObject.FromCBORObjectAndTag(CBORObject.FromInt(i), 29); - arr = CBORObject.FromObject(new CBORObject[] { + arr = CBORObject.FromCBORArray(new CBORObject[] { refobj, refobj, refobj, refobj, refobj, refobj, refobj, refobj, refobj, }); From 596d858e4807fe7ba0ca9a7afce087f74682c590 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Wed, 11 Oct 2023 23:24:58 +0100 Subject: [PATCH 22/29] duplicate annotations --- CBOR/PeterO/Cbor/CBORObject.cs | 2 +- CBOR/PeterO/Cbor/PropertyMap.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index 92b49ed6..98706315 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -1811,7 +1811,7 @@ internal EDecimal ToEDecimal() { [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] internal object ToObject( - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t, + Type t, CBORTypeMapper mapper, PODOptions options, int depth) { diff --git a/CBOR/PeterO/Cbor/PropertyMap.cs b/CBOR/PeterO/Cbor/PropertyMap.cs index 8459f404..082f167c 100644 --- a/CBOR/PeterO/Cbor/PropertyMap.cs +++ b/CBOR/PeterO/Cbor/PropertyMap.cs @@ -932,7 +932,7 @@ private static object TypeToIntegerObject(CBORObject objThis, Type t) { [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static object TypeToObject( CBORObject objThis, - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t, + Type t, CBORTypeMapper mapper, PODOptions options, int depth) { @@ -1316,7 +1316,7 @@ public static CBORObject FromObjectOther(object obj) { [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static object ObjectWithProperties( - [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] Type t, + Type t, IEnumerable> keysValues, CBORTypeMapper mapper, PODOptions options, From 3cdd1fc2d6268e0727879f183d98f1b1d7ac623e Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Thu, 12 Oct 2023 18:48:49 +0100 Subject: [PATCH 23/29] revert unintentional change --- CBOR/PeterO/Cbor/CBORObject.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index 98706315..5a799022 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -1847,7 +1847,8 @@ internal object ToObject( // might throw InvalidOperationException rather than CBORException. // Make them throw CBORException in next major version. if (t.Equals(typeof(EDecimal))) { - this.ToEDecimal(); + CBORNumber cn = this.AsNumber(); + return cn.GetNumberInterface().AsEDecimal(cn.GetValue()); } if (t.Equals(typeof(EFloat))) { var cn = CBORNumber.FromCBORObject(this); From 536897a51ab921aa6538a5175ac4756c5d11b278 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Thu, 12 Oct 2023 19:12:36 +0100 Subject: [PATCH 24/29] wip --- CBOR/PeterO/Cbor/CBORObject.cs | 4 +++- CBOR/PeterO/Cbor/PropertyMap.cs | 10 +++++----- CBOR/PeterO/DebugUtility.cs | 7 ++++--- 3 files changed, 12 insertions(+), 9 deletions(-) diff --git a/CBOR/PeterO/Cbor/CBORObject.cs b/CBOR/PeterO/Cbor/CBORObject.cs index 5a799022..bac2e294 100644 --- a/CBOR/PeterO/Cbor/CBORObject.cs +++ b/CBOR/PeterO/Cbor/CBORObject.cs @@ -2827,7 +2827,7 @@ public static CBORObject FromObject( ArgumentNullException(nameof(mapper)) : FromObject(obj, options, mapper, 0); } - [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] // Annotation comes from use of GetProperties + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] internal static CBORObject FromObject( object obj, PODOptions options, @@ -3071,6 +3071,7 @@ public static CBORObject FromCBORObjectAndTag( /// A CBOR object where the object /// is given the tag . [Obsolete("Use FromCBORObjectAndTag instead.")] + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static CBORObject FromObjectAndTag(object valueObValue, EInteger bigintTag) => FromCBORObjectAndTag(FromObject(valueObValue), bigintTag); @@ -3138,6 +3139,7 @@ public static CBORObject FromCBORObjectAndTag( /// The parameter is less than 0. [Obsolete("Use FromCBORObjectAndTag instead.")] + [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static CBORObject FromObjectAndTag(object valueObValue, int smallTag) => FromCBORObjectAndTag(FromObject(valueObValue), smallTag); diff --git a/CBOR/PeterO/Cbor/PropertyMap.cs b/CBOR/PeterO/Cbor/PropertyMap.cs index 082f167c..7010ee5a 100644 --- a/CBOR/PeterO/Cbor/PropertyMap.cs +++ b/CBOR/PeterO/Cbor/PropertyMap.cs @@ -474,8 +474,8 @@ private static IEnumerable GetTypeInterfaces(Type t) { private static MethodInfo GetTypeMethod( Type t, string name, - Type parameter) { - return t.GetRuntimeMethod(name, new[] { parameter }); + Type[] parameters) { + return t.GetRuntimeMethod(name, parameters); } private static bool HasCustomAttribute( @@ -867,10 +867,10 @@ public static IDictionary NewOrderedDict() { [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static object FindOneArgumentMethod( - Type objType, + object obj, string name, Type argtype) { - return GetTypeMethod(objType, name, argtype); + return GetTypeMethod(obj.GetType(), name, new[] { argtype }); } public static object InvokeOneArgumentMethod( @@ -1151,7 +1151,7 @@ public static object TypeToObject( #endif if (objThis.Type == CBORType.Array && genericListObject != null) { object addMethod = FindOneArgumentMethod( - genericListObject.GetType(), + genericListObject, "Add", objectType); if (addMethod == null) { diff --git a/CBOR/PeterO/DebugUtility.cs b/CBOR/PeterO/DebugUtility.cs index 1e67d212..b499dae7 100644 --- a/CBOR/PeterO/DebugUtility.cs +++ b/CBOR/PeterO/DebugUtility.cs @@ -28,12 +28,12 @@ public static void SetWriter(Action wr) { private static MethodInfo GetTypeMethod( Type t, string name, - Type parameter) { + Type[] parameters) { #if NET40 || NET20 return t.GetMethod(name, new[] { parameter }); #else { - return t?.GetRuntimeMethod(name, new[] { parameter }); + return t?.GetRuntimeMethod(name, parameters); } #endif } @@ -63,7 +63,8 @@ public static void Log(string str) { #endif } } - MethodInfo typeMethod = GetTypeMethod(type, "WriteLine", typeof(string)); + Type[] types = new[] { typeof(string) }; + MethodInfo typeMethod = GetTypeMethod(type, "WriteLine", types); if (typeMethod != null) { typeMethod.Invoke( type, From 7e736ec74ecf2a86e88e759a3084204cc0dc408e Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Thu, 12 Oct 2023 22:19:52 +0100 Subject: [PATCH 25/29] redo Type arguments --- CBOR/PeterO/Cbor/PropertyMap.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CBOR/PeterO/Cbor/PropertyMap.cs b/CBOR/PeterO/Cbor/PropertyMap.cs index 7010ee5a..6ca567e3 100644 --- a/CBOR/PeterO/Cbor/PropertyMap.cs +++ b/CBOR/PeterO/Cbor/PropertyMap.cs @@ -867,10 +867,10 @@ public static IDictionary NewOrderedDict() { [RequiresUnreferencedCode("Do not use in AOT or reflection-free contexts.")] public static object FindOneArgumentMethod( - object obj, + Type ty, string name, Type argtype) { - return GetTypeMethod(obj.GetType(), name, new[] { argtype }); + return GetTypeMethod(ty, name, new[] { argtype }); } public static object InvokeOneArgumentMethod( @@ -1151,7 +1151,7 @@ public static object TypeToObject( #endif if (objThis.Type == CBORType.Array && genericListObject != null) { object addMethod = FindOneArgumentMethod( - genericListObject, + genericListObject.GetType(), "Add", objectType); if (addMethod == null) { From 1289d23523083656afae4eb593afad1236d4966c Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Thu, 12 Oct 2023 22:45:49 +0100 Subject: [PATCH 26/29] fix tests --- CBOR/PeterO/Cbor/CBORTypeMapper.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CBOR/PeterO/Cbor/CBORTypeMapper.cs b/CBOR/PeterO/Cbor/CBORTypeMapper.cs index 475ea387..7eb9edd5 100644 --- a/CBOR/PeterO/Cbor/CBORTypeMapper.cs +++ b/CBOR/PeterO/Cbor/CBORTypeMapper.cs @@ -54,7 +54,7 @@ public CBORTypeMapper AddConverter( { Converter = converter, ToObject = PropertyMap.FindOneArgumentMethod( - typeof(ICBORConverter), + converter.GetType(), "ToCBORObject", type), }; @@ -63,7 +63,7 @@ public CBORTypeMapper AddConverter( "Converter doesn't contain a proper ToCBORObject method"); } ci.FromObject = PropertyMap.FindOneArgumentMethod( - typeof(ICBORConverter), + converter.GetType(), "FromCBORObject", typeof(CBORObject)); this.converters[type] = ci; From bd687a9f16800c3a569093eaf302501287150238 Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Thu, 12 Oct 2023 22:47:38 +0100 Subject: [PATCH 27/29] net6 --- CBOR/CBOR.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CBOR/CBOR.csproj b/CBOR/CBOR.csproj index 0d3f9bea..271dc4af 100644 --- a/CBOR/CBOR.csproj +++ b/CBOR/CBOR.csproj @@ -1,7 +1,7 @@  - netstandard1.0; net7.0 + netstandard1.0; net6.0 True 5.0-alpha1 Peter Occil From 5181f5b01761f8b218e7a825f4a09bc4fd262c9d Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Thu, 12 Oct 2023 22:54:51 +0100 Subject: [PATCH 28/29] back to 7 --- CBOR/CBOR.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CBOR/CBOR.csproj b/CBOR/CBOR.csproj index 271dc4af..0d3f9bea 100644 --- a/CBOR/CBOR.csproj +++ b/CBOR/CBOR.csproj @@ -1,7 +1,7 @@  - netstandard1.0; net6.0 + netstandard1.0; net7.0 True 5.0-alpha1 Peter Occil From 5038a942023ec97c30739f119a969f432f24217a Mon Sep 17 00:00:00 2001 From: Charles Roddie Date: Thu, 12 Oct 2023 22:59:23 +0100 Subject: [PATCH 29/29] update yml dotnet --- .github/workflows/Test.yml | 4 ++-- .github/workflows/codeql-analysis.yml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/Test.yml b/.github/workflows/Test.yml index 89416995..c9ac0342 100644 --- a/.github/workflows/Test.yml +++ b/.github/workflows/Test.yml @@ -16,9 +16,9 @@ jobs: with: submodules: 'recursive' - name: Setup .NET Core - uses: actions/setup-dotnet@v1 + uses: actions/setup-dotnet@v3 with: - dotnet-version: '6.0' + dotnet-version: '7.0' - name: Test run: | dotnet add CBORTest package Microsoft.NET.Test.Sdk # Update is required for GitHubActionsTestLogger to print anything diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 3ef5d44c..b502d7f9 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -52,9 +52,9 @@ jobs: # queries: ./path/to/local/query, your-org/your-repo/queries@main - name: Setup .NET Core - uses: actions/setup-dotnet@v1 + uses: actions/setup-dotnet@v3 with: - dotnet-version: '6.0' + dotnet-version: '7.0' - name: Autobuild uses: github/codeql-action/autobuild@v2