Esempio n. 1
0
 public ChoContractResolverJsonConverter(ChoFileRecordFieldConfiguration fc, CultureInfo culture, Type objType, ChoObjectValidationMode validationMode, MemberInfo mi)
 {
     _fc             = fc;
     _culture        = culture;
     _objType        = objType;
     _validationMode = validationMode;
     _mi             = mi;
 }
Esempio n. 2
0
        private string CleanFieldValue(ChoFileRecordFieldConfiguration config, Type fieldType, string fieldValue)
        {
            if (fieldValue == null)
            {
                return(fieldValue);
            }

            ChoFieldValueTrimOption fieldValueTrimOption = config.GetFieldValueTrimOptionForRead(fieldType, Configuration.FieldValueTrimOption);

            switch (fieldValueTrimOption)
            {
            case ChoFieldValueTrimOption.Trim:
                fieldValue = fieldValue.Trim();
                break;

            case ChoFieldValueTrimOption.TrimStart:
                fieldValue = fieldValue.TrimStart();
                break;

            case ChoFieldValueTrimOption.TrimEnd:
                fieldValue = fieldValue.TrimEnd();
                break;
            }

            if (config.Size != null)
            {
                if (fieldValue.Length > config.Size.Value)
                {
                    if (!config.Truncate)
                    {
                        throw new ChoParserException("Incorrect field value length found for '{0}' member [Expected: {1}, Actual: {2}].".FormatString(config.FieldName, config.Size.Value, fieldValue.Length));
                    }
                    else
                    {
                        if (fieldValueTrimOption == ChoFieldValueTrimOption.TrimStart)
                        {
                            fieldValue = fieldValue.Right(config.Size.Value);
                        }
                        else
                        {
                            fieldValue = fieldValue.Substring(0, config.Size.Value);
                        }
                    }
                }
            }
            if (fieldValue.StartsWith(@"""") && fieldValue.EndsWith(@""""))
            {
                fieldValue = fieldValue.Substring(1, fieldValue.Length - 2);
            }

            return(System.Net.WebUtility.HtmlDecode(fieldValue));
        }
Esempio n. 3
0
        //protected Type DiscoverFieldType(string value, ChoFileRecordConfiguration config)
        //{
        //    bool treatCurrencyAsDecimal = config.TreatCurrencyAsDecimal;
        //    long lresult = 0;
        //    double dresult = 0;
        //    DateTime dtresult;
        //    Decimal decResult = 0;
        //    ChoCurrency currResult = 0;
        //    Guid guidResult;

        //    if (value == null)
        //        return typeof(string);
        //    else if (long.TryParse(value, out lresult))
        //        return typeof(long);
        //    else if (double.TryParse(value, out dresult))
        //        return typeof(double);
        //    else if (RecordConfiguration.TypeConverterFormatSpec.DateTimeFormat.IsNullOrWhiteSpace()
        //        && ChoDateTime.TryParseExact(value, RecordConfiguration.TypeConverterFormatSpec.DateTimeFormat, CultureInfo.CurrentCulture, out dtResult))
        //        return typeof(DateTime);
        //    else if (DateTime.TryParse(value, out dtresult))
        //        return typeof(DateTime);
        //    else if (Guid.TryParse(value, out guidResult))
        //        return typeof(Guid);
        //    else if (!treatCurrencyAsDecimal && ChoCurrency.TryParse(value, out currResult))
        //        return typeof(ChoCurrency);
        //    else if (treatCurrencyAsDecimal && Decimal.TryParse(value, NumberStyles.Currency, CultureInfo.CurrentCulture, out decResult))
        //        return typeof(Decimal);
        //    else
        //        return typeof(string);
        //}

        protected object GetDeclaringRecord(string declaringMember, object rec, ChoFileRecordFieldConfiguration config = null)
        {
            if (rec == null)
            {
                return(null);
            }

            var obj = ChoType.GetDeclaringRecord(declaringMember, rec);

            if (obj == null)
            {
                return(null);
            }

            Type recordType = obj.GetType();

            if (config != null)
            {
                if (config.ArrayIndex != null &&
                    config.ArrayIndex.Value >= 0 &&
                    obj is IEnumerable &&
                    !(obj is ArrayList))
                {
                    var item = Enumerable.Skip(((IEnumerable)obj).Cast <object>(), config.ArrayIndex.Value).FirstOrDefault();

                    if (item == null)
                    {
                        Type itemType = obj.GetType().GetItemType();
                        item = ChoActivator.CreateInstance(itemType);
                    }

                    if (obj is Array)
                    {
                        if (config.ArrayIndex.Value < ((Array)obj).Length)
                        {
                            ((Array)obj).SetValue(item, config.ArrayIndex.Value);
                        }
                    }
                    else if (obj is IList)
                    {
                        ((IList)obj).Add(item);
                    }

                    return(item);
                }
            }

            return(obj);
        }