public object GetValue(object target) { if (MemberInfo != null) { return(ChoType.GetMemberValue(target, MemberInfo)); } else if (target is IDictionary <string, object> ) ///ExpandoObject || target is ChoDynamicObject) { IDictionary <string, object> dict = target as IDictionary <string, object>; if (dict.ContainsKey(MemberName)) { return(((IDictionary <string, object>)target)[MemberName]); } else { if (target is ChoDynamicObject && ((ChoDynamicObject)target).DynamicObjectName == MemberName) { return(((ChoDynamicObject)target).GetText()); } throw new ApplicationException("Can't find '{0}' member in dynamic object.".FormatString(MemberName)); } } else { return(ChoConvert.ConvertTo(ChoType.GetMemberValue(target, MemberName), ProperyType)); } }
public static void GetNConvertMemberValue(this object rec, string fn, ChoRecordFieldConfiguration fieldConfig, CultureInfo culture, ref object fieldValue) { if (fieldConfig.Converters.IsNullOrEmpty()) { fieldValue = ChoConvert.ConvertTo(fieldValue, typeof(string), null, fieldConfig.PropConverters, fieldConfig.PropConverterParams, culture); } else { fieldValue = ChoConvert.ConvertTo(fieldValue, typeof(string), null, fieldConfig.Converters.ToArray(), null, culture); } }
public static bool TryConvertTo(object value, Type targetType, CultureInfo culture, out object output) { output = (object)null; try { output = ChoConvert.ConvertTo(value, targetType, culture); return(true); } catch { return(false); } }
public static bool TryConvertTo(object value, MemberInfo memberInfo, Type targetType, object sourceObject, CultureInfo culture, out object output) { output = (object)null; ChoGuard.ArgumentNotNull((object)memberInfo, "MemberInfo"); try { output = ChoConvert.ConvertTo(value, targetType, sourceObject, ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo), culture); return(true); } catch { return(false); } }
public static object ConvertTo(object value, Type targetType, CultureInfo culture = null) { if (value == null) { return(ChoConvert.ConvertTo(value, targetType, value, (object[])null, (object[])null, culture)); } Type origType = targetType; Type type = value == null ? typeof(object) : value.GetType(); if (type == origType) { return(value); } return(ChoConvert.ConvertTo(value, targetType, value, ChoTypeDescriptor.GetTypeConvertersForType(type), null, culture)); }
protected virtual object Serialize(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { IList result = ChoActivator.CreateInstance(typeof(IList <>).MakeGenericType(targetType)) as IList; if (value != null && !value.GetType().IsCollectionType()) { if (targetType == typeof(object) || targetType.IsSimple()) { foreach (var p in ChoTypeDescriptor.GetProperties(value.GetType()).Where(pd => ChoTypeDescriptor.GetPropetyAttribute <ChoIgnoreMemberAttribute>(pd) == null)) { result.Add(ChoConvert.ConvertTo(ChoType.GetPropertyValue(value, p.Name), targetType, culture)); } } } return(result.OfType <object>().ToArray()); }
public static bool GetFallbackValue(this object rec, string fn, ChoRecordFieldConfiguration fieldConfig, CultureInfo culture, ref object fieldValue) { if (!fieldConfig.IsFallbackValueSpecified) { return(false); } if (fieldConfig.Converters.IsNullOrEmpty()) { fieldValue = ChoConvert.ConvertTo(fieldConfig.FallbackValue, fieldConfig.FieldType, null, fieldConfig.PropConverters, fieldConfig.PropConverterParams, culture); } else { fieldValue = ChoConvert.ConvertTo(fieldConfig.FallbackValue, fieldConfig.FieldType, null, fieldConfig.Converters.ToArray(), null, culture); } return(true); }
public object NormalizeValue(string name, object value, Type targetType = null) { var m1 = ChoType.GetMembers(GetType()).Where(m => !ChoType.IsReadOnlyMember(m) && (m.GetCustomAttribute <ChoPropertyAttribute>() != null && m.GetCustomAttribute <ChoPropertyAttribute>().Name == name) || m.Name == name).FirstOrDefault(); if (m1 == null) { return(value); } value = ChoConvert.ConvertTo(value, m1, targetType == null ? typeof(string) : targetType); var attr = ChoType.GetMemberAttribute <ChoPropertyAttribute>(m1); if (value != null && value is string) { string mv = value as string; if (attr != null) { switch (attr.TrimOption) { case ChoPropertyValueTrimOption.Trim: mv = mv.Trim(); break; case ChoPropertyValueTrimOption.TrimEnd: mv = mv.TrimEnd(); break; case ChoPropertyValueTrimOption.TrimStart: mv = mv.TrimStart(); break; } } return(mv); } return(value); }
public static bool GetFallbackValue(this object rec, string fn, ChoRecordFieldConfiguration fieldConfig, CultureInfo culture, ref object fieldValue) { if (rec is ExpandoObject) { return(((IDictionary <string, object>)rec).GetFallbackValue(fn, fieldConfig, culture, ref fieldValue)); } //Set Fallback value to member bool FallbackValueExists = true; object FallbackValue = null; if (fieldConfig.IsFallbackValueSpecified) { FallbackValue = fieldConfig.FallbackValue; } else if (ChoType.HasFallbackValue(ChoTypeDescriptor.GetProperty(rec.GetType(), fn))) { FallbackValue = ChoType.GetRawFallbackValue(ChoTypeDescriptor.GetProperty(rec.GetType(), fn)); } else { FallbackValueExists = false; } if (FallbackValueExists) { if (fieldConfig.Converters.IsNullOrEmpty()) { fieldValue = ChoConvert.ConvertTo(FallbackValue, ChoType.GetMemberInfo(rec.GetType(), fn), null, culture); } else { fieldValue = ChoConvert.ConvertTo(FallbackValue, ChoType.GetMemberType(rec.GetType(), fn), null, fieldConfig.Converters.ToArray(), null, culture); } } return(FallbackValueExists); }
//***** public static bool GetDefaultValue(this object rec, string fn, ChoRecordFieldConfiguration fieldConfig, CultureInfo culture, ref object fieldValue) { if (!fieldConfig.IsDefaultValueSpecified) { return(false); } if (!fieldConfig.FormatText.IsNullOrWhiteSpace()) { fieldValue = ("{0:" + fieldConfig.FormatText + "}").FormatString(fieldValue); } else { if (fieldConfig.Converters.IsNullOrEmpty()) { fieldValue = ChoConvert.ConvertTo(fieldConfig.DefaultValue, fieldConfig.FieldType, null, fieldConfig.PropConverters, fieldConfig.PropConverterParams, culture); } else { fieldValue = ChoConvert.ConvertTo(fieldConfig.DefaultValue, fieldConfig.FieldType, null, fieldConfig.Converters.ToArray(), null, culture); } } return(true); }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value != null && _objType != null) { value = ChoConvert.ConvertTo(value, _objType, null, _fc.PropConverters, _fc.PropConverterParams, _culture); } if (_fc.CustomSerializer == null) { if (_fc.ValueConverter == null) { JToken t = JToken.FromObject(value); t.WriteTo(writer); } else { object retValue = _fc.ValueConverter(value); Validate(retValue); ChoETLRecordHelper.DoMemberLevelValidation(retValue, _fc.Name, _fc, _validationMode); JToken t = JToken.FromObject(retValue); t.WriteTo(writer); } } else { object retValue = _fc.CustomSerializer(writer); if (retValue != null) { Validate(retValue); JToken t = JToken.FromObject(value); t.WriteTo(writer); } } }
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var crs = Writer.ContractResolverState; if (crs == null) { var t = serializer.SerializeToJToken(value); t.WriteTo(writer); //serializer.Serialize(writer, value); return; } var fc = crs.FieldConfig; crs.Name = _fc == null?_mi.GetFullName() : crs.Name; var rec = ChoType.GetMemberObjectMatchingType(crs.Name, crs.Record); var name = ChoType.GetFieldName(crs.Name); var st = ChoType.GetMemberAttribute(_mi, typeof(ChoSourceTypeAttribute)) as ChoSourceTypeAttribute; if (st != null && st.Type != null) { _objType = st.Type; } if (_fc != null && _fc.SourceType != null) { _objType = _fc.SourceType; } if (RaiseBeforeRecordFieldWrite(rec, crs.Index, name, ref value)) { if (_fc != null) { if (value != null && _objType != null) { value = ChoConvert.ConvertTo(value, _objType, null, _fc.PropConverters, _fc.PropConverterParams, _culture); } if (_fc.CustomSerializer == null) { if (_fc.ValueConverter == null) { var t = serializer.SerializeToJToken(value); t.WriteTo(writer); } else { object retValue = _fc.ValueConverter(value); ValidateOnWrite(ref retValue); //ChoETLRecordHelper.DoMemberLevelValidation(retValue, _fc.Name, _fc, _validationMode); JToken t = JToken.FromObject(retValue, serializer); t.WriteTo(writer); } } else { object retValue = _fc.CustomSerializer(writer); ValidateOnWrite(ref retValue); JToken t = JToken.FromObject(retValue, serializer); t.WriteTo(writer); } } else { if (value != null && _objType != null) { value = ChoConvert.ConvertTo(value, _objType, null, ChoTypeDescriptor.GetTypeConverters(_mi), ChoTypeDescriptor.GetTypeConverterParams(_mi), _culture); } if (ValidateOnWrite(ref value)) { var t = serializer.SerializeToJToken(value); t.WriteTo(writer); } else { JToken t = JToken.FromObject(null, serializer); t.WriteTo(writer); } } RaiseAfterRecordFieldWrite(rec, crs.Index, name, value); } else { JToken t = JToken.FromObject(null, serializer); t.WriteTo(writer); } }
private bool FillRecord(ref object rec, Tuple <long, IDictionary <string, object> > pair) { long lineNo; lineNo = pair.Item1; var node = pair.Item2; if (Configuration.LiteParsing && Configuration.IsDynamicObject && rec is ChoDynamicObject) { ((ChoDynamicObject)rec).SetDictionary(node as IDictionary <string, object>); return(true); } fieldValue = null; fieldConfig = null; pi = null; object rootRec = rec; foreach (KeyValuePair <string, ChoParquetRecordFieldConfiguration> kvp in Configuration.RecordFieldConfigurationsDict) { if (!Configuration.SupportsMultiRecordTypes && Configuration.IsDynamicObject) { if (Configuration.IgnoredFields.Contains(kvp.Key)) { continue; } } fieldValue = null; fieldConfig = kvp.Value; if (Configuration.PIDict != null) { // if FieldName is set if (!string.IsNullOrEmpty(fieldConfig.FieldName)) { // match using FieldName Configuration.PIDict.TryGetValue(fieldConfig.FieldName, out pi); } if (pi == null) { // otherwise match usign the property name Configuration.PIDict.TryGetValue(kvp.Key, out pi); } } rec = GetDeclaringRecord(kvp.Value.DeclaringMember, rootRec); if (!node.ContainsKey(kvp.Value.FieldName)) { if (Configuration.ColumnCountStrict) { throw new ChoParserException("No matching '{0}' field found.".FormatString(fieldConfig.FieldName)); } } else { fieldValue = node[kvp.Value.FieldName]; } if (!RaiseBeforeRecordFieldLoad(rec, pair.Item1, kvp.Key, ref fieldValue)) { continue; } try { if (!Configuration.SupportsMultiRecordTypes && Configuration.IsDynamicObject) { } else { if (pi != null) { kvp.Value.FieldType = pi.PropertyType; } else { kvp.Value.FieldType = typeof(string); } } object v1 = node; if (fieldConfig.CustomSerializer != null) { fieldValue = fieldConfig.CustomSerializer(v1); } else if (RaiseRecordFieldDeserialize(rec, pair.Item1, kvp.Key, ref v1)) { fieldValue = v1; } else if (fieldConfig.PropCustomSerializer != null) { fieldValue = ChoCustomSerializer.Deserialize(v1, fieldConfig.FieldType, fieldConfig.PropCustomSerializer, fieldConfig.PropCustomSerializerParams, Configuration.Culture, fieldConfig.Name); } else { if (!node.ContainsKey(kvp.Value.FieldName)) { if (Configuration.ColumnCountStrict) { throw new ChoParserException("No matching '{0}' field found.".FormatString(fieldConfig.FieldName)); } } else { fieldValue = node[kvp.Value.FieldName]; } } if (fieldConfig.IgnoreFieldValueMode == null) { if (fieldValue.IsObjectNullOrEmpty() && fieldConfig.IsDefaultValueSpecified) { fieldValue = fieldConfig.DefaultValue; } } else { bool ignoreFieldValue = fieldValue.IgnoreFieldValue(fieldConfig.IgnoreFieldValueMode); if (ignoreFieldValue && fieldConfig.IsDefaultValueSpecified) { fieldValue = fieldConfig.DefaultValue; } ignoreFieldValue = fieldValue.IgnoreFieldValue(fieldConfig.IgnoreFieldValueMode); if (ignoreFieldValue) { continue; } } if (!Configuration.SupportsMultiRecordTypes && Configuration.IsDynamicObject) { var dict = rec as IDictionary <string, Object>; dict.ConvertNSetMemberValue(kvp.Key, kvp.Value, ref fieldValue, Configuration.Culture); if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel) { dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode); } } else { if (Configuration.SupportsMultiRecordTypes) { ChoType.TryGetProperty(rec.GetType(), kvp.Key, out pi); //*** TODO //fieldConfig.PI = pi; //fieldConfig.PropConverters = ChoTypeDescriptor.GetTypeConverters(fieldConfig.PI); //fieldConfig.PropConverterParams = ChoTypeDescriptor.GetTypeConverterParams(fieldConfig.PI); } if (pi != null) { if (Configuration.LiteParsing) { ChoType.SetPropertyValue(rec, fieldConfig.PI, fieldConfig.FieldType == null || fieldConfig.FieldType == typeof(string) ? fieldValue : Convert.ChangeType(fieldValue, fieldConfig.FieldType, Configuration.Culture)); } else { rec.ConvertNSetMemberValue(kvp.Key, kvp.Value, ref fieldValue, Configuration.Culture); } } else if (RecordType.IsSimple()) { rec = ChoConvert.ConvertTo(fieldValue, RecordType, Configuration.Culture); } else { throw new ChoMissingRecordFieldException("Missing '{0}' property in {1} type.".FormatString(kvp.Key, ChoType.GetTypeName(rec))); } if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel) { rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode); } } if (!RaiseAfterRecordFieldLoad(rec, pair.Item1, kvp.Key, fieldValue)) { return(false); } } catch (ChoParserException) { Reader.IsValid = false; throw; } catch (ChoMissingRecordFieldException) { Reader.IsValid = false; if (Configuration.ThrowAndStopOnMissingField) { throw; } } catch (Exception ex) { Reader.IsValid = false; ChoETLFramework.HandleException(ref ex); if (fieldConfig.ErrorMode == ChoErrorMode.ThrowAndStop) { throw; } try { if (!Configuration.SupportsMultiRecordTypes && Configuration.IsDynamicObject) { var dict = rec as IDictionary <string, Object>; if (dict.SetFallbackValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue)) { dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode); } else if (dict.SetDefaultValue(kvp.Key, kvp.Value, Configuration.Culture)) { dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode); } else if (ex is ValidationException) { throw; } else { throw new ChoReaderException($"Failed to parse '{fieldValue}' value for '{fieldConfig.FieldName}' field.", ex); } } else if (pi != null) { if (rec.SetFallbackValue(kvp.Key, kvp.Value, Configuration.Culture)) { rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode); } else if (rec.SetDefaultValue(kvp.Key, kvp.Value, Configuration.Culture)) { rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode); } else if (ex is ValidationException) { throw; } else { throw new ChoReaderException($"Failed to parse '{fieldValue}' value for '{fieldConfig.FieldName}' field.", ex); } } else { throw new ChoReaderException($"Failed to parse '{fieldValue}' value for '{fieldConfig.FieldName}' field.", ex); } } catch (Exception innerEx) { if (ex == innerEx.InnerException || ex is ValidationException) { if (fieldConfig.ErrorMode == ChoErrorMode.IgnoreAndContinue) { ChoETLFramework.WriteLog(TraceSwitch.TraceError, "Error [{0}] found. Ignoring field...".FormatString(ex.Message)); continue; } else { if (!RaiseRecordFieldLoadError(rec, pair.Item1, kvp.Key, ref fieldValue, ex)) { if (ex is ValidationException) { throw; } throw new ChoReaderException($"Failed to parse '{fieldValue}' value for '{fieldConfig.FieldName}' field.", ex); } else { try { if (Configuration.IsDynamicObject) { var dict = rec as IDictionary <string, Object>; dict.ConvertNSetMemberValue(kvp.Key, fieldConfig, ref fieldValue, Configuration.Culture); } else { if (pi != null) { rec.ConvertNSetMemberValue(kvp.Key, fieldConfig, ref fieldValue, Configuration.Culture); } else { throw new ChoMissingRecordFieldException("Missing '{0}' property in {1} type.".FormatString(kvp.Key, ChoType.GetTypeName(rec))); } } } catch { } } } } else { throw new ChoReaderException("Failed to assign '{0}' fallback value to '{1}' field.".FormatString(fieldValue, fieldConfig.FieldName), innerEx); } } } } rec = rootRec; return(true); }
private bool FillRecord(object rec, Tuple <long, XElement> pair) { long lineNo; XElement node; lineNo = pair.Item1; node = pair.Item2; fXElements = null; fieldValue = null; fieldConfig = null; pi = null; xpn = node.CreateNavigator(Configuration.NamespaceManager.NameTable); ToDictionary(node); foreach (KeyValuePair <string, ChoXmlRecordFieldConfiguration> kvp in Configuration.RecordFieldConfigurationsDict) { fieldValue = null; fieldConfig = kvp.Value; if (Configuration.PIDict != null) { Configuration.PIDict.TryGetValue(kvp.Key, out pi); } if (fieldConfig.XPath == "text()") { if (Configuration.GetNameWithNamespace(node.Name) == fieldConfig.FieldName) { object value = node; if (!RaiseBeforeRecordFieldLoad(rec, pair.Item1, kvp.Key, ref value)) { continue; } if (fieldConfig.ValueConverter != null) { value = fieldConfig.ValueConverter(value); } fieldValue = value is XElement ? ((XElement)value).ToObjectFromXml(typeof(ChoDynamicObject)) : value; //fieldValue = value is XElement ? node.Value : value; } else if (Configuration.ColumnCountStrict) { throw new ChoParserException("Missing '{0}' xml node.".FormatString(fieldConfig.FieldName)); } } else { if (/*!fieldConfig.UseCache && */ !xDict.ContainsKey(fieldConfig.FieldName)) { xNodes.Clear(); foreach (XPathNavigator z in xpn.Select(fieldConfig.XPath, Configuration.NamespaceManager)) { xNodes.Add(z.UnderlyingObject); } object value = xNodes; if (!RaiseBeforeRecordFieldLoad(rec, pair.Item1, kvp.Key, ref value)) { continue; } if (fieldConfig.ValueConverter != null) { fieldValue = fieldConfig.ValueConverter(value); } else { //object[] xNodes = ((IEnumerable)node.XPathEvaluate(fieldConfig.XPath, Configuration.NamespaceManager)).OfType<object>().ToArray(); //continue; XAttribute[] fXAttributes = xNodes.OfType <XAttribute>().ToArray(); if (!fXAttributes.IsNullOrEmpty()) //fXAttribute != null) { //fieldValue = fXAttribute.Value; if (fieldConfig.FieldType == null) { if (fXAttributes.Length == 1) { if (fieldConfig.ItemConverter != null) { fieldValue = fieldConfig.ItemConverter(fXAttributes[0]); } else { fieldValue = fXAttributes[0].Value; } } else { List <object> arr = new List <object>(); foreach (var ele in fXAttributes) { if (fieldConfig.ItemConverter != null) { arr.Add(fieldConfig.ItemConverter(ele)); } else { arr.Add(ele.Value); } } fieldValue = arr.ToArray(); } } else if (fieldConfig.FieldType == typeof(string) || fieldConfig.FieldType.IsSimple()) { XAttribute fXElement = fXAttributes.FirstOrDefault(); if (fXElement != null) { if (fieldConfig.ItemConverter != null) { fieldValue = fieldConfig.ItemConverter(fXElement); } else { fieldValue = fXElement.Value; } } } else if (fieldConfig.FieldType.IsCollection()) { List <object> list = new List <object>(); Type itemType = fieldConfig.FieldType.GetItemType().GetUnderlyingType(); foreach (var ele in fXAttributes) { if (fieldConfig.ItemConverter != null) { list.Add(fieldConfig.ItemConverter(ele)); } else { if (itemType.IsSimple()) { list.Add(ChoConvert.ConvertTo(ele.Value, itemType)); } else { list.Add(ele.Value); } } } fieldValue = list.ToArray(); } else { XAttribute fXElement = fXAttributes.FirstOrDefault(); if (fXElement != null) { if (fieldConfig.ItemConverter != null) { fieldValue = fieldConfig.ItemConverter(fXElement); } else { fieldValue = fXElement.Value; } } } } else { fXElements = xNodes.OfType <XElement>().ToArray(); if (!fXElements.IsNullOrEmpty()) { if (fieldConfig.IsArray != null && fieldConfig.IsArray.Value) { List <object> list = new List <object>(); Type itemType = fieldConfig.FieldType != null?fieldConfig.FieldType.GetItemType().GetUnderlyingType() : typeof(ChoDynamicObject); foreach (var ele in fXElements) { if (fieldConfig.ItemConverter != null) { list.Add(fieldConfig.ItemConverter(ele)); } else { if (itemType.IsSimple()) { list.Add(ChoConvert.ConvertTo(ele.Value, itemType)); } else { if (itemType == typeof(ChoDynamicObject)) { list.Add(ele.ToDynamic()); } else { list.Add(ele.ToObjectFromXml(itemType, GetXmlOverrides(fieldConfig))); } } } } fieldValue = list.ToArray(); } else { if (fieldConfig.FieldType == null || fieldConfig.FieldType == typeof(object) || fieldConfig.FieldType.GetItemType() == typeof(object)) { if (fXElements.Length == 1) { if (fieldConfig.ItemConverter != null) { fieldValue = fieldConfig.ItemConverter(fXElements[0]); } else { fieldValue = fXElements[0].ToObjectFromXml(typeof(ChoDynamicObject)); } } else { List <object> arr = new List <object>(); foreach (var ele in fXElements) { if (fieldConfig.ItemConverter != null) { arr.Add(fieldConfig.ItemConverter(ele)); } else { arr.Add(ele.ToObjectFromXml(typeof(ChoDynamicObject)) as ChoDynamicObject); } } fieldValue = arr.ToArray(); } } else if (fieldConfig.FieldType == typeof(string) || fieldConfig.FieldType.IsSimple()) { XElement fXElement = fXElements.FirstOrDefault(); if (fXElement != null) { if (fieldConfig.ItemConverter != null) { fieldValue = fieldConfig.ItemConverter(fXElement); } else { fieldValue = fXElement.Value; } } } else if (fieldConfig.FieldType.IsCollection()) { List <object> list = new List <object>(); Type itemType = fieldConfig.FieldType.GetItemType().GetUnderlyingType(); //if (!itemType.IsSimple()) //{ // fXElements = fXElements.SelectMany(e => e.Elements()).ToArray(); //} foreach (var ele in fXElements) { if (fieldConfig.ItemConverter != null) { list.Add(fieldConfig.ItemConverter(ele)); } else { if (itemType.IsSimple()) { list.Add(ChoConvert.ConvertTo(ele.Value, itemType)); } else { list.Add(ele.ToObjectFromXml(itemType, null)); } } } fieldValue = list.ToArray(); } else { XElement fXElement = fXElements.FirstOrDefault(); //.SelectMany(e => e.Elements()).FirstOrDefault(); if (fXElement != null) { if (fieldConfig.ItemConverter != null) { fieldValue = fieldConfig.ItemConverter(fXElement); } else { fieldValue = fXElement.ToObjectFromXml(fieldConfig.FieldType, null); } } } } } else { XText[] xTexts = xNodes.OfType <XText>().ToArray(); if (!xTexts.IsNullOrEmpty()) { if (fieldConfig.FieldType == null) { if (xTexts.Length == 1) { if (fieldConfig.ItemConverter != null) { fieldValue = fieldConfig.ItemConverter(xTexts[0]); } else { fieldValue = xTexts[0].Value; } } else { List <object> arr = new List <object>(); foreach (var ele in xTexts) { if (fieldConfig.ItemConverter != null) { arr.Add(fieldConfig.ItemConverter(ele)); } else { arr.Add(ele.Value); } } fieldValue = arr.ToArray(); } } else if (fieldConfig.FieldType == typeof(string) || fieldConfig.FieldType.IsSimple()) { XText fXElement = xTexts.FirstOrDefault(); if (fXElement != null) { if (fieldConfig.ItemConverter != null) { fieldValue = fieldConfig.ItemConverter(fXElement); } else { fieldValue = fXElement.Value; } } } else if (fieldConfig.FieldType.IsCollection()) { List <object> list = new List <object>(); Type itemType = fieldConfig.FieldType.GetItemType().GetUnderlyingType(); foreach (var ele in xTexts) { if (fieldConfig.ItemConverter != null) { list.Add(fieldConfig.ItemConverter(ele)); } else { if (itemType.IsSimple()) { list.Add(ChoConvert.ConvertTo(ele.Value, itemType)); } else { list.Add(ele.Value); } } } fieldValue = list.ToArray(); } else { XText fXElement = xTexts.FirstOrDefault(); if (fXElement != null) { if (fieldConfig.ItemConverter != null) { fieldValue = fieldConfig.ItemConverter(fXElement); } else { fieldValue = fXElement.Value; } } } } else if (Configuration.ColumnCountStrict) { throw new ChoParserException("Missing '{0}' xml node.".FormatString(fieldConfig.FieldName)); } } } } } else { if (xDict[fieldConfig.FieldName].Count == 1) { fieldValue = xDict[fieldConfig.FieldName][0]; } else { fieldValue = xDict[fieldConfig.FieldName]; } if (!RaiseBeforeRecordFieldLoad(rec, pair.Item1, kvp.Key, ref fieldValue)) { continue; } if (fieldConfig.ValueConverter != null) { fieldValue = fieldConfig.ValueConverter(fieldValue); } } } if (Configuration.IsDynamicObject) { if (fieldValue != null && kvp.Value.FieldType == null && lineNo == 1) { kvp.Value.FieldType = fieldValue is ICollection?fieldValue.GetType() : fieldValue.GetType().IsSimple() ? DiscoverFieldType(fieldValue as string) : null; } } else { if (pi != null) { kvp.Value.FieldType = pi.PropertyType; } else { kvp.Value.FieldType = typeof(string); } } if (fieldValue is string) { fieldValue = CleanFieldValue(fieldConfig, kvp.Value.FieldType, fieldValue as string, kvp.Value.EncodeValue); } try { bool ignoreFieldValue = fieldConfig.IgnoreFieldValue(fieldValue); if (ignoreFieldValue) { fieldValue = fieldConfig.IsDefaultValueSpecified ? fieldConfig.DefaultValue : null; } if (Configuration.IsDynamicObject) { var dict = rec as IDictionary <string, Object>; if (!fieldConfig.IsArray.CastTo <bool>()) { dict.ConvertNSetMemberValue(kvp.Key, kvp.Value, ref fieldValue, Configuration.Culture); } else { dict[kvp.Key] = fieldValue; } if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel) { dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode); } } else { if (pi != null) { rec.ConvertNSetMemberValue(kvp.Key, kvp.Value, ref fieldValue, Configuration.Culture); } else { throw new ChoMissingRecordFieldException("Missing '{0}' property in {1} type.".FormatString(kvp.Key, ChoType.GetTypeName(rec))); } if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel) { rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode); } } if (!RaiseAfterRecordFieldLoad(rec, pair.Item1, kvp.Key, fieldValue)) { return(false); } } catch (ChoParserException) { Reader.IsValid = false; throw; } catch (ChoMissingRecordFieldException) { Reader.IsValid = false; if (Configuration.ThrowAndStopOnMissingField) { throw; } } catch (Exception ex) { Reader.IsValid = false; ChoETLFramework.HandleException(ref ex); if (fieldConfig.ErrorMode == ChoErrorMode.ThrowAndStop) { throw; } try { if (Configuration.IsDynamicObject) { var dict = rec as IDictionary <string, Object>; if (dict.SetFallbackValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue)) { dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode); } else if (dict.SetDefaultValue(kvp.Key, kvp.Value, Configuration.Culture)) { dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode); } else { throw new ChoReaderException($"Failed to parse '{fieldValue}' value for '{fieldConfig.FieldName}' field.", ex); } } else if (pi != null) { if (rec.SetFallbackValue(kvp.Key, kvp.Value, Configuration.Culture)) { rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode); } else if (rec.SetDefaultValue(kvp.Key, kvp.Value, Configuration.Culture)) { rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode); } else { throw new ChoReaderException($"Failed to parse '{fieldValue}' value for '{fieldConfig.FieldName}' field.", ex); } } else { throw new ChoReaderException($"Failed to parse '{fieldValue}' value for '{fieldConfig.FieldName}' field.", ex); } } catch (Exception innerEx) { if (ex == innerEx.InnerException) { if (fieldConfig.ErrorMode == ChoErrorMode.IgnoreAndContinue) { continue; } else { if (!RaiseRecordFieldLoadError(rec, pair.Item1, kvp.Key, fieldValue, ex)) { throw new ChoReaderException($"Failed to parse '{fieldValue}' value for '{fieldConfig.FieldName}' field.", ex); } } } else { throw new ChoReaderException("Failed to assign '{0}' fallback value to '{1}' field.".FormatString(fieldValue, fieldConfig.FieldName), innerEx); } } } } return(true); }
private bool ToText(long index, object rec, out string recText) { if (typeof(IChoScalarObject).IsAssignableFrom(Configuration.RecordType)) { rec = ChoActivator.CreateInstance(Configuration.RecordType, rec); } if (!Configuration.IsDynamicObject) { if (rec.ToTextIfCustomSerialization(out recText)) { return(true); } //Check if KVP object if (rec.GetType().IsKeyValueType()) { recText = SerializeObject(rec); return(true); } } recText = null; if (Configuration.UseYamlSerialization) { recText = new Serializer().Serialize(rec, rec.GetType()); return(true); } StringBuilder msg = new StringBuilder(); object fieldValue = null; string fieldText = null; ChoYamlRecordFieldConfiguration fieldConfig = null; string fieldName = null; if (Configuration.ColumnCountStrict) { CheckColumnsStrict(rec); } //bool firstColumn = true; PropertyInfo pi = null; bool isFirst = true; object rootRec = rec; Dictionary <string, object> output = new Dictionary <string, object>(); foreach (KeyValuePair <string, ChoYamlRecordFieldConfiguration> kvp in Configuration.RecordFieldConfigurationsDict.OrderBy(kvp => kvp.Value.Order)) { //if (Configuration.IsDynamicObject) //{ if (Configuration.IgnoredFields.Contains(kvp.Key)) { continue; } //} fieldConfig = kvp.Value; fieldName = fieldConfig.FieldName; fieldValue = null; fieldText = String.Empty; if (Configuration.PIDict != null) { Configuration.PIDict.TryGetValue(kvp.Key, out pi); } rec = GetDeclaringRecord(kvp.Value.DeclaringMember, rootRec); if (Configuration.ThrowAndStopOnMissingField) { if (Configuration.IsDynamicObject) { var dict = rec.ToDynamicObject() as IDictionary <string, Object>; if (!dict.ContainsKey(kvp.Key)) { throw new ChoMissingRecordFieldException("No matching property found in the object for '{0}' Yaml node.".FormatString(fieldConfig.FieldName)); } } else { if (pi == null) { if (!RecordType.IsSimple()) { throw new ChoMissingRecordFieldException("No matching property found in the object for '{0}' Yaml node.".FormatString(fieldConfig.FieldName)); } } } } try { if (Configuration.IsDynamicObject) { IDictionary <string, Object> dict = rec.ToDynamicObject() as IDictionary <string, Object>; fieldValue = dict[kvp.Key]; // dict.GetValue(kvp.Key, Configuration.FileHeaderConfiguration.IgnoreCase, Configuration.Culture); if (rec is ChoDynamicObject) { } if (kvp.Value.FieldType == null) { if (rec is ChoDynamicObject) { var dobj = rec as ChoDynamicObject; kvp.Value.FieldType = dobj.GetMemberType(kvp.Key); } if (kvp.Value.FieldType == null) { if (ElementType == null) { kvp.Value.FieldType = typeof(object); } else { kvp.Value.FieldType = ElementType; } } } } else { if (pi != null) { fieldValue = ChoType.GetPropertyValue(rec, pi); if (kvp.Value.FieldType == null) { kvp.Value.FieldType = pi.PropertyType; } } else { kvp.Value.FieldType = typeof(string); } } //Discover default value, use it if null //if (fieldValue == null) //{ // if (fieldConfig.IsDefaultValueSpecified) // fieldValue = fieldConfig.DefaultValue; //} bool ignoreFieldValue = fieldValue.IgnoreFieldValue(fieldConfig.IgnoreFieldValueMode); if (ignoreFieldValue) { fieldValue = fieldConfig.IsDefaultValueSpecified ? fieldConfig.DefaultValue : null; } if (!RaiseBeforeRecordFieldWrite(rec, index, kvp.Key, ref fieldValue)) { return(false); } if (fieldConfig.ValueConverter != null) { fieldValue = fieldConfig.ValueConverter(fieldValue); } else if (RecordType.IsSimple()) { fieldValue = new List <object> { rec } } ; else { rec.GetNConvertMemberValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue, true); } if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.ObjectLevel) == ChoObjectValidationMode.MemberLevel) { rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode, fieldValue); } if (!RaiseAfterRecordFieldWrite(rec, index, kvp.Key, fieldValue)) { return(false); } } catch (ChoParserException) { throw; } catch (ChoMissingRecordFieldException) { if (Configuration.ThrowAndStopOnMissingField) { throw; } } catch (Exception ex) { ChoETLFramework.HandleException(ref ex); if (fieldConfig.ErrorMode == ChoErrorMode.ThrowAndStop) { throw; } try { if (Configuration.IsDynamicObject) { var dict = rec.ToDynamicObject() as IDictionary <string, Object>; if (dict.GetFallbackValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue)) { dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode, fieldValue); } else if (dict.GetDefaultValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue)) { dict.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode, fieldValue); } else { var ex1 = new ChoWriterException($"Failed to write '{fieldValue}' value for '{fieldConfig.FieldName}' member.", ex); fieldValue = null; throw ex1; } } else if (pi != null) { if (rec.GetFallbackValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue)) { rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode); } else if (rec.GetDefaultValue(kvp.Key, kvp.Value, Configuration.Culture, ref fieldValue)) { rec.DoMemberLevelValidation(kvp.Key, kvp.Value, Configuration.ObjectValidationMode, fieldValue); } else { var ex1 = new ChoWriterException($"Failed to write '{fieldValue}' value for '{fieldConfig.FieldName}' member.", ex); fieldValue = null; throw ex1; } } else { var ex1 = new ChoWriterException($"Failed to write '{fieldValue}' value for '{fieldConfig.FieldName}' member.", ex); fieldValue = null; throw ex1; } } catch (Exception innerEx) { if (ex == innerEx.InnerException) { if (fieldConfig.ErrorMode == ChoErrorMode.IgnoreAndContinue) { continue; } else { if (!RaiseRecordFieldWriteError(rec, index, kvp.Key, ref fieldValue, ex)) { throw new ChoWriterException($"Failed to write '{fieldValue}' value of '{kvp.Key}' member.", ex); } } } else { throw new ChoWriterException("Failed to use '{0}' fallback value for '{1}' member.".FormatString(fieldValue, kvp.Key), innerEx); } } } bool isSimple = true; if (fieldConfig.CustomSerializer != null) { fieldText = fieldConfig.CustomSerializer(fieldValue) as string; } else if (RaiseRecordFieldSerialize(rec, index, kvp.Key, ref fieldValue)) { fieldText = fieldValue as string; } else if (fieldConfig.PropCustomSerializer != null) { fieldText = ChoCustomSerializer.Serialize(fieldValue, typeof(string), fieldConfig.PropCustomSerializer, fieldConfig.PropCustomSerializerParams, Configuration.Culture, fieldConfig.Name) as string; } else { Type ft = fieldValue == null ? typeof(object) : fieldValue.GetType(); //if (fieldConfig.IgnoreFieldValue(fieldValue)) // fieldText = null; bool ignoreFieldValue = fieldValue.IgnoreFieldValue(fieldConfig.IgnoreFieldValueMode); if (ignoreFieldValue) { fieldText = null; } else if (fieldValue == null) { //if (fieldConfig.FieldType == null || fieldConfig.FieldType == typeof(object)) //{ // if (fieldConfig.NullValue == null) // fieldText = !fieldConfig.IsArray ? "null" : "[]"; // else // fieldText = fieldConfig.NullValue; //} if (Configuration.NullValueHandling == ChoNullValueHandling.Ignore) { fieldText = null; } else if (Configuration.NullValueHandling == ChoNullValueHandling.Default) { fieldText = ChoActivator.CreateInstance(fieldConfig.FieldType).ToNString(); } else if (Configuration.NullValueHandling == ChoNullValueHandling.Empty && fieldConfig.FieldType == typeof(string)) { fieldText = String.Empty; } else { if (fieldConfig.NullValue == null) { } else { fieldText = fieldConfig.NullValue; } } } else if (ft == typeof(string) || ft == typeof(char)) { fieldText = NormalizeFieldValue(kvp.Key, fieldValue.ToString(), kvp.Value.Size, kvp.Value.Truncate, false, GetFieldValueJustification(kvp.Value.FieldValueJustification, kvp.Value.FieldType), GetFillChar(kvp.Value.FillChar, kvp.Value.FieldType), false, kvp.Value.GetFieldValueTrimOption(kvp.Value.FieldType, Configuration.FieldValueTrimOption)); } else if (ft == typeof(DateTime) || ft == typeof(TimeSpan)) { fieldText = ChoConvert.ConvertTo(fieldValue, typeof(String), Configuration.Culture) as string; } else if (ft.IsEnum) { fieldText = ChoConvert.ConvertTo(fieldValue, typeof(String), Configuration.Culture) as string; } else if (ft == typeof(ChoCurrency)) { fieldText = fieldValue.ToString(); } else if (ft == typeof(bool)) { fieldText = ChoConvert.ConvertTo(fieldValue, typeof(String), Configuration.Culture) as string; } else if (ft.IsNumeric()) { fieldText = ChoConvert.ConvertTo(fieldValue, typeof(String), Configuration.Culture) as string; } else { isSimple = false; } } object objValue = null; if (isSimple) { objValue = fieldText; } else { Writer.ContractResolverState = new ChoContractResolverState { Name = kvp.Key, Index = index, Record = rec, FieldConfig = kvp.Value }; var json = JsonConvert.SerializeObject(fieldValue, Configuration.JsonSerializerSettings); if (RecordType.IsSimple()) { objValue = JsonConvert.DeserializeObject <IList <object> >(json); } else { objValue = JsonConvert.DeserializeObject <IDictionary <string, object> >(json); } } if (!RecordType.IsSimple()) { output.Add(fieldName, objValue); } else { fieldValue = objValue; } } if (!RecordType.IsSimple()) { recText = Configuration.YamlSerializer.Serialize(output, output.GetType()); } else { recText = Configuration.YamlSerializer.Serialize(fieldValue, fieldValue.GetType()); } return(true); }
public static object ConvertTo(object value, MemberInfo memberInfo, Type targetType, object sourceObject = null, CultureInfo culture = null) { ChoGuard.ArgumentNotNull((object)memberInfo, "MemberInfo"); return(ChoConvert.ConvertTo(value, targetType, sourceObject, ChoTypeDescriptor.GetTypeConverters(memberInfo), ChoTypeDescriptor.GetTypeConverterParams(memberInfo), culture)); }
public static T Map <T>(this IChoCSVLiteReader parser, int lineNo, string[] cols, Action <int, string[], T> mapper, bool hasHeader, ref bool skip, ref string[] headers, bool positionalMapping) where T : new() { skip = false; if (mapper != null) { if (hasHeader) { if (lineNo == 0) { skip = true; return(default(T)); } } var rec = parser.CreateInstance <T>(); mapper(lineNo, cols, rec); return(rec); } else { if (hasHeader) { if (lineNo == 0) { headers = cols.Select((c, i) => c).ToArray(); skip = true; return(default(T)); } } else { if (lineNo == 0) { headers = cols.Select((c, i) => $"Column{i}").ToArray(); } } var recType = typeof(T); if (recType == typeof(ChoDynamicObject) || recType == typeof(ExpandoObject)) { if (recType == typeof(ChoDynamicObject)) { var rec = parser.CreateInstance <T>(); dynamic dObj = rec; var dict = headers.Select((h, i) => new { Key = h, Value = cols[i] }).ToDictionary(kvp => kvp.Key, kvp => (object)kvp.Value); dObj.SetDictionary(dict); return(rec); } else { var expando = parser.CreateInstance <T>(); var expandoDic = (IDictionary <string, object>)expando; int index = 0; foreach (var header in headers) { expandoDic.Add(header, cols[index++]); } return(expando); } } else { var rec = parser.CreateInstance <T>(); if (positionalMapping) { PropertyInfo[] props = GetPropertyInfos <T>(); int index = 0; string col = null; Type propType = null; foreach (var prop in props) { if (index < cols.Length) { propType = props[index].PropertyType; col = cols[index++]; ChoType.SetPropertyValue(rec, prop, ChoConvert.ConvertTo(col, propType)); } else { break; } } } else { Dictionary <string, PropertyInfo> propDict = GetPropertyInfoDictionary <T>(); int index = 0; string col = null; Type propType = null; PropertyInfo prop = null; foreach (var header in headers) { if (propDict.ContainsKey(header)) { prop = propDict[header]; if (prop != null) { propType = prop.PropertyType; col = cols[index++]; ChoType.SetPropertyValue(rec, prop, ChoConvert.ConvertTo(col, propType)); } } } } return(rec); } } }
private bool FillRecord(object rec, Tuple <int, string> pair) { int lineNo; string line; lineNo = pair.Item1; line = pair.Item2; object fieldValue = null; string[] fieldValues = (from x in line.Split(Configuration.Delimiter, Configuration.StringSplitOptions, Configuration.QuoteChar) select x).ToArray(); if (Configuration.ColumnCountStrict) { if (fieldValues.Length != Configuration.RecordFieldConfigurations.Count) { throw new ChoParserException("Incorrect number of field values found at line [{2}]. Expected [{0}] field values. Found [{1}] field values.".FormatString(Configuration.RecordFieldConfigurations.Count, fieldValues.Length, pair.Item1)); } } Dictionary <string, string> _fieldNameValues = ToFieldNameValues(fieldValues); ValidateLine(pair.Item1, fieldValues); ChoCSVRecordFieldConfiguration fieldConfig = null; foreach (KeyValuePair <string, ChoCSVRecordFieldConfiguration> kvp in Configuration.RecordFieldConfigurationsDict) { fieldConfig = kvp.Value; if (Configuration.CSVFileHeaderConfiguration.HasHeaderRecord) { if (_fieldNameValues.ContainsKey(fieldConfig.FieldName)) { fieldValue = _fieldNameValues[fieldConfig.FieldName]; } else if (Configuration.ColumnCountStrict) { throw new ChoParserException("No matching '{0}' field header found.".FormatString(fieldConfig.FieldName)); } } else { if (fieldConfig.FieldPosition - 1 < fieldValues.Length) { fieldValue = fieldValues[fieldConfig.FieldPosition - 1]; } else if (Configuration.ColumnCountStrict) { throw new ChoParserException("Missing field value for {0} [Position: {1}] field.".FormatString(fieldConfig.FieldName, fieldConfig.FieldPosition)); } } fieldValue = CleanFieldValue(fieldConfig, fieldValue as string); if (!RaiseBeforeRecordFieldLoad(rec, pair.Item1, kvp.Key, ref fieldValue)) { continue; } try { bool ignoreFieldValue = fieldConfig.IgnoreFieldValue(fieldValue); if (!ignoreFieldValue) { if (rec is ExpandoObject) { if (fieldConfig.FieldType != typeof(string)) { fieldValue = ChoConvert.ConvertTo(fieldValue, fieldConfig.FieldType, Configuration.Culture); } var x = rec as IDictionary <string, Object>; x.Add(kvp.Key, fieldValue); } else { if (ChoType.HasProperty(rec.GetType(), kvp.Key)) { ChoType.ConvertNSetMemberValue(rec, kvp.Key, fieldValue); fieldValue = ChoType.GetMemberValue(rec, kvp.Key); if ((Configuration.ObjectValidationMode & ChoObjectValidationMode.MemberLevel) == ChoObjectValidationMode.MemberLevel) { ChoValidator.ValididateFor(rec, kvp.Key); } } else { throw new ChoMissingRecordFieldException("Missing '{0}' property in {1} type.".FormatString(kvp.Key, ChoType.GetTypeName(rec))); } } } if (!RaiseAfterRecordFieldLoad(rec, pair.Item1, kvp.Key, fieldValue)) { return(false); } } catch (ChoParserException) { throw; } catch (ChoMissingRecordFieldException) { if (Configuration.ThrowAndStopOnMissingField) { throw; } } catch (Exception ex) { ChoETLFramework.HandleException(ex); if (fieldConfig.ErrorMode == ChoErrorMode.ThrowAndStop) { throw; } try { ChoFallbackValueAttribute fbAttr = ChoTypeDescriptor.GetPropetyAttribute <ChoFallbackValueAttribute>(rec.GetType(), kvp.Key); if (fbAttr != null) { if (!fbAttr.Value.IsNullOrDbNull()) { ChoType.ConvertNSetMemberValue(rec, kvp.Key, fbAttr.Value); } } else { throw; } } catch { if (fieldConfig.ErrorMode == ChoErrorMode.IgnoreAndContinue) { continue; } else if (fieldConfig.ErrorMode == ChoErrorMode.ReportAndContinue) { if (!RaiseRecordFieldLoadError(rec, pair.Item1, kvp.Key, fieldValue, ex)) { throw; } } else { throw; } } } } return(true); }
public static JToken SerializeToJToken(this JsonSerializer serializer, object value, Formatting?formatting = null, JsonSerializerSettings settings = null, bool dontUseConverter = false, bool enableXmlAttributePrefix = false) { JsonConverter conv = null; if (!dontUseConverter) { Type vt = value != null?value.GetType() : typeof(object); var convName = GetTypeConverterName(vt); conv = serializer.Converters.Where(c => c.GetType().Name == convName || (c.GetType().IsGenericType&& c.GetType().GetGenericArguments()[0] == vt)).FirstOrDefault(); if (conv == null && ChoJSONConvertersCache.IsInitialized) { if (ChoJSONConvertersCache.Contains(convName)) { conv = ChoJSONConvertersCache.Get(convName); } else if (ChoJSONConvertersCache.Contains(vt)) { conv = ChoJSONConvertersCache.Get(vt); } } } if (value != null) { if (!value.GetType().IsSimple()) { bool disableImplcityOp = false; if (ChoTypeDescriptor.GetTypeAttribute <ChoTurnOffImplicitOpsAttribute>(value.GetType()) != null) { disableImplcityOp = ChoTypeDescriptor.GetTypeAttribute <ChoTurnOffImplicitOpsAttribute>(value.GetType()).Flag; } if (!disableImplcityOp) { Type to = null; if (value.GetType().CanCastToPrimitiveType(out to)) { value = ChoConvert.ConvertTo(value, to); } else if (value.GetType().GetImplicitTypeCastBackOps().Any()) { var castTypes = value.GetType().GetImplicitTypeCastBackOps(); foreach (var ct in castTypes) { try { value = ChoConvert.ConvertTo(value, ct); break; } catch { } } } } } } JToken t = null; if (settings != null) { if (conv != null) { settings.Converters.Add(conv); } } if (formatting == null) { formatting = serializer.Formatting; } if (settings != null && settings.Context.Context == null && enableXmlAttributePrefix) { settings.Context = new System.Runtime.Serialization.StreamingContext(System.Runtime.Serialization.StreamingContextStates.All, new ChoDynamicObject()); dynamic ctx = settings.Context.Context; ctx.EnableXmlAttributePrefix = enableXmlAttributePrefix; } if (conv != null) { t = JToken.Parse(JsonConvert.SerializeObject(value, formatting.Value, conv)); } else if (settings != null) { t = JToken.Parse(JsonConvert.SerializeObject(value, formatting.Value, settings)); } else { t = JToken.FromObject(value, serializer); } return(t); }