/// <inheritdoc /> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var properties = value.GetType().GetRuntimeProperties().Where(p => p.CanRead && p.CanWrite); JObject main = new JObject(); foreach (PropertyInfo prop in properties) { string jsonPath = null; ChoJSONPathAttribute att1 = prop.GetCustomAttributes(true) .OfType <ChoJSONPathAttribute>() .FirstOrDefault(); if (att1 != null && !att1.JSONPath.IsNullOrWhiteSpace()) { jsonPath = att1.JSONPath; } else if (att1 == null) { JsonPropertyAttribute att = prop.GetCustomAttributes(true) .OfType <JsonPropertyAttribute>() .FirstOrDefault(); jsonPath = att != null ? att.PropertyName : prop.Name; } if (!Regex.IsMatch(jsonPath, @"^[a-zA-Z0-9_.-]+$")) { throw new InvalidOperationException("JProperties of JsonPathConverter can have only letters, numbers, underscores, hyphens and dots but name was '" + jsonPath + "'."); // Array operations not permitted } if (serializer.ContractResolver is DefaultContractResolver) { var resolver = (DefaultContractResolver)serializer.ContractResolver; jsonPath = resolver.GetResolvedPropertyName(jsonPath); } var nesting = jsonPath.Split('.'); JObject lastLevel = main; for (int i = 0; i < nesting.Length; i++) { if (i == nesting.Length - 1) { lastLevel[nesting[i]] = new JValue(prop.GetValue(value)); } else { if (lastLevel[nesting[i]] == null) { lastLevel[nesting[i]] = new JObject(); } lastLevel = (JObject)lastLevel[nesting[i]]; } } } serializer.Serialize(writer, main); }
/// <inheritdoc /> public override object ReadJson( JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JObject jo = JObject.Load(reader); object targetObj = Activator.CreateInstance(objectType); dynamic ctx = serializer.Context.Context; StringComparison comparision = ctx != null? ChoUtility.CastTo <StringComparison>(ctx.StringComparision, StringComparison.InvariantCultureIgnoreCase) : StringComparison.InvariantCultureIgnoreCase; foreach (PropertyInfo prop in objectType.GetProperties().Where(p => p.CanRead && p.CanWrite)) { string jsonPath = null; ChoJSONPathAttribute att1 = prop.GetCustomAttributes(true) .OfType <ChoJSONPathAttribute>() .FirstOrDefault(); if (att1 != null && !att1.JSONPath.IsNullOrWhiteSpace()) { jsonPath = att1.JSONPath; } else if (att1 == null) { JsonPropertyAttribute att = prop.GetCustomAttributes(true) .OfType <JsonPropertyAttribute>() .FirstOrDefault(); jsonPath = att != null ? att.PropertyName : null; // prop.Name; } if (serializer.ContractResolver is DefaultContractResolver) { var resolver = (DefaultContractResolver)serializer.ContractResolver; jsonPath = resolver.GetResolvedPropertyName(jsonPath); } JToken token = jsonPath.IsNullOrWhiteSpace() ? jo.GetProperty(prop.Name, comparision) : jo.SelectToken(jsonPath); if (token != null && token.Type != JTokenType.Null) { object value = token.ToObject(prop.PropertyType, serializer); prop.SetValue(targetObj, value, null); } } return(targetObj); }
private Type DiscoverRecordFields(Type recordType, string declaringMember, bool optIn = false, List <ChoJSONRecordFieldConfiguration> recordFieldConfigurations = null, bool isTop = false) { if (recordType == null) { return(recordType); } if (!recordType.IsDynamicType()) { Type pt = null; if (ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoJSONRecordFieldAttribute>().Any()).Any()) { foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType)) { pt = pd.PropertyType.GetUnderlyingType(); bool optIn1 = ChoTypeDescriptor.GetProperties(pt).Where(pd1 => pd1.Attributes.OfType <ChoJSONRecordFieldAttribute>().Any()).Any(); if (optIn1 && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt) && FlatToNestedObjectSupport) { DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn1, recordFieldConfigurations, false); } else if (pd.Attributes.OfType <ChoJSONRecordFieldAttribute>().Any()) { var obj = new ChoJSONRecordFieldConfiguration(pd.Name, pd.Attributes.OfType <ChoJSONRecordFieldAttribute>().First(), pd.Attributes.OfType <Attribute>().ToArray()); obj.FieldType = pt; obj.PropertyDescriptor = pd; obj.DeclaringMember = declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name); if (recordFieldConfigurations != null) { if (!recordFieldConfigurations.Any(c => c.Name == pd.Name)) { recordFieldConfigurations.Add(obj); } } } } } else { if (isTop) { if (typeof(IList).IsAssignableFrom(recordType) || (recordType.IsGenericType && recordType.GetGenericTypeDefinition() == typeof(IList <>))) { throw new ChoParserException("Record type not supported."); } else if (typeof(IDictionary <string, object>).IsAssignableFrom(recordType)) { recordType = typeof(ExpandoObject); return(recordType); } else if (typeof(IDictionary).IsAssignableFrom(recordType)) { recordType = typeof(ExpandoObject); return(recordType); } } if (recordType.IsSimple()) { var obj = new ChoJSONRecordFieldConfiguration("Value", "$.Value"); obj.FieldType = recordType; recordFieldConfigurations.Add(obj); return(recordType); } foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType)) { JsonIgnoreAttribute jiAttr = pd.Attributes.OfType <JsonIgnoreAttribute>().FirstOrDefault(); if (jiAttr != null) { continue; } pt = pd.PropertyType.GetUnderlyingType(); if (pt != typeof(object) && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt) && FlatToNestedObjectSupport) { DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn, recordFieldConfigurations, false); } else { var obj = new ChoJSONRecordFieldConfiguration(pd.Name, ChoTypeDescriptor.GetPropetyAttribute <ChoJSONRecordFieldAttribute>(pd), pd.Attributes.OfType <Attribute>().ToArray()); obj.FieldType = pt; obj.PropertyDescriptor = pd; obj.DeclaringMember = declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name); StringLengthAttribute slAttr = pd.Attributes.OfType <StringLengthAttribute>().FirstOrDefault(); if (slAttr != null && slAttr.MaximumLength > 0) { obj.Size = slAttr.MaximumLength; } ChoUseJSONSerializationAttribute sAttr = pd.Attributes.OfType <ChoUseJSONSerializationAttribute>().FirstOrDefault(); if (sAttr != null) { obj.UseJSONSerialization = sAttr.Flag; } ChoJSONPathAttribute jpAttr = pd.Attributes.OfType <ChoJSONPathAttribute>().FirstOrDefault(); if (jpAttr != null) { obj.JSONPath = jpAttr.JSONPath; } JsonPropertyAttribute jAttr = pd.Attributes.OfType <JsonPropertyAttribute>().FirstOrDefault(); if (jAttr != null && !jAttr.PropertyName.IsNullOrWhiteSpace()) { obj.FieldName = jAttr.PropertyName; obj.JSONPath = jAttr.PropertyName; obj.Order = jAttr.Order; } else { DisplayNameAttribute dnAttr = pd.Attributes.OfType <DisplayNameAttribute>().FirstOrDefault(); if (dnAttr != null && !dnAttr.DisplayName.IsNullOrWhiteSpace()) { obj.FieldName = dnAttr.DisplayName.Trim(); } else { DisplayAttribute dpAttr = pd.Attributes.OfType <DisplayAttribute>().FirstOrDefault(); if (dpAttr != null) { if (!dpAttr.ShortName.IsNullOrWhiteSpace()) { obj.FieldName = dpAttr.ShortName; } else if (!dpAttr.Name.IsNullOrWhiteSpace()) { obj.FieldName = dpAttr.Name; } obj.Order = dpAttr.Order; } else { ColumnAttribute clAttr = pd.Attributes.OfType <ColumnAttribute>().FirstOrDefault(); if (clAttr != null) { obj.Order = clAttr.Order; if (!clAttr.Name.IsNullOrWhiteSpace()) { obj.FieldName = clAttr.Name; } } } } } DisplayFormatAttribute dfAttr = pd.Attributes.OfType <DisplayFormatAttribute>().FirstOrDefault(); if (dfAttr != null && !dfAttr.DataFormatString.IsNullOrWhiteSpace()) { obj.FormatText = dfAttr.DataFormatString; } if (dfAttr != null && !dfAttr.NullDisplayText.IsNullOrWhiteSpace()) { obj.NullValue = dfAttr.NullDisplayText; } if (recordFieldConfigurations != null) { if (!recordFieldConfigurations.Any(c => c.Name == pd.Name)) { recordFieldConfigurations.Add(obj); } } } } } } return(recordType); }
private void DiscoverRecordFields(Type recordType, string declaringMember, bool optIn = false, List <ChoJSONRecordFieldConfiguration> recordFieldConfigurations = null) { if (!recordType.IsDynamicType()) { Type pt = null; if (ChoTypeDescriptor.GetProperties(recordType).Where(pd => pd.Attributes.OfType <ChoJSONRecordFieldAttribute>().Any()).Any()) { foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType)) { pt = pd.PropertyType.GetUnderlyingType(); bool optIn1 = ChoTypeDescriptor.GetProperties(pt).Where(pd1 => pd1.Attributes.OfType <ChoJSONRecordFieldAttribute>().Any()).Any(); if (optIn1 && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt) && FlatToNestedObjectSupport) { DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn1); } else if (pd.Attributes.OfType <ChoJSONRecordFieldAttribute>().Any()) { var obj = new ChoJSONRecordFieldConfiguration(pd.Name, pd.Attributes.OfType <ChoJSONRecordFieldAttribute>().First(), pd.Attributes.OfType <Attribute>().ToArray()); obj.FieldType = pt; obj.PropertyDescriptor = pd; obj.DeclaringMember = declaringMember == null ? null : "{0}.{1}".FormatString(declaringMember, pd.Name); if (recordFieldConfigurations != null) { if (!recordFieldConfigurations.Any(c => c.Name == pd.Name)) { recordFieldConfigurations.Add(obj); } } } } } else { foreach (PropertyDescriptor pd in ChoTypeDescriptor.GetProperties(recordType)) { JsonIgnoreAttribute jiAttr = pd.Attributes.OfType <JsonIgnoreAttribute>().FirstOrDefault(); if (jiAttr != null) { continue; } pt = pd.PropertyType.GetUnderlyingType(); if (pt != typeof(object) && !pt.IsSimple() && !typeof(IEnumerable).IsAssignableFrom(pt) && FlatToNestedObjectSupport) { DiscoverRecordFields(pt, declaringMember == null ? pd.Name : "{0}.{1}".FormatString(declaringMember, pd.Name), optIn); } else { var obj = new ChoJSONRecordFieldConfiguration(pd.Name, (string)null); obj.FieldType = pt; obj.PropertyDescriptor = pd; obj.DeclaringMember = declaringMember == null ? null : "{0}.{1}".FormatString(declaringMember, pd.Name); StringLengthAttribute slAttr = pd.Attributes.OfType <StringLengthAttribute>().FirstOrDefault(); if (slAttr != null && slAttr.MaximumLength > 0) { obj.Size = slAttr.MaximumLength; } ChoUseJSONSerializationAttribute sAttr = pd.Attributes.OfType <ChoUseJSONSerializationAttribute>().FirstOrDefault(); if (sAttr != null) { obj.UseJSONSerialization = true; } ChoJSONPathAttribute jpAttr = pd.Attributes.OfType <ChoJSONPathAttribute>().FirstOrDefault(); if (jpAttr != null) { obj.JSONPath = jpAttr.JSONPath; } JsonPropertyAttribute jAttr = pd.Attributes.OfType <JsonPropertyAttribute>().FirstOrDefault(); if (jAttr != null && !jAttr.PropertyName.IsNullOrWhiteSpace()) { obj.FieldName = jAttr.PropertyName; } else { DisplayNameAttribute dnAttr = pd.Attributes.OfType <DisplayNameAttribute>().FirstOrDefault(); if (dnAttr != null && !dnAttr.DisplayName.IsNullOrWhiteSpace()) { obj.FieldName = dnAttr.DisplayName.Trim(); } else { DisplayAttribute dpAttr = pd.Attributes.OfType <DisplayAttribute>().FirstOrDefault(); if (dpAttr != null) { if (!dpAttr.ShortName.IsNullOrWhiteSpace()) { obj.FieldName = dpAttr.ShortName; } else if (!dpAttr.Name.IsNullOrWhiteSpace()) { obj.FieldName = dpAttr.Name; } } } } DisplayFormatAttribute dfAttr = pd.Attributes.OfType <DisplayFormatAttribute>().FirstOrDefault(); if (dfAttr != null && !dfAttr.DataFormatString.IsNullOrWhiteSpace()) { obj.FormatText = dfAttr.DataFormatString; } if (dfAttr != null && !dfAttr.NullDisplayText.IsNullOrWhiteSpace()) { obj.NullValue = dfAttr.NullDisplayText; } if (recordFieldConfigurations != null) { if (!recordFieldConfigurations.Any(c => c.Name == pd.Name)) { recordFieldConfigurations.Add(obj); } } } } } } }
internal ChoJSONRecordFieldConfiguration(string name, ChoJSONRecordFieldAttribute attr = null, Attribute[] otherAttrs = null) : base(name, attr, otherAttrs) { //IsArray = false; FieldName = name; if (attr != null) { Order = attr.Order; JSONPath = attr.JSONPath; UseJSONSerialization = attr.UseJSONSerializationInternal; FieldName = attr.FieldName.IsNullOrWhiteSpace() ? Name.NTrim() : attr.FieldName.NTrim(); } if (otherAttrs != null) { var sa = otherAttrs.OfType <ChoSourceTypeAttribute>().FirstOrDefault(); if (sa != null) { SourceType = sa.Type; } StringLengthAttribute slAttr = otherAttrs.OfType <StringLengthAttribute>().FirstOrDefault(); if (slAttr != null && slAttr.MaximumLength > 0) { Size = slAttr.MaximumLength; } ChoUseJSONSerializationAttribute sAttr = otherAttrs.OfType <ChoUseJSONSerializationAttribute>().FirstOrDefault(); if (sAttr != null) { UseJSONSerialization = sAttr.Flag; } ChoJSONPathAttribute jpAttr = otherAttrs.OfType <ChoJSONPathAttribute>().FirstOrDefault(); if (jpAttr != null) { JSONPath = jpAttr.JSONPath; } JsonPropertyAttribute jAttr = otherAttrs.OfType <JsonPropertyAttribute>().FirstOrDefault(); if (jAttr != null && !jAttr.PropertyName.IsNullOrWhiteSpace()) { FieldName = jAttr.PropertyName; JSONPath = jAttr.PropertyName; Order = jAttr.Order; } else { DisplayNameAttribute dnAttr = otherAttrs.OfType <DisplayNameAttribute>().FirstOrDefault(); if (dnAttr != null && !dnAttr.DisplayName.IsNullOrWhiteSpace()) { FieldName = dnAttr.DisplayName.Trim(); } else { DisplayAttribute dpAttr = otherAttrs.OfType <DisplayAttribute>().FirstOrDefault(); if (dpAttr != null) { if (!dpAttr.ShortName.IsNullOrWhiteSpace()) { FieldName = dpAttr.ShortName; } else if (!dpAttr.Name.IsNullOrWhiteSpace()) { FieldName = dpAttr.Name; } Order = dpAttr.Order; } else { ColumnAttribute clAttr = otherAttrs.OfType <ColumnAttribute>().FirstOrDefault(); if (clAttr != null) { Order = clAttr.Order; if (!clAttr.Name.IsNullOrWhiteSpace()) { FieldName = clAttr.Name; } } } } } DisplayFormatAttribute dfAttr = otherAttrs.OfType <DisplayFormatAttribute>().FirstOrDefault(); if (dfAttr != null && !dfAttr.DataFormatString.IsNullOrWhiteSpace()) { FormatText = dfAttr.DataFormatString; } if (dfAttr != null && !dfAttr.NullDisplayText.IsNullOrWhiteSpace()) { NullValue = dfAttr.NullDisplayText; } } }