public Rule(string sourceColumn, string destinationColumn, string dataType, MappingRule mappingRule)
 {
     SourceColumn      = sourceColumn;
     DestinationColumn = destinationColumn;
     DataType          = dataType;
     ComplexType       = mappingRule;
 }
 public Rule(string sourceColumn, string destinationColumn, string dataType, MappingRule mappingRule, Transform transform)
 {
     SourceColumn      = sourceColumn;
     DestinationColumn = destinationColumn;
     DataType          = dataType;
     ComplexType       = mappingRule;
     TransformValue    = transform;
 }
        /// <summary>
        /// Creates an intance of destination type and sets the properties
        /// </summary>
        /// <param name="jsonObject"></param>
        /// <param name="mapping"></param>
        /// <returns></returns>
        private object Execute(JObject jsonObject, MappingRule mapping)
        {
            Type type   = Type.GetType(mapping.DestinationType, true);
            var  entity = Activator.CreateInstance(type);

            // Set the value for each item in destinationType
            foreach (var rule in mapping.TruthTable)
            {
                var propertyInfo = entity.GetType().GetProperty(rule.DestinationColumn);
                if (propertyInfo != null)
                {
                    if (rule.ComplexType == null)
                    {
                        string valueType;
                        var    value = GetValue(jsonObject, rule.SourceColumn, rule.TransformValue, out valueType);

                        if (value != null)
                        {
                            if (rule.DataType == null)
                            {
                                rule.DataType = valueType;
                            }
                            var finalValue = handler.GetHandler <TypeConverterHandler>()
                                             .Run(JObject.FromObject(rule), JObject.FromObject(new { value = value }));
                            propertyInfo.SetValue(entity, finalValue, null);
                        }
                        else
                        {
                            propertyInfo.SetValue(entity, value, null);
                        }
                    }
                    else
                    {
                        propertyInfo.SetValue(entity, Execute(jsonObject, rule.ComplexType), null);
                    }
                }
            }
            return(entity);
        }
        private dynamic TransformJArray(JObject jsonObject, MappingRule mapping, bool ignoreNullVaue)
        {
            var    tokens   = jsonObject.SelectTokens(mapping.Node);
            JArray array    = new JArray();
            bool   hasToken = false;

            foreach (var item in tokens)
            {
                hasToken = true;
                if (string.Equals(item.GetType().Name, "jarray", StringComparison.OrdinalIgnoreCase))
                {
                    JArray itemJArray = (JArray)item;
                    if (itemJArray.Any())
                    {
                        foreach (var a in itemJArray)
                        {
                            var o = (JToken)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(ExecuteToJson((JObject)a, mapping)), JsonConvertSettings);
                            array.Add(o);
                        }
                    }
                    else
                    {
                        itemJArray.Add(new JObject(new JProperty("temp", "")));
                        array.Add((JToken)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(ExecuteToJson((JObject)item.FirstOrDefault(), mapping))));
                    }
                }
                else
                {
                    array.Add((JToken)JsonConvert.DeserializeObject(JsonConvert.SerializeObject(ExecuteToJson((JObject)item, mapping)), JsonConvertSettings));
                }
            }
            if (!hasToken && mapping.IgnoreEmptyArray)
            {
                return(null);
            }
            return(array);
        }
        protected JObject ExecuteToJson(JObject jsonObject, MappingRule mapping)
        {
            var jsonString = new JsonTransform();

            foreach (var rule in mapping.TruthTable)
            {
                // handle transpose
                if (rule.TransformValue != null && rule.TransformValue.Type != null && string.Equals(rule.TransformValue.Type, "promoteArrayToProperty", StringComparison.OrdinalIgnoreCase))
                {
                    Dictionary <string, object> transposeResponse = handler.GetHandler <TransposeHandler>()
                                                                    .Run(JObject.FromObject(rule), jsonObject);
                    if (transposeResponse != null)
                    {
                        jsonString.Json.AddRange(transposeResponse);
                    }
                }
                else if (!string.IsNullOrEmpty(rule.DestinationColumn))
                {
                    if (rule.ComplexType == null)
                    {
                        // Handle Jvalue
                        string valueType;
                        string destinationValue = rule.DestinationColumn.StartsWith("$") ? jsonObject.SelectToken(rule.DestinationColumn).ToString() : rule.DestinationColumn;

                        var value = GetValue(jsonObject, rule.SourceColumn, rule.TransformValue, out valueType);
                        if (rule.DataType == null)
                        {
                            rule.DataType = valueType;
                        }
                        var finalValue = handler.GetHandler <TypeConverterHandler>()
                                         .Run(JObject.FromObject(rule), JObject.FromObject(new { value = value }));
                        if (finalValue != null || finalValue.Type != JTokenType.Null || (finalValue == null && !mapping.IgnoreNullValue) || (finalValue.Type == JTokenType.Null && !mapping.IgnoreNullValue))
                        {
                            jsonString.Json.Add(destinationValue, finalValue);
                        }
                    }
                    else if (rule.ComplexType.DataType != null && rule.ComplexType.DataType.ToUpperInvariant().Equals("JARRAY"))
                    {
                        var result = TransformJArray(jsonObject, rule.ComplexType, mapping.IgnoreNullValue);
                        if (result != null)
                        {
                            jsonString.Json.Add(rule.DestinationColumn, result);
                        }
                    }
                    else
                    {
                        // Recursive call to handle complex type
                        var result = ExecuteToJson(jsonObject, rule.ComplexType);
                        if (result != null)
                        {
                            if (!string.IsNullOrWhiteSpace(rule.DataType))
                            {
                                jsonString.Json.Add(rule.DestinationColumn, handler.GetHandler <TypeConverterHandler>()
                                                    .Run(JObject.FromObject(rule), JObject.FromObject(new { value = result })));
                            }

                            else
                            {
                                jsonString.Json.Add(rule.DestinationColumn, result);
                            }
                        }
                    }
                }
            }

            return(JObject.FromObject(jsonString));
        }