Skip to content

Commit 58bf085

Browse files
committed
Version 4.2.0
1 parent 7eb0f54 commit 58bf085

9 files changed

Lines changed: 148 additions & 127 deletions

File tree

CBOR.nuspec

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
<package
2-
><metadata><version>4.1.0</version><id>PeterO.Cbor</id><requireLicenseAcceptance>false</requireLicenseAcceptance><releaseNotes>Version 4.1:
2+
><metadata><version>4.2.0</version><id>PeterO.Cbor</id><requireLicenseAcceptance>false</requireLicenseAcceptance><releaseNotes>Version 4.2:
33

4-
- JSONOptions string constructor now sets ReplaceSurrogates to false by default (previously, it was inadvertently true).</releaseNotes><summary></summary><license type='expression'>CC0-1.0</license><projectUrl>https://github.com/peteroupc/CBOR</projectUrl><authors>Peter Occil</authors><description>A C# implementation of Concise Binary Object Representation (CBOR), a general-purpose binary data format defined in RFC 7049.</description><owners>Peter Occil</owners><title>CBOR (Concise Binary Object Representation)</title><tags>cbor data serialization binary json</tags><dependencies><group><dependency id='PeterO.URIUtility' version='1.0.0' /><dependency id='PeterO.Numbers' version='1.5.1' /></group></dependencies></metadata><files><file src='CBOR/bin/Release/netstandard1.0/CBOR.dll' target='/lib/netstandard1.0' /><file src='CBOR/bin/Release/netstandard1.0/CBOR.xml' target='/lib/netstandard1.0' /><file src='CBOR20/bin/Release/CBOR.dll' target='/lib/net20' /><file src='CBOR20/bin/Release/CBOR.xml' target='/lib/net20' /><file src='CBOR40/bin/Release/CBOR.dll' target='/lib/net40' /><file src='CBOR40/bin/Release/CBOR.xml' target='/lib/net40' /></files></package
5-
>
4+
- Some arithmetic methods in CBORNumber do basic overflow checks.
5+
- Add char array and byte array overloads to ParseJSONNumber
6+
- Support implementations of IList in CBORObject deserialization
7+
- Internally, the code avoids storing doubles (64-bit floating-point numbers) directly in CBORNumbers, uses sorted maps rather than hash tables in some CBOR objects, and can now store text strings as UTF-8 byte arrays. This could help avoid unnecessary string conversions in many case.
8+
- Bug fixes and performance improvements
9+
- Now uses Numbers library version 1.7.3</releaseNotes><summary></summary><license type='expression'>CC0-1.0</license><projectUrl>https://github.com/peteroupc/CBOR</projectUrl><authors>Peter Occil</authors><description>A C# implementation of Concise Binary Object Representation (CBOR), a general-purpose binary data format defined in RFC 7049.</description><owners>Peter Occil</owners><title>CBOR (Concise Binary Object Representation)</title><tags>cbor data serialization binary json</tags><dependencies><group targetFramework='.NETStandard1.0'><dependency id='PeterO.URIUtility' version='1.0.0' /><dependency id='PeterO.Numbers' version='1.7.3' /></group><group targetFramework='.NETFramework2.0'><dependency id='PeterO.URIUtility' version='1.0.0' /><dependency id='PeterO.Numbers' version='1.7.3' /></group><group targetFramework='.NETFramework4.0'><dependency id='PeterO.URIUtility' version='1.0.0' /><dependency id='PeterO.Numbers' version='1.7.3' /></group></dependencies></metadata><files><file src='CBOR/bin/Release/netstandard1.0/CBOR.dll' target='/lib/netstandard1.0' /><file src='CBOR/bin/Release/netstandard1.0/CBOR.xml' target='/lib/netstandard1.0' /><file src='CBOR20/bin/Release/CBOR.dll' target='/lib/net20' /><file src='CBOR20/bin/Release/CBOR.xml' target='/lib/net20' /><file src='CBOR40/bin/Release/CBOR.dll' target='/lib/net40' /><file src='CBOR40/bin/Release/CBOR.xml' target='/lib/net40' /></files></package
10+
>

CBOR/PeterO/Cbor/PropertyMap.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ public void SetValue(object obj, object value) {
101101
}
102102

103103
#if NET20 || NET40
104-
public static bool HasUsableGetter (PropertyInfo pi) {
104+
public static bool HasUsableGetter(PropertyInfo pi) {
105105
return pi != null && pi.CanRead && !pi.GetGetMethod().IsStatic &&
106106
pi.GetGetMethod().IsPublic;
107107
}
108108

109-
public static bool HasUsableSetter (PropertyInfo pi) {
109+
public static bool HasUsableSetter(PropertyInfo pi) {
110110
return pi != null && pi.CanWrite && !pi.GetSetMethod().IsStatic &&
111111
pi.GetSetMethod().IsPublic;
112112
}
@@ -166,47 +166,47 @@ public MemberInfo Prop {
166166
}
167167

168168
#if NET40 || NET20
169-
private static bool IsGenericType (Type type) {
169+
private static bool IsGenericType(Type type) {
170170
return type.IsGenericType;
171171
}
172172

173-
private static bool IsClassOrValueType (Type type) {
173+
private static bool IsClassOrValueType(Type type) {
174174
return type.IsClass || type.IsValueType;
175175
}
176176

177-
private static Type FirstGenericArgument (Type type) {
177+
private static Type FirstGenericArgument(Type type) {
178178
return type.GetGenericArguments()[0];
179179
}
180180

181-
private static IEnumerable<PropertyInfo> GetTypeProperties (Type t) {
182-
return t.GetProperties (BindingFlags.Public |
181+
private static IEnumerable<PropertyInfo> GetTypeProperties(Type t) {
182+
return t.GetProperties(BindingFlags.Public |
183183
BindingFlags.Instance);
184184
}
185185

186-
private static IEnumerable<FieldInfo> GetTypeFields (Type t) {
187-
return t.GetFields (BindingFlags.Public | BindingFlags.Instance);
186+
private static IEnumerable<FieldInfo> GetTypeFields(Type t) {
187+
return t.GetFields(BindingFlags.Public | BindingFlags.Instance);
188188
}
189189

190-
private static IEnumerable<Type> GetTypeInterfaces (Type t) {
190+
private static IEnumerable<Type> GetTypeInterfaces(Type t) {
191191
return t.GetInterfaces();
192192
}
193193

194-
private static bool IsAssignableFrom (Type superType, Type subType) {
195-
return superType.IsAssignableFrom (subType);
194+
private static bool IsAssignableFrom(Type superType, Type subType) {
195+
return superType.IsAssignableFrom(subType);
196196
}
197197

198198
private static MethodInfo GetTypeMethod(
199199
Type t,
200200
string name,
201201
Type[] parameters) {
202-
return t.GetMethod (name, parameters);
202+
return t.GetMethod(name, parameters);
203203
}
204204

205205
private static bool HasCustomAttribute(
206206
Type t,
207207
string name) {
208-
foreach (var attr in t.GetCustomAttributes (false)) {
209-
if (attr.GetType().FullName.Equals (name,
208+
foreach (var attr in t.GetCustomAttributes(false)) {
209+
if (attr.GetType().FullName.Equals(name,
210210
StringComparison.Ordinal)) {
211211
return true;
212212
}
@@ -784,11 +784,11 @@ public static object TypeToObject(
784784
object listObject = null;
785785
object genericListObject = null;
786786
#if NET40 || NET20
787-
if (IsAssignableFrom (typeof(Array), t)) {
787+
if (IsAssignableFrom(typeof(Array), t)) {
788788
Type elementType = t.GetElementType();
789789
Array array = Array.CreateInstance(
790790
elementType,
791-
GetDimensions (objThis));
791+
GetDimensions(objThis));
792792
return FillArray(
793793
array,
794794
elementType,
@@ -799,15 +799,15 @@ public static object TypeToObject(
799799
}
800800
if (t.IsGenericType) {
801801
Type td = t.GetGenericTypeDefinition();
802-
isList = td.Equals (typeof(List<>)) || td.Equals (typeof(IList<>)) ||
803-
td.Equals (typeof(ICollection<>)) ||
804-
td.Equals (typeof(IEnumerable<>));
802+
isList = td.Equals(typeof(List<>)) || td.Equals(typeof(IList<>)) ||
803+
td.Equals(typeof(ICollection<>)) ||
804+
td.Equals(typeof(IEnumerable<>));
805805
}
806806
isList = isList && t.GetGenericArguments().Length == 1;
807807
if (isList) {
808808
objectType = t.GetGenericArguments()[0];
809-
Type listType = typeof(List<>).MakeGenericType (objectType);
810-
listObject = Activator.CreateInstance (listType);
809+
Type listType = typeof(List<>).MakeGenericType(objectType);
810+
listObject = Activator.CreateInstance(listType);
811811
}
812812
#else
813813
if (IsAssignableFrom(typeof(Array), t)) {
@@ -896,8 +896,8 @@ public static object TypeToObject(
896896
isDict = t.IsGenericType;
897897
if (t.IsGenericType) {
898898
Type td = t.GetGenericTypeDefinition();
899-
isDict = td.Equals (typeof(Dictionary<,>)) ||
900-
td.Equals (typeof(IDictionary<,>));
899+
isDict = td.Equals(typeof(Dictionary<,>)) ||
900+
td.Equals(typeof(IDictionary<,>));
901901
}
902902
// DebugUtility.Log("list=" + isDict);
903903
isDict = isDict && t.GetGenericArguments().Length == 2;
@@ -908,7 +908,7 @@ public static object TypeToObject(
908908
Type listType = typeof(Dictionary<,>).MakeGenericType(
909909
keyType,
910910
valueType);
911-
dictObject = Activator.CreateInstance (listType);
911+
dictObject = Activator.CreateInstance(listType);
912912
}
913913
#else
914914
isDict = t.GetTypeInfo().IsGenericType;

CBOR20/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System.Reflection;
22
[assembly: System.CLSCompliant(true)]
3-
[assembly: AssemblyInformationalVersion("4.1.0")]
4-
[assembly: AssemblyVersion("4.1.0.0")]
5-
[assembly: AssemblyFileVersion("4.1.0.0")]
3+
[assembly: AssemblyInformationalVersion("4.2.0")]
4+
[assembly: AssemblyVersion("4.2.0.0")]
5+
[assembly: AssemblyFileVersion("4.2.0.0")]
66
[assembly: AssemblyProduct("CBOR (Concise Binary Object Representati" +
77
"on)")]
88
[assembly: AssemblyTitle("CBOR (Concise Binary Object Representati" +

CBOR40/Properties/AssemblyInfo.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System.Reflection;
22
[assembly: System.CLSCompliant(true)]
3-
[assembly: AssemblyInformationalVersion("4.1.0")]
4-
[assembly: AssemblyVersion("4.1.0.0")]
5-
[assembly: AssemblyFileVersion("4.1.0.0")]
3+
[assembly: AssemblyInformationalVersion("4.2.0")]
4+
[assembly: AssemblyVersion("4.2.0.0")]
5+
[assembly: AssemblyFileVersion("4.2.0.0")]
66
[assembly: AssemblyProduct("CBOR (Concise Binary Object Representati" +
77
"on)")]
88
[assembly: AssemblyTitle("CBOR (Concise Binary Object Representati" +

CBORTest/CBORExtraTest.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,25 @@ public enum CustomBits {
172172
C = 4,
173173
}
174174

175+
[System.Diagnostics.CodeAnalysis.SuppressMessage(
176+
"Microsoft.Design",
177+
"CA1034",
178+
Justification = "Testing whether serialization works " + "on nested public types")]
175179
public sealed class CustomCollectionContainer {
180+
[System.Diagnostics.CodeAnalysis.SuppressMessage(
181+
"Microsoft.Usage",
182+
"CA2227",
183+
Justification = "Testing whether serialization works " + "on public properties of nested public types")]
176184
public CustomCollection CList {
177185
get;
178186
set;
179187
}
180188
}
181189

190+
[System.Diagnostics.CodeAnalysis.SuppressMessage(
191+
"Microsoft.Design",
192+
"CA1034",
193+
Justification = "Testing whether serialization works " + "on nested public types")]
182194
public sealed class CustomCollection : IList<CustomEnum> {
183195
private List<CustomEnum> w = new List<CustomEnum>();
184196

CBORTest/CBORObjectTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7317,7 +7317,6 @@ public void TestFromJsonStringLongSpecific1() {
73177317
}
73187318

73197319
[Test]
7320-
[Timeout(5000)]
73217320
public void TestFromJsonStringFastCases() {
73227321
var op = new JSONOptions("numberconversion=double");
73237322
Assert.AreEqual(

CBORTest/CBORTest.cs

Lines changed: 64 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -2240,7 +2240,6 @@ private static string ToByteArrayStringFrom(byte[] array, int pos) {
22402240
public void TestRandomNonsense() {
22412241
var rand = new RandomGenerator();
22422242
for (var i = 0; i < 1000; ++i) {
2243-
Console.WriteLine(i + ": " + DateTime.UtcNow);
22442243
var array = new byte[rand.UniformInt(100000) + 1];
22452244
rand.GetBytes(array, 0, array.Length);
22462245
TestRandomOne(array);
@@ -2274,16 +2273,13 @@ public static void TestRandomOne(byte[] array) {
22742273
long oldPos = inputStream.Position;
22752274
o = CBORObject.Read(inputStream);
22762275
long cborlen = inputStream.Position - oldPos;
2277-
if (cborlen > 3000) {
2278-
Console.WriteLine("pos=" + inputStream.Position + " of " +
2279-
inputStream.Length + ", cborlen=" + cborlen);
2280-
}
2276+
// if (cborlen > 3000) {
2277+
// Console.WriteLine("pos=" + inputStream.Position + " of " +
2278+
// inputStream.Length + ", cborlen=" + cborlen);
2279+
// }
22812280
byte[] encodedBytes = (o == null) ? null : o.EncodeToBytes();
22822281
try {
22832282
CBORObject.DecodeFromBytes(encodedBytes);
2284-
if (cborlen > 3000) {
2285-
Console.WriteLine("end DecodeFromBytes");
2286-
}
22872283
} catch (Exception ex) {
22882284
throw new InvalidOperationException(ex.Message, ex);
22892285
}
@@ -2294,29 +2290,14 @@ public static void TestRandomOne(byte[] array) {
22942290
}
22952291
if (o != null) {
22962292
try {
2297-
if (cborlen > 3000) {
2298-
Console.WriteLine("toJSONString " + DateTime.UtcNow);
2299-
}
23002293
jsonString = o.ToJSONString();
2301-
if (cborlen > 3000) {
2302-
Console.WriteLine("jsonStringLen = " + jsonString.Length);
2303-
}
23042294
} catch (CBORException ex) {
23052295
Console.WriteLine(ex.Message);
23062296
jsonString = String.Empty;
23072297
}
23082298
if (jsonString.Length > 0) {
2309-
if (cborlen > 3000) {
2310-
Console.WriteLine("fromJSONString " + DateTime.UtcNow);
2311-
}
23122299
CBORObject.FromJSONString(jsonString);
2313-
if (cborlen > 3000) {
2314-
Console.WriteLine("writeToJSON " + DateTime.UtcNow);
2315-
}
23162300
TestWriteToJSON(o);
2317-
if (cborlen > 3000) {
2318-
Console.WriteLine("endJSON " + DateTime.UtcNow);
2319-
}
23202301
}
23212302
}
23222303
} catch (Exception ex) {
@@ -2740,66 +2721,66 @@ public void TestAsNumberAddSubtract() {
27402721
public static bool TestAsNumberMultiplyDivideOne(
27412722
CBORObject o1,
27422723
CBORObject o2) {
2743-
if (o1 == null) {
2744-
throw new ArgumentNullException(nameof(o1));
2745-
}
2746-
if (o2 == null) {
2747-
throw new ArgumentNullException(nameof(o2));
2748-
}
2749-
if (!o1.IsNumber || !o2.IsNumber) {
2750-
return false;
2751-
}
2752-
byte[] eb1 = o1.EncodeToBytes();
2753-
byte[] eb2 = o2.EncodeToBytes();
2754-
CBORTestCommon.AssertRoundTrip(o1);
2755-
CBORTestCommon.AssertRoundTrip(o2);
2756-
CBORNumber on1 = o1.AsNumber();
2757-
CBORNumber on2 = o2.AsNumber();
2758-
CBORNumber onSum = null;
2759-
try {
2760-
onSum = on1.Multiply(on2);
2761-
} catch (OutOfMemoryException) {
2762-
return false;
2763-
}
2764-
if (!onSum.IsFinite()) {
2765-
// Console.WriteLine("on1=" + o1);
2766-
// Console.WriteLine("on2=" + o2);
2767-
return false;
2768-
}
2769-
// Console.WriteLine(i+"");
2770-
// Console.WriteLine(i+" "+Chop(o1.ToString()));
2771-
// Console.WriteLine(i+" "+Chop(o2.ToString()));
2772-
// Console.WriteLine(i + " " + Chop(onSum.ToString()));
2773-
if (!onSum.IsFinite()) {
2774-
Assert.Fail("onSum is not finite\n" +
2775-
"o1=" + TestCommon.ToByteArrayString(eb1) + "\n" +
2776-
"o2=" + TestCommon.ToByteArrayString(eb2) + "\n");
2777-
}
2778-
CBORNumber on2a = onSum.Divide(on1);
2779-
// NOTE: Ignore if divisor is zero
2780-
if (!on1.IsZero() && !on2a.IsFinite()) {
2781-
Assert.Fail("on2a is not finite\n" +
2782-
"o1=" + TestCommon.ToByteArrayString(eb1) + "\n" +
2783-
"o2=" + TestCommon.ToByteArrayString(eb2) + "\n");
2784-
}
2785-
if (!on1.IsZero() && !on2.IsZero()) {
2786-
VerifyEqual(on2a, on2, o1, o2);
2787-
}
2788-
CBORNumber on1a = onSum.Divide(on2);
2789-
// NOTE: Ignore if divisor is zero
2790-
if (!on2.IsZero() && !on1a.IsFinite()) {
2791-
Assert.Fail("on1a is not finite\n" +
2792-
"o1=" + on1 + "\n" + "o2=" + on2 + "\n" +
2793-
"{\nbyte[] bytes1 = " + TestCommon.ToByteArrayString(eb1) + ";\n" +
2794-
"byte[] bytes2 =" + TestCommon.ToByteArrayString(eb2) + ";\n" +
2795-
"TestAsNumberMultiplyDivideOne(\nCBORObject.D" +
2796-
"ecodeFromBytes(bytes1),\n" +
2797-
"CBORObject.DecodeFromBytes(bytes2));\n}\n");
2798-
}
2799-
if (!on1.IsZero() && !on2.IsZero()) {
2800-
VerifyEqual(on1a, on1, o1, o2);
2801-
}
2802-
return true;
2724+
if (o1 == null) {
2725+
throw new ArgumentNullException(nameof(o1));
2726+
}
2727+
if (o2 == null) {
2728+
throw new ArgumentNullException(nameof(o2));
2729+
}
2730+
if (!o1.IsNumber || !o2.IsNumber) {
2731+
return false;
2732+
}
2733+
byte[] eb1 = o1.EncodeToBytes();
2734+
byte[] eb2 = o2.EncodeToBytes();
2735+
CBORTestCommon.AssertRoundTrip(o1);
2736+
CBORTestCommon.AssertRoundTrip(o2);
2737+
CBORNumber on1 = o1.AsNumber();
2738+
CBORNumber on2 = o2.AsNumber();
2739+
CBORNumber onSum = null;
2740+
try {
2741+
onSum = on1.Multiply(on2);
2742+
} catch (OutOfMemoryException) {
2743+
return false;
2744+
}
2745+
if (!onSum.IsFinite()) {
2746+
// Console.WriteLine("on1=" + o1);
2747+
// Console.WriteLine("on2=" + o2);
2748+
return false;
2749+
}
2750+
// Console.WriteLine(i+"");
2751+
// Console.WriteLine(i+" "+Chop(o1.ToString()));
2752+
// Console.WriteLine(i+" "+Chop(o2.ToString()));
2753+
// Console.WriteLine(i + " " + Chop(onSum.ToString()));
2754+
if (!onSum.IsFinite()) {
2755+
Assert.Fail("onSum is not finite\n" +
2756+
"o1=" + TestCommon.ToByteArrayString(eb1) + "\n" +
2757+
"o2=" + TestCommon.ToByteArrayString(eb2) + "\n");
2758+
}
2759+
CBORNumber on2a = onSum.Divide(on1);
2760+
// NOTE: Ignore if divisor is zero
2761+
if (!on1.IsZero() && !on2a.IsFinite()) {
2762+
Assert.Fail("on2a is not finite\n" +
2763+
"o1=" + TestCommon.ToByteArrayString(eb1) + "\n" +
2764+
"o2=" + TestCommon.ToByteArrayString(eb2) + "\n");
2765+
}
2766+
if (!on1.IsZero() && !on2.IsZero()) {
2767+
VerifyEqual(on2a, on2, o1, o2);
2768+
}
2769+
CBORNumber on1a = onSum.Divide(on2);
2770+
// NOTE: Ignore if divisor is zero
2771+
if (!on2.IsZero() && !on1a.IsFinite()) {
2772+
Assert.Fail("on1a is not finite\n" +
2773+
"o1=" + on1 + "\n" + "o2=" + on2 + "\n" +
2774+
"{\nbyte[] bytes1 = " + TestCommon.ToByteArrayString(eb1) + ";\n" +
2775+
"byte[] bytes2 =" + TestCommon.ToByteArrayString(eb2) + ";\n" +
2776+
"TestAsNumberMultiplyDivideOne(\nCBORObject.D" +
2777+
"ecodeFromBytes(bytes1),\n" +
2778+
"CBORObject.DecodeFromBytes(bytes2));\n}\n");
2779+
}
2780+
if (!on1.IsZero() && !on2.IsZero()) {
2781+
VerifyEqual(on1a, on1, o1, o2);
2782+
}
2783+
return true;
28032784
}
28042785

28052786
[Test]

0 commit comments

Comments
 (0)