Esempio n. 1
0
        private static JsonObject ReadCollectionProperty(XElement property, string typeName)
        {
            var collectionType = GetCollectionType(typeName);

            var json    = new JsonObject();
            var results = new List <object>();

            foreach (XElement item in property.Elements())
            {
                object resultItem = ReadDataItem(item);
                results.Add(resultItem);

                JsonObject complexValue = resultItem as JsonObject;
                if (complexValue != null)
                {
                    var metadata = complexValue["__metadata"] as JsonObject;
                    if (!string.IsNullOrEmpty(collectionType) && metadata["type"] == null)
                    {
                        metadata["type"] = collectionType;
                    }
                }
            }

            json["results"]    = results;
            json["__metadata"] = ReaderUtils.CreateEntryPropertyMetadata(typeName, false);

            return(json);
        }
Esempio n. 2
0
        private static JsonObject ReadComplexProperty(XElement container, string typeName)
        {
            JsonObject json = ReadObject(container);

            json["__metadata"] = ReaderUtils.CreateEntryPropertyMetadata(GetTypeAttribute(container), false);
            return(json);
        }
Esempio n. 3
0
        private static JsonObject ReadPropertyMetadata(XElement property)
        {
            var metadata = ReaderUtils.CreateEntryPropertyMetadata(GetTypeAttribute(property));

            if (IsCollectionProperty(property))
            {
                string collectionType = GetCollectionType(GetTypeAttribute(property));
                if (collectionType == null)
                {
                    metadata["type"] = "Collection()";
                }

                List <JsonObject> elements = new List <JsonObject>();
                foreach (XElement item in property.Elements(XName.Get("element", odataXmlNs)))
                {
                    string itemType =
                        HasTypeAttribute(item) ? GetTypeAttribute(item) :
                        IsCollectionProperty(item) ? "Collection()" : collectionType;

                    var itemMetadata = ReaderUtils.CreateEntryPropertyMetadata(itemType);
                    if (item.Elements().Any(e => e.Name.NamespaceName == odataXmlNs))
                    {
                        itemMetadata["properties"] = ReadPropertiesMetadata(item);
                    }
                    elements.Add(itemMetadata);
                }
                metadata["elements"] = elements.ToArray();
            }
            else if (property != null && property.Elements().Any(e => e.Name.NamespaceName == odataXmlNs))
            {
                metadata["properties"] = ReadPropertiesMetadata(property);
            }

            return(metadata);
        }
Esempio n. 4
0
        private static JsonObject ReadJsonSpatialProperty(XElement container, XElement gmlValue, bool isGeography)
        {
            GmlFormatter           gmlFormatter  = GmlFormatter.Create();
            GeoJsonObjectFormatter jsonformatter = GeoJsonObjectFormatter.Create();

            bool ignoreCrc = !gmlValue.Attributes().Any(a => a.Name.LocalName == "srsName");

            ISpatial spatialValue;

            if (isGeography)
            {
                spatialValue = gmlFormatter.Read <Geography>(gmlValue.CreateReader());
            }
            else
            {
                spatialValue = gmlFormatter.Read <Geometry>(gmlValue.CreateReader());
            }

            IDictionary <string, object> geoJsonData = jsonformatter.Write(spatialValue);
            JsonObject json = new JsonObject();

            Queue <object> geoJsonScopes = new Queue <object>();
            Queue <object> jsonScopes    = new Queue <object>();

            geoJsonScopes.Enqueue(geoJsonData);
            jsonScopes.Enqueue(json);

            Func <object, object> convertScope = (scope) =>
            {
                object newScope =
                    scope is List <object> || scope is object[] ? (object)new List <Object>() :
                    scope is IDictionary <string, object>?(object)new JsonObject() :
                        null;

                if (newScope != null)
                {
                    geoJsonScopes.Enqueue(scope);
                    jsonScopes.Enqueue(newScope);
                }

                return(newScope ?? scope);
            };

            while (jsonScopes.Count > 0)
            {
                if (jsonScopes.Peek() is JsonObject)
                {
                    var currentGeoJson = (IDictionary <string, object>)geoJsonScopes.Dequeue();
                    var currentJson    = (JsonObject)jsonScopes.Dequeue();

                    foreach (var item in currentGeoJson)
                    {
                        if (!ignoreCrc || item.Key != "crs")
                        {
                            currentJson[item.Key] = convertScope(item.Value);
                        }
                    }
                }
                else
                {
                    var currentGeoJson = (IEnumerable <object>)geoJsonScopes.Dequeue();
                    var currentJson    = (List <object>)jsonScopes.Dequeue();

                    foreach (var item in currentGeoJson)
                    {
                        currentJson.Add(convertScope(item));
                    }
                }
            }
            json["__metadata"] = ReaderUtils.CreateEntryPropertyMetadata(GetTypeAttribute(container), false);
            return(json);
        }