private static bool TryDeserializeScalar(IIonReader reader, Type type, IScalarConverter scalarConverter, ref object result) { if (type == typeof(string)) { if (reader.CurrentType != IonType.String && reader.CurrentType != IonType.Null) { return(false); } result = reader.CurrentIsNull ? null : reader.StringValue(); return(true); } if (reader.CurrentIsNull) { if (type.IsValueType) { return(false); } result = null; return(true); } //check for enum/symbol if (type.IsEnum) { if (reader.CurrentType != IonType.Symbol) { goto NoMatch; } var symbolText = reader.SymbolValue().Text; return(Enum.TryParse(type, symbolText, out result)); } if (type == typeof(bool)) { if (reader.CurrentType != IonType.Bool) { goto NoMatch; } result = reader.BoolValue(); return(true); } if (type == typeof(int)) { if (reader.CurrentType != IonType.Int) { goto NoMatch; } switch (reader.GetIntegerSize()) { case IntegerSize.Int: result = reader.IntValue(); return(true); case IntegerSize.Long: case IntegerSize.BigInteger: throw new OverflowException($"Encoded value is too big for int32"); default: throw new ArgumentOutOfRangeException(); } } if (type == typeof(long)) { if (reader.CurrentType != IonType.Int) { goto NoMatch; } switch (reader.GetIntegerSize()) { case IntegerSize.Int: result = (long)reader.IntValue(); return(true); case IntegerSize.Long: result = reader.LongValue(); return(true); case IntegerSize.BigInteger: throw new OverflowException($"Encoded value is too big for int32"); default: throw new ArgumentOutOfRangeException(); } } if (type == typeof(BigInteger)) { if (reader.CurrentType != IonType.Int) { goto NoMatch; } switch (reader.GetIntegerSize()) { case IntegerSize.Int: result = new BigInteger(reader.IntValue()); return(true); case IntegerSize.Long: result = new BigInteger(reader.LongValue()); return(true); case IntegerSize.BigInteger: result = reader.BigIntegerValue(); return(true); default: throw new ArgumentOutOfRangeException(); } } if (type == typeof(float)) { if (reader.CurrentType != IonType.Float) { goto NoMatch; } result = (float)reader.DoubleValue(); return(true); } if (type == typeof(double)) { if (reader.CurrentType != IonType.Float) { goto NoMatch; } result = reader.DoubleValue(); return(true); } if (type == typeof(decimal)) { if (reader.CurrentType != IonType.Decimal) { goto NoMatch; } result = reader.DecimalValue(); return(true); } if (type == typeof(DateTime)) { if (reader.CurrentType != IonType.Timestamp) { goto NoMatch; } result = reader.TimestampValue().DateTime; return(true); } if (type == typeof(DateTimeOffset)) { if (reader.CurrentType != IonType.Timestamp) { goto NoMatch; } result = reader.TimestampValue().AsDateTimeOffset(); return(true); } NoMatch: //here means we don't know , try the scalar converter return(scalarConverter != null && reader.TryConvertTo(type, scalarConverter, out result)); }