private static XElement WriteAtomObject(object obj, INamingScheme namingScheme)
        {
            var resources = ContentHelper.AsDictionaries(obj);

            if (resources != null)
            {
                return(WriteFeed(resources, namingScheme));
            }

            var resource = ContentHelper.AsDictionary(obj);

            if (resource != null)
            {
                return(WriteEntry(resource, namingScheme));
            }

            return(null);
        }
        private static SDataResource ReadResource(IDictionary <string, object> obj)
        {
            var resource = new SDataResource
            {
                Id          = ReadProtocolValue <string>(obj, "id"),
                Title       = ReadProtocolValue <string>(obj, "title"),
                Updated     = ReadProtocolValue <DateTimeOffset?>(obj, "updated"),
                HttpMethod  = ReadProtocolValue <HttpMethod?>(obj, "httpMethod", true),
                HttpStatus  = ReadProtocolValue <HttpStatusCode?>(obj, "httpStatus"),
                HttpMessage = ReadProtocolValue <string>(obj, "httpMessage"),
                Location    = ReadProtocolValue <string>(obj, "location"),
                ETag        = ReadProtocolValue <string>(obj, "etag"),
                IfMatch     = ReadProtocolValue <string>(obj, "ifMatch"),
                Url         = ReadProtocolValue <Uri>(obj, "url"),
                Key         = ReadProtocolValue <string>(obj, "key"),
                Uuid        = ReadProtocolValue <Guid?>(obj, "uuid"),
                Lookup      = ReadProtocolValue <string>(obj, "lookup"),
                Descriptor  = ReadProtocolValue <string>(obj, "descriptor"),
                Links       = ReadLinks(obj),
                IsDeleted   = ReadProtocolValue <bool?>(obj, "isDeleted")
            };

            object value;

            if (obj.TryGetValue("$diagnoses", out value))
            {
                resource.Diagnoses = ContentHelper.Deserialize <Diagnoses>(value);
            }
            if (obj.TryGetValue("$syncState", out value))
            {
                resource.SyncState = ContentHelper.Deserialize <SyncState>(value);
            }
            if (obj.TryGetValue("$permissions", out value))
            {
                resource.Permissions = ContentHelper.Deserialize <IList <SDataPermission> >(value, NamingScheme.CamelCase);
            }

            foreach (var item in obj.Where(item => !item.Key.StartsWith("$", StringComparison.Ordinal)))
            {
                resource[item.Key] = ReadObject(item.Value);
            }

            return(resource);
        }
        private static object WriteItem(object obj, bool isRoot, INamingScheme namingScheme, bool postMode)
        {
            var jsonObj = WriteObject(obj, isRoot, namingScheme, postMode);

            if (jsonObj != null)
            {
                return(jsonObj);
            }

            if (ContentHelper.IsObject(obj))
            {
                jsonObj = WriteObject(ContentHelper.Serialize(obj, namingScheme), isRoot, namingScheme, postMode);
                if (jsonObj != null)
                {
                    return(jsonObj);
                }
            }

            return(obj);
        }
        private static object WriteItem(XName name, object value, INamingScheme namingScheme, bool postMode)
        {
            var obj = WriteObject(name, value, namingScheme, postMode);

            if (obj != null)
            {
                return(obj);
            }

            if (ContentHelper.IsObject(value))
            {
                obj = WriteObject(name, ContentHelper.Serialize(value, namingScheme), namingScheme, postMode);
                if (obj != null)
                {
                    return(obj);
                }
            }

            return(new XElement(name, value));
        }
        private static object WriteObject(XName name, object value, INamingScheme namingScheme, bool postMode)
        {
            if (value == null)
            {
                return(new XElement(name, new XAttribute(_xsiNs + "nil", true)));
            }

            if (Equals(value, string.Empty))
            {
                return(new XElement(name));
            }

            var resource = ContentHelper.AsDictionary(value);

            if (resource != null)
            {
                var prot     = resource as ISDataProtocolObject;
                var info     = prot != null ? prot.Info : null;
                var itemName = info != null && info.XmlNamespace != null
                    ? XName.Get(name.LocalName, prot.Info.XmlNamespace)
                    : name;

                return(WriteResource(itemName, resource, namingScheme, postMode));
            }

            var resources = ContentHelper.AsDictionaries(value);

            if (resources != null)
            {
                return(WriteResourceCollection(name, resources, namingScheme, postMode));
            }

            var items = ContentHelper.AsCollection(value);

            if (items != null)
            {
                return(WriteSimpleCollection(name, items, namingScheme, postMode));
            }

            return(null);
        }
        private static IDictionary <string, object> WriteResourceCollection(IEnumerable <IDictionary <string, object> > collection, INamingScheme namingScheme, bool postMode)
        {
            var obj = new Dictionary <string, object>();

            var prot = collection as ISDataProtocolObject;
            var info = prot != null ? prot.Info : null;

            if (info != null)
            {
                WriteProtocolValue(obj, "id", info.Id);
                WriteProtocolValue(obj, "title", info.Title);
                WriteProtocolValue(obj, "updated", info.Updated);
                WriteProtocolValue(obj, "totalResults", info.TotalResults);
                WriteProtocolValue(obj, "startIndex", info.StartIndex);
                WriteProtocolValue(obj, "itemsPerPage", info.ItemsPerPage);
                WriteProtocolValue(obj, "url", info.Url != null ? info.Url.AbsoluteUri : null);
                WriteProtocolValue(obj, "deleteMissing", info.DeleteMissing);
                WriteLinks(obj, info.Links);
                WriteProtocolValue(obj, "syncMode", info.SyncMode);

                var diagnoses = info.Diagnoses;
                if (diagnoses != null && diagnoses.Count > 0)
                {
                    obj["$diagnoses"] = ContentHelper.Serialize(diagnoses);
                }

                var syncDigest = info.SyncDigest;
                if (syncDigest != null)
                {
                    obj["$digest"] = ContentHelper.Serialize(syncDigest);
                }
            }

            obj["$resources"] = collection.Select(item => (object)WriteResource(item, namingScheme, postMode)).ToList();
            return(obj);
        }
        private static object WriteObject(object obj, bool isRoot, INamingScheme namingScheme, bool postMode)
        {
            var resource = ContentHelper.AsDictionary(obj);

            if (resource != null)
            {
                return(WriteResource(resource, namingScheme, postMode));
            }

            var resources = ContentHelper.AsDictionaries(obj);

            if (resources != null)
            {
                var prot = resources as ISDataProtocolObject;
                if (prot != null && prot.Info != null && prot.Info.JsonIsSimpleArray)
                {
#if NET_2_0 || NET_3_5
                    return(WriteSimpleCollection(resources.Cast <object>(), namingScheme, postMode));
#else
                    return(WriteSimpleCollection(resources, namingScheme, postMode));
#endif
                }
                return(WriteResourceCollection(resources, namingScheme, postMode));
            }

            if (!isRoot)
            {
                var items = ContentHelper.AsCollection(obj);
                if (items != null)
                {
                    return(WriteSimpleCollection(items, namingScheme, postMode));
                }
            }

            return(null);
        }