/// <summary> /// WriteJson /// </summary> /// <param name="writer">json writer</param> /// <param name="value">object value</param> /// <param name="serializer">json serilaizer value</param> /// <returns>void</returns> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value.GetType().BaseType == Type.GetType("System.Array")) { writer.WriteStartArray(); foreach (var arr in value as Array) { serializer.Serialize(writer, arr); } writer.WriteEndArray(); return; } if (value.GetType().GetProperties().Count() > 0) { writer.WriteStartObject(); foreach (PropertyInfo propertyInfo in value.GetType().GetProperties()) { if (propertyInfo.CanRead && ((propertyInfo.PropertyType.IsArray && propertyInfo.PropertyType.GetElementType() == Type.GetType("System.Object")) || (propertyInfo.PropertyType == Type.GetType("System.Object")))) { object val = propertyInfo.GetValue(value, null); if (!(val == null && (serializer.NullValueHandling == NullValueHandling.Ignore))) { string propName = propertyInfo.Name + "Specified"; PropertyInfo specifiedProp = value.GetType().GetProperty(propName); if (specifiedProp != null && ((bool)specifiedProp.GetValue(value, null) == true)) { writer.WritePropertyName(val.GetType().Name); serializer.Serialize(writer, propertyInfo.GetValue(value, null)); } else if (specifiedProp == null) { System.Xml.Serialization.XmlChoiceIdentifierAttribute[] attrs = (System.Xml.Serialization.XmlChoiceIdentifierAttribute[])propertyInfo.GetCustomAttributes((new System.Xml.Serialization.XmlChoiceIdentifierAttribute()).GetType(), false); if (attrs.Count() > 0) { System.Xml.Serialization.XmlChoiceIdentifierAttribute attr = attrs[0]; PropertyInfo choiceNameProp = value.GetType().GetProperty(attr.MemberName); if (choiceNameProp.PropertyType.IsArray) { //AnyIntuitObjects var TypeofValue = choiceNameProp.GetValue(value, null); Array choiceNameArr = (TypeofValue as Array); Array valueArr = (propertyInfo.GetValue(value, null) as Array); for (int i = 0; i < choiceNameArr.Length; i++) { writer.WritePropertyName(choiceNameArr.GetValue(i).ToString()); serializer.Serialize(writer, valueArr.GetValue(i)); } } else { //AnyIntuitObject var TypeofValue = choiceNameProp.GetValue(value, null); writer.WritePropertyName(TypeofValue.ToString()); serializer.Serialize(writer, propertyInfo.GetValue(value, null)); } } else { Type valueType = null; if (val.GetType().IsArray) { if ((val as Array).Length > 0) { valueType = (val as Array).GetValue(0).GetType(); } } else { valueType = val.GetType(); } System.Xml.Serialization.XmlElementAttribute[] elementAttrs = (System.Xml.Serialization.XmlElementAttribute[])propertyInfo.GetCustomAttributes((new System.Xml.Serialization.XmlElementAttribute()).GetType(), false); foreach (System.Xml.Serialization.XmlElementAttribute attr in elementAttrs) { if (valueType.Name == attr.Type.Name) { writer.WritePropertyName(attr.ElementName); serializer.Serialize(writer, propertyInfo.GetValue(value, null)); } } } } } } else { if ((propertyInfo.GetValue(value, null) != null) && (serializer.NullValueHandling == NullValueHandling.Ignore)) { if (propertyInfo.GetCustomAttributes(false).Contains(new Newtonsoft.Json.JsonIgnoreAttribute()) != true) { if (propertyInfo.GetCustomAttributes(false).Contains(new System.Xml.Serialization.XmlTextAttribute()) && propertyInfo.Name == "Value") { writer.WritePropertyName("value"); writer.WriteValue(propertyInfo.GetValue(value, null).ToString()); } else { string propName = propertyInfo.Name + "Specified"; PropertyInfo specifiedProp = value.GetType().GetProperty(propName); if (specifiedProp != null && (bool)specifiedProp.GetValue(value, null) == true) { writer.WritePropertyName(propertyInfo.Name); serializer.Serialize(writer, propertyInfo.GetValue(value, null)); } else if (specifiedProp == null) { //check if this has choiceIdentifier put key name as its value else put name of property System.Xml.Serialization.XmlChoiceIdentifierAttribute[] choiceattrs = (System.Xml.Serialization.XmlChoiceIdentifierAttribute[])propertyInfo.GetCustomAttributes((new System.Xml.Serialization.XmlChoiceIdentifierAttribute()).GetType(), false); if (choiceattrs.Count() > 0) { foreach ( System.Xml.Serialization.XmlChoiceIdentifierAttribute choiceAttr in choiceattrs) { PropertyInfo choiceProp = value.GetType().GetProperty(choiceAttr.MemberName); writer.WritePropertyName(choiceProp.GetValue(value, null).ToString()); } } else { //Adding additional hardcode for recurring txn json serializtion issue string[] entitiesSupportedforRecurTxn = { "Bill", "Invoice", "Purchase", "CreditMemo", "Deposit", "Estimate", "JournalEntry", "RefundReceipt", "SalesReceipt", "Transfer", "VendorCredit", "PurchaseOrder" }; if (propertyInfo.Name == "AnyIntuitObject" && value.GetType().Name == "RecurringTransaction") { string entityNametoCheckForRecurTxn = propertyInfo.GetValue(value, null).GetType().Name; if (entitiesSupportedforRecurTxn.Contains(entityNametoCheckForRecurTxn)) { writer.WritePropertyName(entityNametoCheckForRecurTxn); } else { writer.WritePropertyName(propertyInfo.Name); } } else { writer.WritePropertyName(propertyInfo.Name); } } //check if its XmlArrayItem (NameValue) System.Xml.Serialization.XmlArrayItemAttribute[] arrayItemAttrs = (System.Xml.Serialization.XmlArrayItemAttribute[])propertyInfo.GetCustomAttributes((new System.Xml.Serialization.XmlArrayItemAttribute()).GetType(), false); if (arrayItemAttrs.Count() > 0) { writer.WriteStartObject(); //if XmlArrayitem has name else data type will be the name if (!string.IsNullOrEmpty(arrayItemAttrs[0].ElementName)) { writer.WritePropertyName(arrayItemAttrs[0].ElementName); } else { writer.WritePropertyName(propertyInfo.GetValue(value, null).GetType().GetElementType().Name); } serializer.Serialize(writer, propertyInfo.GetValue(value, null)); writer.WriteEndObject(); } else { serializer.Serialize(writer, propertyInfo.GetValue(value, null)); } } } } } } } writer.WriteEndObject(); } else { writer.WriteValue(value); } }
/// <summary> /// WriteJson /// </summary> /// <param name="writer">json writer</param> /// <param name="value">object value</param> /// <param name="serializer">json serilaizer value</param> /// <returns>void</returns> public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { if (value.GetType().BaseType == Type.GetType("System.Array")) { writer.WriteStartArray(); foreach (var arr in value as Array) { serializer.Serialize(writer, arr); } writer.WriteEndArray(); return; } if (value.GetType().GetProperties().Count() > 0) { writer.WriteStartObject(); foreach (PropertyInfo propertyInfo in value.GetType().GetProperties()) { if (propertyInfo.CanRead && ((propertyInfo.PropertyType.IsArray && propertyInfo.PropertyType.GetElementType() == Type.GetType("System.Object")) || (propertyInfo.PropertyType == Type.GetType("System.Object")))) { object val = propertyInfo.GetValue(value, null); if (!(val == null && (serializer.NullValueHandling == NullValueHandling.Ignore))) { string propName = propertyInfo.Name + "Specified"; PropertyInfo specifiedProp = value.GetType().GetProperty(propName); if (specifiedProp != null && ((bool)specifiedProp.GetValue(value, null) == true)) { writer.WritePropertyName(val.GetType().Name); serializer.Serialize(writer, propertyInfo.GetValue(value, null)); } else if (specifiedProp == null) { System.Xml.Serialization.XmlChoiceIdentifierAttribute[] attrs = (System.Xml.Serialization.XmlChoiceIdentifierAttribute[])propertyInfo.GetCustomAttributes((new System.Xml.Serialization.XmlChoiceIdentifierAttribute()).GetType(), false); if (attrs.Count() > 0) { System.Xml.Serialization.XmlChoiceIdentifierAttribute attr = attrs[0]; PropertyInfo choiceNameProp = value.GetType().GetProperty(attr.MemberName); if (choiceNameProp.PropertyType.IsArray) { //AnyIntuitObjects var TypeofValue = choiceNameProp.GetValue(value, null); Array choiceNameArr = (TypeofValue as Array); Array valueArr = (propertyInfo.GetValue(value, null) as Array); for (int i = 0; i < choiceNameArr.Length; i++) { writer.WritePropertyName(choiceNameArr.GetValue(i).ToString()); serializer.Serialize(writer, valueArr.GetValue(i)); } } else { //AnyIntuitObject var TypeofValue = choiceNameProp.GetValue(value, null); writer.WritePropertyName(TypeofValue.ToString()); serializer.Serialize(writer, propertyInfo.GetValue(value, null)); } } else { Type valueType = null; if (val.GetType().IsArray) { if ((val as Array).Length > 0) { valueType = (val as Array).GetValue(0).GetType(); } } else { valueType = val.GetType(); } System.Xml.Serialization.XmlElementAttribute[] elementAttrs = (System.Xml.Serialization.XmlElementAttribute[])propertyInfo.GetCustomAttributes((new System.Xml.Serialization.XmlElementAttribute()).GetType(), false); foreach (System.Xml.Serialization.XmlElementAttribute attr in elementAttrs) { if (valueType.Name == attr.Type.Name) { writer.WritePropertyName(attr.ElementName); serializer.Serialize(writer, propertyInfo.GetValue(value, null)); } } } } } } else { if ((propertyInfo.GetValue(value, null) != null) && (serializer.NullValueHandling == NullValueHandling.Ignore)) { if (propertyInfo.GetCustomAttributes(false).Contains(new Newtonsoft.Json.JsonIgnoreAttribute()) != true) { if (propertyInfo.GetCustomAttributes(false).Contains(new System.Xml.Serialization.XmlTextAttribute()) && propertyInfo.Name == "Value") { writer.WritePropertyName("value"); writer.WriteValue(propertyInfo.GetValue(value, null).ToString()); } else { string propName = propertyInfo.Name + "Specified"; PropertyInfo specifiedProp = value.GetType().GetProperty(propName); if (specifiedProp != null && (bool)specifiedProp.GetValue(value, null) == true) { writer.WritePropertyName(propertyInfo.Name); serializer.Serialize(writer, propertyInfo.GetValue(value, null)); } else if (specifiedProp == null) { //check if this has choiceIdentifier put key name as its value else put name of property System.Xml.Serialization.XmlChoiceIdentifierAttribute[] choiceattrs = (System.Xml.Serialization.XmlChoiceIdentifierAttribute[])propertyInfo.GetCustomAttributes((new System.Xml.Serialization.XmlChoiceIdentifierAttribute()).GetType(), false); if (choiceattrs.Count() > 0) { foreach ( System.Xml.Serialization.XmlChoiceIdentifierAttribute choiceAttr in choiceattrs) { PropertyInfo choiceProp = value.GetType().GetProperty(choiceAttr.MemberName); writer.WritePropertyName(choiceProp.GetValue(value, null).ToString()); } } else { writer.WritePropertyName(propertyInfo.Name); } //check if its XmlArrayItem (NameValue) System.Xml.Serialization.XmlArrayItemAttribute[] arrayItemAttrs = (System.Xml.Serialization.XmlArrayItemAttribute[])propertyInfo.GetCustomAttributes((new System.Xml.Serialization.XmlArrayItemAttribute()).GetType(), false); if (arrayItemAttrs.Count() > 0) { writer.WriteStartObject(); //if XmlArrayitem has name else data type will be the name if (!string.IsNullOrEmpty(arrayItemAttrs[0].ElementName)) { writer.WritePropertyName(arrayItemAttrs[0].ElementName); } else { writer.WritePropertyName(propertyInfo.GetValue(value, null).GetType().GetElementType().Name); } serializer.Serialize(writer, propertyInfo.GetValue(value, null)); writer.WriteEndObject(); } else { serializer.Serialize(writer, propertyInfo.GetValue(value, null)); } } } } } } } writer.WriteEndObject(); } else { writer.WriteValue(value); } }