Skip to content

Commit

Permalink
.NET 2 compat
Browse files Browse the repository at this point in the history
  • Loading branch information
peteroupc committed Dec 7, 2021
1 parent a7d878c commit 773f741
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 47 deletions.
20 changes: 15 additions & 5 deletions CBOR/PeterO/Cbor/PropertyMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1096,17 +1096,21 @@ public static object TypeToObject(
isList = td.Equals(typeof(List<>)) || td.Equals(typeof(IList<>)) ||
td.Equals(typeof(ICollection<>)) ||
td.Equals(typeof(IEnumerable<>));
#if NET20
isReadOnlyCollection = false;
#else
isReadOnlyCollection = (td.Equals(typeof(IReadOnlyCollection<>)) ||
td.Equals(typeof(IReadOnlyList<>)) ||

td.Equals(
typeof(System.Collections.ObjectModel.ReadOnlyCollection<>)))
&&
t.GenericTypeArguments.Length == 1;
t.GetGenericArguments().Length == 1;
#endif
}
isList = isList && t.GetGenericArguments().Length == 1;
if (isReadOnlyCollection) {
objectType = t.GenericTypeArguments[0];
objectType = t.GetGenericArguments()[0];
Type listType = typeof(List<>).MakeGenericType(objectType);
listObject = Activator.CreateInstance(listType);
} else if (isList) {
Expand Down Expand Up @@ -1191,7 +1195,7 @@ public static object TypeToObject(
ie.Add(value.ToObject(objectType, mapper, options, depth + 1));
}
if (isReadOnlyCollection) {
objectType = t.GenericTypeArguments[0];
objectType = FirstGenericArgument(t);
Type rocType =
typeof(System.Collections.ObjectModel.ReadOnlyCollection<>)
.MakeGenericType(objectType);
Expand All @@ -1212,10 +1216,14 @@ public static object TypeToObject(
Type td = t.GetGenericTypeDefinition();
isDict = td.Equals(typeof(Dictionary<,>)) ||
td.Equals(typeof(IDictionary<,>));
#if NET20
isReadOnlyDict = false;
#else
isReadOnlyDict =
(td.Equals(typeof(System.Collections.ObjectModel.ReadOnlyDictionary<,>)) ||
td.Equals(typeof(IReadOnlyDictionary<,>))) &&
t.GetGenericArguments().Length == 2;
#endif
}
// DebugUtility.Log("list=" + isDict);
isDict = isDict && t.GetGenericArguments().Length == 2;
Expand Down Expand Up @@ -1267,14 +1275,16 @@ public static object TypeToObject(
key.ToObject(keyType, mapper, options, depth + 1),
value.ToObject(valueType, mapper, options, depth + 1));
}
#if !NET20
if (isReadOnlyDict) {
Type listType =
typeof(
System.Collections.ObjectModel.ReadOnlyDictionary<,>).MakeGenericType(
keyType,
System.Collections.ObjectModel.ReadOnlyDictionary<,>)
.MakeGenericType(keyType,
valueType);
dictObject = Activator.CreateInstance(listType, dictObject);
}
#endif
return dictObject;
}
if (mapper != null) {
Expand Down
13 changes: 0 additions & 13 deletions CBOR20/CBOR20.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Condition="Exists('../packages/Microsoft.CodeAnalysis.NetAnalyzers.5.0.3/build/Microsoft.CodeAnalysis.NetAnalyzers.props')" Project="../packages/Microsoft.CodeAnalysis.NetAnalyzers.5.0.3/build/Microsoft.CodeAnalysis.NetAnalyzers.props" />
<Import Condition="Exists('../packages/Microsoft.NetFramework.Analyzers.3.3.0/build/Microsoft.NetFramework.Analyzers.props')" Project="../packages/Microsoft.NetFramework.Analyzers.3.3.0/build/Microsoft.NetFramework.Analyzers.props" />
<Import Condition="Exists('../packages/Microsoft.NetCore.Analyzers.3.3.0/build/Microsoft.NetCore.Analyzers.props')" Project="../packages/Microsoft.NetCore.Analyzers.3.3.0/build/Microsoft.NetCore.Analyzers.props" />
<Import Condition="Exists('../packages/Microsoft.CodeQuality.Analyzers.3.3.0/build/Microsoft.CodeQuality.Analyzers.props')" Project="../packages/Microsoft.CodeQuality.Analyzers.3.3.0/build/Microsoft.CodeQuality.Analyzers.props" />
<Import Condition="Exists('../packages/Microsoft.CodeAnalysis.VersionCheckAnalyzer.3.3.0/build/Microsoft.CodeAnalysis.VersionCheckAnalyzer.props')" Project="../packages/Microsoft.CodeAnalysis.VersionCheckAnalyzer.3.3.0/build/Microsoft.CodeAnalysis.VersionCheckAnalyzer.props" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand Down Expand Up @@ -216,13 +211,5 @@
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('../packages/Microsoft.CodeAnalysis.VersionCheckAnalyzer.3.3.0/build/Microsoft.CodeAnalysis.VersionCheckAnalyzer.props')" Text="$([System.String]::Format('$(ErrorText)', '../packages/Microsoft.CodeAnalysis.VersionCheckAnalyzer.3.3.0/build/Microsoft.CodeAnalysis.VersionCheckAnalyzer.props'))" />
<Error Condition="!Exists('../packages/Microsoft.CodeQuality.Analyzers.3.3.0/build/Microsoft.CodeQuality.Analyzers.props')" Text="$([System.String]::Format('$(ErrorText)', '../packages/Microsoft.CodeQuality.Analyzers.3.3.0/build/Microsoft.CodeQuality.Analyzers.props'))" />
<Error Condition="!Exists('../packages/Microsoft.NetCore.Analyzers.3.3.0/build/Microsoft.NetCore.Analyzers.props')" Text="$([System.String]::Format('$(ErrorText)', '../packages/Microsoft.NetCore.Analyzers.3.3.0/build/Microsoft.NetCore.Analyzers.props'))" />
<Error Condition="!Exists('../packages/Microsoft.NetFramework.Analyzers.3.3.0/build/Microsoft.NetFramework.Analyzers.props')" Text="$([System.String]::Format('$(ErrorText)', '../packages/Microsoft.NetFramework.Analyzers.3.3.0/build/Microsoft.NetFramework.Analyzers.props'))" />
<Error Condition="!Exists('../packages/Microsoft.CodeAnalysis.NetAnalyzers.5.0.3/build/Microsoft.CodeAnalysis.NetAnalyzers.props')" Text="$([System.String]::Format('$(ErrorText)', '../packages/Microsoft.CodeAnalysis.NetAnalyzers.5.0.3/build/Microsoft.CodeAnalysis.NetAnalyzers.props'))" />
<Error Condition="!Exists('../packages/Microsoft.CodeAnalysis.NetAnalyzers.5.0.3/build/Microsoft.CodeAnalysis.NetAnalyzers.props')" Text="$([System.String]::Format('$(ErrorText)', '../packages/Microsoft.CodeAnalysis.NetAnalyzers.5.0.3/build/Microsoft.CodeAnalysis.NetAnalyzers.props'))" />
<Error Condition="!Exists('../packages/Microsoft.CodeAnalysis.NetAnalyzers.5.0.3/build/Microsoft.CodeAnalysis.NetAnalyzers.targets')" Text="$([System.String]::Format('$(ErrorText)', '../packages/Microsoft.CodeAnalysis.NetAnalyzers.5.0.3/build/Microsoft.CodeAnalysis.NetAnalyzers.targets'))" />
</Target>
<Import Condition="Exists('../packages/Microsoft.CodeAnalysis.NetAnalyzers.5.0.3/build/Microsoft.CodeAnalysis.NetAnalyzers.targets')" Project="../packages/Microsoft.CodeAnalysis.NetAnalyzers.5.0.3/build/Microsoft.CodeAnalysis.NetAnalyzers.targets" />
</Project>
6 changes: 3 additions & 3 deletions CBOR20/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Reflection;
[assembly: System.CLSCompliant(true)]
[assembly: AssemblyInformationalVersion("4.4.2")]
[assembly: AssemblyVersion("4.4.2.0")]
[assembly: AssemblyFileVersion("4.4.2.0")]
[assembly: AssemblyInformationalVersion("4.5")]
[assembly: AssemblyVersion("4.5.0")]
[assembly: AssemblyFileVersion("4.5.0")]
[assembly: AssemblyProduct("CBOR (Concise Binary Object Representati" +
"on)")]
[assembly: AssemblyTitle("CBOR (Concise Binary Object Representati" +
Expand Down
7 changes: 0 additions & 7 deletions CBOR20/packages.config
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.CodeAnalysis.NetAnalyzers" version="5.0.3" targetFramework="net20" developmentDependency="true" />
<package id="Microsoft.CodeAnalysis.VersionCheckAnalyzer" version="3.3.2" targetFramework="net20" developmentDependency="true" />
<package id="Microsoft.CodeQuality.Analyzers" version="3.3.2" targetFramework="net20" developmentDependency="true" />
<package id="Microsoft.NetCore.Analyzers" version="3.3.2" targetFramework="net20" developmentDependency="true" />
<package id="Microsoft.NetFramework.Analyzers" version="3.3.2" targetFramework="net20" developmentDependency="true" />
<package id="PeterO.Numbers" targetFramework="net20" version="1.8.2" />
<package id="StyleCop.Analyzers" version="1.2.0-beta.354" targetFramework="net20" developmentDependency="true" />
<package id="StyleCop.Analyzers.Unstable" version="1.2.0.354" targetFramework="net20" developmentDependency="true" />
</packages>
2 changes: 1 addition & 1 deletion docs/PeterO.Cbor.CBORDateConverter.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ Tries to extract the fields of a date and time in the form of a CBOR object.

* <i>obj</i>: A CBOR object that specifies a date/time according to the conversion type used to create this date converter.

* <i>year</i>: An array whose first element will store the year. The array's length must be 1 or greater. If this function fails, the year is set to null.
* <i>year</i>: Will store the year. If this function fails, the year is set to null.

* <i>lesserFields</i>: An array that will store the fields (other than the year) of the date and time. The array's length must be 7 or greater. If this function fails, the first seven elements are set to 0. For more information, see the (EInteger[], int) overload of this method.

Expand Down
14 changes: 13 additions & 1 deletion docs/PeterO.Cbor.CBOREncodeOptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Specifies options for encoding and decoding CBOR objects.
* <code>[public static readonly PeterO.Cbor.CBOREncodeOptions Default;](#Default)</code> - Default options for CBOR objects.
* <code>[public static readonly PeterO.Cbor.CBOREncodeOptions DefaultCtap2Canonical;](#DefaultCtap2Canonical)</code> - Default options for CBOR objects serialized using the CTAP2 canonicalization (used in Web Authentication, among other specifications).
* <code>[Float64](#Float64)</code> - Gets a value indicating whether to encode floating-point numbers in a CBOR object in their 64-bit encoding form regardless of whether their value can be encoded without loss in a smaller form.
* <code>[KeepKeyOrder](#KeepKeyOrder)</code> - Gets a value indicating whether to preserve the order in which a CBOR map's keys appear when decoding a CBOR object, by using maps created as though by CBORObject.
* <code>[ResolveReferences](#ResolveReferences)</code> - Gets a value indicating whether to resolve references to sharable objects and sharable strings in the process of decoding a CBOR object.
* <code>[ToString()](#ToString)</code> - Gets the values of this options object's properties in text form.
* <code>[UseIndefLengthStrings](#UseIndefLengthStrings)</code> - Gets a value indicating whether to encode strings with an indefinite-length encoding under certain circumstances.
Expand Down Expand Up @@ -62,7 +63,7 @@ Initializes a new instance of the [PeterO.Cbor.CBOREncodeOptions](PeterO.Cbor.CB

<b>Parameters:</b>

* <i>paramString</i>: A string setting forth the options to use. This is a semicolon-separated list of options, each of which has a key and a value separated by an equal sign ("="). Whitespace and line separators are not allowed to appear between the semicolons or between the equal signs, nor may the string begin or end with whitespace. The string can be empty, but cannot be null. The following is an example of this parameter: `allowduplicatekeys=true;ctap2Canonical=true` . The key can be any one of the following where the letters can be any combination of basic upper-case and/or basic lower-case letters: `allowduplicatekeys` , `ctap2canonical` , `resolvereferences` , `useindeflengthstrings` , `allowempty` , `float64` . Keys other than these are ignored in this version of the CBOR library. The key `float64` was introduced in version 4.4 of this library. (Keys are compared using a basic case-insensitive comparison, in which two strings are equal if they match after converting the basic upper-case letters A to Z (U+0041 to U+005A) in both strings to basic lower-case letters.) If two or more key/value pairs have equal keys (in a basic case-insensitive comparison), the value given for the last such key is used. The four keys just given can have a value of `1` , `true` , `yes` , or `on` (where the letters can be any combination of basic upper-case and/or basic lower-case letters), which means true, and any other value meaning false. For example, `allowduplicatekeys=Yes` and `allowduplicatekeys=1` both set the `AllowDuplicateKeys` property to true. In the future, this class may allow other keys to store other kinds of values, not just true or false.
* <i>paramString</i>: A string setting forth the options to use. This is a semicolon-separated list of options, each of which has a key and a value separated by an equal sign ("="). Whitespace and line separators are not allowed to appear between the semicolons or between the equal signs, nor may the string begin or end with whitespace. The string can be empty, but cannot be null. The following is an example of this parameter: `allowduplicatekeys=true;ctap2Canonical=true` . The key can be any one of the following where the letters can be any combination of basic upper-case and/or basic lower-case letters: `allowduplicatekeys` , `ctap2canonical` , `resolvereferences` , `useindeflengthstrings` , `allowempty` , `float64` , `keepkeyorder` . Keys other than these are ignored in this version of the CBOR library. The key `float64` was introduced in version 4.4 of this library. The key `keepkeyorder` was introduced in version 4.5 of this library.(Keys are compared using a basic case-insensitive comparison, in which two strings are equal if they match after converting the basic upper-case letters A to Z (U+0041 to U+005A) in both strings to basic lower-case letters.) If two or more key/value pairs have equal keys (in a basic case-insensitive comparison), the value given for the last such key is used. The four keys just given can have a value of `1` , `true` , `yes` , or `on` (where the letters can be any combination of basic upper-case and/or basic lower-case letters), which means true, and any other value meaning false. For example, `allowduplicatekeys=Yes` and `allowduplicatekeys=1` both set the `AllowDuplicateKeys` property to true. In the future, this class may allow other keys to store other kinds of values, not just true or false.

<b>Exceptions:</b>

Expand Down Expand Up @@ -141,6 +142,17 @@ Gets a value indicating whether to encode floating-point numbers in a CBOR objec

Gets a value indicating whether to encode floating-point numbers in a CBOR object in their 64-bit encoding form regardless of whether their value can be encoded without loss in a smaller form. Used only when encoding CBOR objects. The default is false.

<a id="KeepKeyOrder"></a>
### KeepKeyOrder

public bool KeepKeyOrder { get; }

Gets a value indicating whether to preserve the order in which a CBOR map's keys appear when decoding a CBOR object, by using maps created as though by CBORObject.NewOrderedMap. If false, key order is not guaranteed to be preserved when decoding CBOR.

<b>Returns:</b>

A value indicating whether to preserve the order in which a CBOR map's keys appear when decoding a CBOR object. The default is false.

<a id="ResolveReferences"></a>
### ResolveReferences

Expand Down
32 changes: 16 additions & 16 deletions docs/PeterO.Cbor.CBORObject.md
Original file line number Diff line number Diff line change
Expand Up @@ -4281,22 +4281,22 @@ A byte array containing the converted in JSON format.
The example code given below (originally written in C# for the.NET version) can be used to write out certain keys of a CBOR map in a given order to a JSON string.

/* Generates a JSON string of 'mapObj' whose keys are in the order
given
in 'keys' . Only keys found in 'keys' will be written if they exist in
'mapObj'. */ private static string KeysToJSONMap(CBORObject mapObj,
IList<CBORObject> keys) { if (mapObj == null) { throw new
ArgumentNullException)nameof(mapObj));}
if (keys == null) { throw new
ArgumentNullException)nameof(keys));}
if (obj.Type != CBORType.Map) {
throw new ArgumentException("'obj' is not a map."); } StringBuilder
builder = new StringBuilder(); var first = true; builder.Append("{");
for (CBORObject key in keys) { if (mapObj.ContainsKey(key)) { if
(!first) {builder.Append(", ");} var keyString=(key.CBORType ==
CBORType.String) ? key.AsString() : key.ToJSONString();
builder.Append(CBORObject.FromObject(keyString) .ToJSONString())
.Append(":").Append(mapObj[key].ToJSONString()); first=false; } } return
builder.Append("}").ToString(); }
given
in 'keys' . Only keys found in 'keys' will be written if they exist in
'mapObj'. */ private static string KeysToJSONMap(CBORObject mapObj,
IList<CBORObject> keys) { if (mapObj == null) { throw new
ArgumentNullException)nameof(mapObj));}
if (keys == null) { throw new
ArgumentNullException)nameof(keys));}
if (obj.Type != CBORType.Map) {
throw new ArgumentException("'obj' is not a map."); } StringBuilder
builder = new StringBuilder(); var first = true; builder.Append("{");
for (CBORObject key in keys) { if (mapObj.ContainsKey(key)) { if
(!first) {builder.Append(", ");} var keyString=(key.CBORType ==
CBORType.String) ? key.AsString() : key.ToJSONString();
builder.Append(CBORObject.FromObject(keyString) .ToJSONString())
.Append(":").Append(mapObj[key].ToJSONString()); first=false; } } return
builder.Append("}").ToString(); }

.

Expand Down
Loading

0 comments on commit 773f741

Please sign in to comment.