Skip to content

Commit

Permalink
change 'char' behavior in FromObject; implement 'char' deserializatio…
Browse files Browse the repository at this point in the history
…n in ToObject; fix tests; etc.
  • Loading branch information
peteroupc committed Sep 30, 2018
1 parent 28602ee commit 6366ee3
Show file tree
Hide file tree
Showing 14 changed files with 450 additions and 478 deletions.
27 changes: 4 additions & 23 deletions CBOR/PeterO/Cbor/CBORObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ public object ToObject(Type t) {
}

/// <include file='../../docs.xml'
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.ToObject(System.Type,PeterO.Cbor.CBORTypeMapper)"]/*'/>
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.ToObject(System.Type,PeterO.Cbor.CBORTypeMapper)"]/*'/>
public object ToObject(Type t, CBORTypeMapper mapper) {
if (mapper == null) {
throw new ArgumentNullException(nameof(mapper));
Expand All @@ -616,7 +616,7 @@ public object ToObject(Type t, CBORTypeMapper mapper) {
}

/// <include file='../../docs.xml'
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.ToObject(System.Type,PeterO.Cbor.PODOptions)"]/*'/>
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.ToObject(System.Type,PeterO.Cbor.PODOptions)"]/*'/>
public object ToObject(Type t, PODOptions options) {
if (options == null) {
throw new ArgumentNullException(nameof(options));
Expand All @@ -625,7 +625,7 @@ public object ToObject(Type t, PODOptions options) {
}

/// <include file='../../docs.xml'
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.ToObject(System.Type,PeterO.Cbor.CBORTypeMapper,PeterO.Cbor.PODOptions)"]/*'/>
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.ToObject(System.Type,PeterO.Cbor.CBORTypeMapper,PeterO.Cbor.PODOptions)"]/*'/>
public object ToObject(Type t, CBORTypeMapper mapper, PODOptions options) {
if (mapper == null) {
throw new ArgumentNullException(nameof(mapper));
Expand Down Expand Up @@ -786,14 +786,6 @@ public static CBORObject FromObject(short value) {
FromObject((long)value);
}

/// <include file='../../docs.xml'
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.FromObject(System.Char)"]/*'/>
public static CBORObject FromObject(char value) {
// TODO: Consider changing this method's behavior
char[] valueChar = { value };
return FromObject(new String(valueChar));
}

/// <include file='../../docs.xml'
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.FromObject(System.Boolean)"]/*'/>
public static CBORObject FromObject(bool value) {
Expand Down Expand Up @@ -958,7 +950,7 @@ internal static CBORObject FromObject(
return FromObject((short)obj);
}
if (obj is char) {
return FromObject((char)obj);
return FromObject((int)(char)obj);
}
if (obj is bool) {
return FromObject((bool)obj);
Expand Down Expand Up @@ -1498,17 +1490,6 @@ public static void Write(short value, Stream stream) {
Write((long)value, stream);
}

/// <include file='../../docs.xml'
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.Write(System.Char,System.IO.Stream)"]/*'/>
public static void Write(char value, Stream stream) {
// TODO: Consider changing this method's behavior
if (value >= 0xd800 && value < 0xe000) {
throw new ArgumentException("Value is a surrogate code point.");
}
char[] valueChar = { value };
Write(new String(valueChar), stream);
}

/// <include file='../../docs.xml'
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.Write(System.Boolean,System.IO.Stream)"]/*'/>
public static void Write(bool value, Stream stream) {
Expand Down
6 changes: 3 additions & 3 deletions CBOR/PeterO/Cbor/CBORObjectExtra.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,19 +241,19 @@ public T ToObject<T>() {
}

/// <include file='../../docs.xml'
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.ToObject``1(PeterO.Cbor.CBORTypeMapper)"]/*'/>
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.ToObject``1(PeterO.Cbor.CBORTypeMapper)"]/*'/>
public T ToObject<T>(CBORTypeMapper mapper) {
return (T)this.ToObject(typeof(T), mapper);
}

/// <include file='../../docs.xml'
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.ToObject``1(PeterO.Cbor.PODOptions)"]/*'/>
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.ToObject``1(PeterO.Cbor.PODOptions)"]/*'/>
public T ToObject<T>(PODOptions options) {
return (T)this.ToObject(typeof(T), options);
}

/// <include file='../../docs.xml'
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.ToObject``1(PeterO.Cbor.CBORTypeMapper,PeterO.Cbor.PODOptions)"]/*'/>
/// path='docs/doc[@name="M:PeterO.Cbor.CBORObject.ToObject``1(PeterO.Cbor.CBORTypeMapper,PeterO.Cbor.PODOptions)"]/*'/>
public T ToObject<T>(CBORTypeMapper mapper, PODOptions options) {
return (T)this.ToObject(typeof(T), mapper, options);
}
Expand Down
29 changes: 15 additions & 14 deletions CBOR/PeterO/Cbor/CBORReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,29 +128,30 @@ public CBORObject ReadForFirstByte(int firstbyte) {
return cbor;
}
// Special check: Decimal fraction or bigfloat
if(firstbyte==0xc4 || firstbyte==0xc5){
int nextbyte=this.stream.ReadByte();
if(nextbyte!=0x82 && nextbyte!=0x9f)
throw new CBORException("2-item array expected");
bool indefArray=(nextbyte==0x9f);
nextbyte=this.stream.ReadByte();
if(nextbyte>=0x40){
if (firstbyte == 0xc4 || firstbyte == 0xc5) {
int nextbyte = this.stream.ReadByte();
if (nextbyte != 0x82 && nextbyte != 0x9f) {
throw new CBORException("2-item array expected");
}
bool indefArray = nextbyte == 0x9f;
nextbyte = this.stream.ReadByte();
if (nextbyte >= 0x40) {
throw new CBORException("Major type 0 or 1 or bignum expected");
}
CBORObject exponent=ReadForFirstByte(nextbyte);
nextbyte=this.stream.ReadByte();
if(nextbyte>=0x40 && nextbyte!=0xc2 && nextbyte!=0xc3){
CBORObject exponent = this.ReadForFirstByte(nextbyte);
nextbyte = this.stream.ReadByte();
if (nextbyte >= 0x40 && nextbyte != 0xc2 && nextbyte != 0xc3) {
throw new CBORException("Major type 0 or 1 expected");
}
CBORObject significand=ReadForFirstByte(nextbyte);
if(indefArray && this.stream.ReadByte()!=0xff){
CBORObject significand = this.ReadForFirstByte(nextbyte);
if (indefArray && this.stream.ReadByte() != 0xff) {
throw new CBORException("End of array expected");
}
CBORObject arr=CBORObject.NewArray()
CBORObject arr = CBORObject.NewArray()
.Add(exponent).Add(significand);
return CBORObject.FromObjectAndTag(
arr,
firstbyte==0xc4 ? 4 : 5);
firstbyte == 0xc4 ? 4 : 5);
}
var uadditional = (long)additional;
EInteger bigintAdditional = EInteger.Zero;
Expand Down
4 changes: 2 additions & 2 deletions CBOR/PeterO/Cbor/CBORTypeMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ internal sealed class ConverterInfo {

public object FromObject { get; set; }

/// <include file='../../docs.xml'
/// path='docs/doc[@name="P:PeterO.Cbor.CBORTypeMapper.ConverterInfo.Converter"]/*'/>
/// <summary>Gets a value not documented yet.</summary>
/// <value>A value not documented yet.</value>
public object Converter { get; set; }
}
}
Expand Down
16 changes: 8 additions & 8 deletions CBOR/PeterO/Cbor/CBORUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,13 +477,13 @@ private static int[] ParseAtomDateTimeString(string str) {
if (bad) {
throw new ArgumentException("Invalid date/time");
}
int year = (str[0] - '0') * 1000 + (str[1] - '0') * 100 +
int year = ((str[0] - '0') * 1000) + (str[1] - '0') * 100 +
(str[2] - '0') * 10 + (str[3] - '0');
int month = (str[5] - '0') * 10 + (str[6] - '0');
int day = (str[8] - '0') * 10 + (str[9] - '0');
int hour = (str[11] - '0') * 10 + (str[12] - '0');
int minute = (str[14] - '0') * 10 + (str[15] - '0');
int second = (str[17] - '0') * 10 + (str[18] - '0');
int month = ((str[5] - '0') * 10) + (str[6] - '0');
int day = ((str[8] - '0') * 10) + (str[9] - '0');
int hour = ((str[11] - '0') * 10) + (str[12] - '0');
int minute = ((str[14] - '0') * 10) + (str[15] - '0');
int second = ((str[17] - '0') * 10) + (str[18] - '0');
var index = 19;
var nanoSeconds = 0;
if (index <= str.Length && str[index] == '.') {
Expand Down Expand Up @@ -524,8 +524,8 @@ following RFC 4287 sec. 3.3*/
throw new ArgumentException("Invalid date/time");
}
bool neg = str[index] == '-';
int tzhour = (str[index + 1] - '0') * 10 + (str[index + 2] - '0');
int tzminute = (str[index + 4] - '0') * 10 + (str[index + 5] - '0');
int tzhour = ((str[index + 1] - '0') * 10) + (str[index + 2] - '0');
int tzminute = ((str[index + 4] - '0') * 10) + (str[index + 5] - '0');
if (tzminute >= 60) {
throw new ArgumentException("Invalid date/time");
}
Expand Down
29 changes: 23 additions & 6 deletions CBOR/PeterO/Cbor/PropertyMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,23 @@ public static object TypeToObject(
if (t.Equals(typeof(bool))) {
return objThis.AsBoolean();
}
if (t.Equals(typeof(char))) {
if (objThis.Type == CBORType.TextString) {
string s = objThis.AsString();
if (s.Length != 1) {
throw new CBORException("Can't convert to char");
}
return s[0];
}
if (objThis.IsIntegral && objThis.CanTruncatedIntFitInInt32()) {
int c = objThis.AsInt32();
if (c < 0 || c >= 0x10000) {
throw new CBORException("Can't convert to char");
}
return (char)c;
}
throw new CBORException("Can't convert to char");
}
if (t.Equals(typeof(DateTime))) {
return new CBORDateConverter().FromCBORObject(objThis);
}
Expand Down Expand Up @@ -655,16 +672,16 @@ public static object TypeToObject(
isDict = t.IsGenericType;
if (t.IsGenericType) {
Type td = t.GetGenericTypeDefinition();
isDict = td.Equals(typeof(Dictionary<, >)) ||
td.Equals(typeof(IDictionary<, >));
isDict = td.Equals(typeof(Dictionary<,>)) ||
td.Equals(typeof(IDictionary<,>));
}
// DebugUtility.Log("list=" + isDict);
isDict = isDict && t.GetGenericArguments().Length == 2;
// DebugUtility.Log("list=" + isDict);
if (isDict) {
keyType = t.GetGenericArguments()[0];
valueType = t.GetGenericArguments()[1];
Type listType = typeof(Dictionary<, >).MakeGenericType(
Type listType = typeof(Dictionary<,>).MakeGenericType(
keyType,
valueType);
dictObject = Activator.CreateInstance(listType);
Expand All @@ -673,16 +690,16 @@ public static object TypeToObject(
isDict = (t.GetTypeInfo().IsGenericType);
if (t.GetTypeInfo().IsGenericType) {
Type td = t.GetGenericTypeDefinition();
isDict = (td.Equals(typeof(Dictionary<, >)) ||
td.Equals(typeof(IDictionary<, >)));
isDict = (td.Equals(typeof(Dictionary<,>)) ||
td.Equals(typeof(IDictionary<,>)));
}
//DebugUtility.Log("list=" + isDict);
isDict = (isDict && t.GenericTypeArguments.Length == 2);
//DebugUtility.Log("list=" + isDict);
if (isDict) {
keyType = t.GenericTypeArguments[0];
valueType = t.GenericTypeArguments[1];
Type listType = typeof(Dictionary<, >).MakeGenericType(
Type listType = typeof(Dictionary<,>).MakeGenericType(
keyType,
valueType);
dictObject = Activator.CreateInstance(listType);
Expand Down
Loading

0 comments on commit 6366ee3

Please sign in to comment.