Пример #1
0
        public static string ToJSON(ContractValue contract, object value)
        {
            var sb = new StringBuilder();
            var sw = new StringWriter(sb);

            using (var writer = new Newtonsoft.Json.JsonTextWriter(sw))
            {
                writer.WriteStartObject();
                var datacontract = ServicesFactory.GetDataContract(contract.Type);

                if (datacontract != null)
                {
                    foreach (var p in datacontract.Properties)
                    {
                        var v = p.Property.GetValue(value, new object[0]);

                        writer.WritePropertyName(p.Name);
                        writer.WriteValue(ValueConverter.ToString(p, v));
                    }
                }
                else
                {
                    writer.WritePropertyName("result");
                    writer.WriteValue(ValueConverter.ToString(contract, value));
                }
                writer.WriteEndObject();
            }
            return(sb.ToString());
        }
Пример #2
0
        private static void GetValues(ContractValue contract, object value, NameValueCollection parms, string header)
        {
            var datacontract = ServicesFactory.GetDataContract(contract.Type);

            if (datacontract != null)
            {
                foreach (var p in datacontract.Properties)
                {
                    object vv = p.Property.GetValue(value, new object[0]);
                    GetValues(p, vv, parms, $"{header}_{p.Name}");
                }
                return;
            }
            parms[$"{header}"] = ToString(contract, value);
        }
Пример #3
0
        private static object FromValues(ContractValue contract, NameValueCollection parms, string header)
        {
            var datacontract = ServicesFactory.GetDataContract(contract.Type);

            if (datacontract != null)
            {
                var result = Activator.CreateInstance(datacontract.Type);

                foreach (var p in datacontract.Properties)
                {
                    p.Property.SetValue(result, FromValues(p, parms, $"{header}_{p.Name}"), new object[0]);
                }
                return(result);
            }
            return(FromString(contract, parms[$"{header}"]));
        }
Пример #4
0
        internal static object FromJson(ContractValue contract, JObject data)
        {
            var datacontract = ServicesFactory.GetDataContract(contract.Type);

            if (datacontract != null)
            {
                //   var obj = data[contract.Name].Value<object>();

                var result = Activator.CreateInstance(datacontract.Type);

                foreach (var p in datacontract.Properties)
                {
                    p.Property.SetValue(result, data[p.Name].ToObject(p.Type));
                }
                return(result);
            }
            else
            {
                var obj = data["result"].Value <JValue>();
                return(obj.ToObject(contract.GetTypeFromTask()));
            }
        }