Skip to content

Commit

Permalink
More netcf compat fixes. ModelReader now works fine
Browse files Browse the repository at this point in the history
  • Loading branch information
cail committed Jul 7, 2011
1 parent 5340d0d commit 5f48a30
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 47 deletions.
16 changes: 14 additions & 2 deletions src/JsonFx/CodeGen/DynamicMethodGenerator_NoEmit.cs
Expand Up @@ -118,7 +118,13 @@ public static GetterDelegate GetPropertyGetter(PropertyInfo propertyInfo)

return delegate(object instance)
{
return methodInfo.Invoke(instance, Type.EmptyTypes);
return methodInfo.Invoke(instance,
#if NETCF
new Type[]{}
#else
Type.EmptyTypes
#endif
);
};
}

Expand Down Expand Up @@ -282,7 +288,13 @@ public static FactoryDelegate GetTypeFactory(Type type)
throw new ArgumentNullException("type");
}

ConstructorInfo ctor = type.GetConstructor(BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.FlattenHierarchy, null, Type.EmptyTypes, null);
ConstructorInfo ctor = type.GetConstructor(BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.FlattenHierarchy, null,
#if NETCF
new Type[]{},
#else
Type.EmptyTypes,
#endif
null);
if (ctor == null)
{
return null;
Expand Down
42 changes: 22 additions & 20 deletions src/JsonFx/Json/JsonTokenizer.cs
Expand Up @@ -526,13 +526,13 @@ private static Token<ModelTokenType> ScanNumber(ITextStream scanner)
if (!hasDecimal && !hasExponent && precision < 19)
{
// Integer value
decimal number;
if (!Decimal.TryParse(
buffer,
NumberStyles.Integer,
NumberFormatInfo.InvariantInfo,
out number))
{
decimal number = 0;
try{
Decimal.Parse(
buffer,
NumberStyles.Integer,
NumberFormatInfo.InvariantInfo);
}catch(Exception){
throw new DeserializationException(JsonTokenizer.ErrorIllegalNumber, numPos, numLine, numCol);
}

Expand All @@ -555,12 +555,12 @@ private static Token<ModelTokenType> ScanNumber(ITextStream scanner)
{
// Floating Point value
double number;
if (!Double.TryParse(
buffer,
NumberStyles.Float,
NumberFormatInfo.InvariantInfo,
out number))
{
try{
number = Double.Parse(
buffer,
NumberStyles.Float,
NumberFormatInfo.InvariantInfo);
}catch(Exception){
throw new DeserializationException(JsonTokenizer.ErrorIllegalNumber, numPos, numLine, numCol);
}

Expand Down Expand Up @@ -698,13 +698,15 @@ private static string ScanString(ITextStream scanner)
}

// unicode ordinal
int utf16;
if (escapeSeq.Length == UnicodeEscapeLength &&
Int32.TryParse(
escapeSeq,
NumberStyles.AllowHexSpecifier,
NumberFormatInfo.InvariantInfo,
out utf16))
int utf16 = 0;
bool parsed = true;
try{
utf16 = Int32.Parse(
escapeSeq,
NumberStyles.AllowHexSpecifier,
NumberFormatInfo.InvariantInfo);
}catch(Exception){ parsed = false; };
if (escapeSeq.Length == UnicodeEscapeLength && parsed)
{
buffer.Append(CharUtility.ConvertFromUtf32(utf16));
}
Expand Down
9 changes: 5 additions & 4 deletions src/JsonFx/Model/Filters/Iso8601DateFilter.cs
Expand Up @@ -178,13 +178,14 @@ public override bool TryWrite(DataWriterSettings settings, DateTime value, out I
/// <returns>true if parsing was successful</returns>
private static bool TryParseIso8601(string date, out DateTime value)
{
if (!DateTime.TryParseExact(
try{
value = DateTime.ParseExact(
date,
Iso8601DateFilter.FullFormat,
CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind|DateTimeStyles.AllowWhiteSpaces|DateTimeStyles.NoCurrentDateDefault,
out value))
{
DateTimeStyles.RoundtripKind|DateTimeStyles.AllowWhiteSpaces|DateTimeStyles.NoCurrentDateDefault
);
}catch(Exception){
value = default(DateTime);
return false;
}
Expand Down
24 changes: 21 additions & 3 deletions src/JsonFx/Serialization/Resolvers/ResolverCache.cs
Expand Up @@ -237,7 +237,10 @@ public FactoryMap(Type type)

Type argType = paramList[0].ParameterType;
if ((argType == typeof(string)) ||
((argType.GetInterface(TypeCoercionUtility.TypeGenericIEnumerable, false) == null) &&
(
#if !NETCF
(argType.GetInterface(TypeCoercionUtility.TypeGenericIEnumerable, false) == null) &&
#endif
(typeof(IEnumerable).IsAssignableFrom(argType))))
{
continue;
Expand Down Expand Up @@ -268,7 +271,10 @@ public FactoryMap(Type type)

// many collection types have an Add method
// which adds items one at a time
Type collectionType = type.GetInterface(TypeCoercionUtility.TypeGenericICollection, false);
Type collectionType = null;
#if !NETCF
collectionType = type.GetInterface(TypeCoercionUtility.TypeGenericICollection, false);
#endif
if (collectionType != null)
{
methodInfo = collectionType.GetMethod("Add");
Expand Down Expand Up @@ -302,7 +308,11 @@ public IEnumerable<Type> ArgTypes
{
if (this.CollectionCtors == null)
{
#if NETCF
return new Type[]{ };
#else
return Type.EmptyTypes;
#endif
}

return this.CollectionCtors.Keys;
Expand Down Expand Up @@ -348,7 +358,15 @@ internal static bool IsImmutableType(Type type)
return
!type.IsInterface &&
!type.IsAbstract &&
(type.IsValueType || (type.GetConstructor(BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.FlattenHierarchy, null, Type.EmptyTypes, null) == null));
(type.IsValueType ||
(type.GetConstructor(BindingFlags.Instance|BindingFlags.Public|BindingFlags.NonPublic|BindingFlags.FlattenHierarchy,
null,
#if NETCF
new Type[]{ },
#else
Type.EmptyTypes,
#endif
null) == null));
}

#endregion Utility Methods
Expand Down
41 changes: 23 additions & 18 deletions src/JsonFx/Serialization/TypeCoercionUtility.cs
Expand Up @@ -252,7 +252,11 @@ internal void SetMemberValue(object target, Type targetType, MemberMap memberMap
((System.Dynamic.DynamicObject)target).TrySetMember(new DynamicSetter(memberName), memberValue);
}
#endif
else if (targetType != null && targetType.GetInterface(TypeCoercionUtility.TypeGenericIDictionary, false) != null)
else if (targetType != null
#if !NETCF
&& targetType.GetInterface(TypeCoercionUtility.TypeGenericIDictionary, false) != null
#endif
)
{
throw new TypeCoercionException(String.Format(
TypeCoercionUtility.ErrorGenericIDictionary,
Expand Down Expand Up @@ -368,18 +372,17 @@ public object CoerceType(Type targetType, object value)
if (targetType == typeof(DateTime))
{
DateTime date;
if (DateTime.TryParse(
try{
date = DateTime.Parse(
(string)value,
CultureInfo.InvariantCulture,
DateTimeStyles.RoundtripKind|DateTimeStyles.AllowWhiteSpaces|DateTimeStyles.NoCurrentDateDefault,
out date))
{
DateTimeStyles.RoundtripKind|DateTimeStyles.AllowWhiteSpaces|DateTimeStyles.NoCurrentDateDefault);
if (date.Kind == DateTimeKind.Local)
{
return date.ToUniversalTime();
}
return date;
}
}catch(Exception){}

if (JsonFx.Model.Filters.MSAjaxDateFilter.TryParseMSAjaxDate(
(string)value,
Expand Down Expand Up @@ -415,16 +418,12 @@ public object CoerceType(Type targetType, object value)
}
else if (targetType == typeof(TimeSpan))
{
long ticks;
if (Int64.TryParse((string)value, out ticks))
{
return TimeSpan.FromTicks(ticks);
}
TimeSpan timespan;
if (TimeSpan.TryParse((string)value, out timespan))
{
return timespan;
}
try{
return TimeSpan.FromTicks(Int64.Parse((string)value));
}catch(Exception){}
try{
return TimeSpan.Parse((string)value);
}catch(Exception){}
}
}
else if (targetType == typeof(TimeSpan))
Expand Down Expand Up @@ -781,7 +780,10 @@ internal static Type GetDictionaryItemType(Type targetType)
return null;
}

Type dictionaryType = targetType.GetInterface(TypeCoercionUtility.TypeGenericIDictionary, false);
Type dictionaryType = null;
#if !NETCF
dictionaryType = targetType.GetInterface(TypeCoercionUtility.TypeGenericIDictionary, false);
#endif
if (dictionaryType == null)
{
// not an IDictionary<TKey, TVal>
Expand Down Expand Up @@ -821,7 +823,10 @@ internal static Type GetElementType(Type targetType)
return targetType.GetElementType();
}

Type arrayType = targetType.GetInterface(TypeCoercionUtility.TypeGenericIEnumerable, false);
Type arrayType = null;
#if !NETCF
arrayType = targetType.GetInterface(TypeCoercionUtility.TypeGenericIEnumerable, false);
#endif
if (arrayType == null)
{
// not an IEnumerable<T>
Expand Down

0 comments on commit 5f48a30

Please sign in to comment.