Ejemplo n.º 1
0
        public static ODataQuerier <T> Create(string name, XElement schema = null, string dataConverter = null, string dateFormatter = null, string format = null)
        {
            Database database = new DatabaseManufacturer().Create(name);

            XElement xSchema = schema ?? new PrimarySchemaProvider().GetSchema(name);

            string sConverter;

            if (string.IsNullOrWhiteSpace(dataConverter))
            {
                if (typeof(T) == typeof(XElement))
                {
                    sConverter = "xml";
                }
                else if (typeof(T) == typeof(string))
                {
                    sConverter = "json";
                }
                else
                {
                    throw new NotSupportedException(typeof(T).ToString());
                }
            }
            else
            {
                sConverter = dataConverter;
            }
            DataConverter <T> oDataConverter = new DataConverterManufacturer().Create <T>(sConverter);

            DateFormatter formatter;

            if (string.IsNullOrWhiteSpace(dateFormatter))
            {
                if (oDataConverter is DataConverter <XElement> )
                {
                    formatter = new DotNETDateFormatter();
                }
                else if (oDataConverter is DataConverter <string> )
                {
                    formatter = new JsonNETFormatter()
                    {
                        TimezoneOffset = schema.GetTimezoneOffset()
                    };
                }
                else
                {
                    throw new ArgumentNullException("dateFormatter");
                }
            }
            else
            {
                formatter = new DateFormatterManufacturer().Create(dateFormatter, schema.GetTimezoneOffset(), format);
            }
            oDataConverter.DateFormatter = formatter;

            return(new ODataQuerier <T>(database, xSchema, oDataConverter));
        }
Ejemplo n.º 2
0
        public static string CreateReturnKeysToJson(this IEnumerable <Dictionary <string, object> > keys)
        {
            List <string> jsons = new List <string>();

            foreach (Dictionary <string, object> key in keys)
            {
                List <string> keyValues = new List <string>();
                foreach (KeyValuePair <string, object> pair in key)
                {
                    Type   type = pair.Value.GetType();
                    string value;
                    if (TypeHelper.IsNumeric(type))
                    {
                        value = pair.Value.ToString();
                    }
                    else if (type == typeof(bool))
                    {
                        value = ((bool)pair.Value) ? "true" : "false";
                    }
                    else if (type == typeof(DateTime))
                    {
                        value = new JsonNETFormatter().Format((DateTime)pair.Value);
                    }
                    else
                    {
                        value = "\"" + pair.Value.ToString() + "\"";
                    }
                    keyValues.Add("\"" + pair.Key + "\":" + value);
                }
                jsons.Add("{" + string.Join(",", keyValues) + "}");
            }

            if (jsons.Count == 1)
            {
                return(jsons.First());
            }

            return("[" + string.Join(",", jsons) + "]");
        }