private static object ReadObject(object obj)
        {
            var jsonObj = ContentHelper.AsDictionary(obj);

            if (jsonObj != null)
            {
                object resourcesObj;
                if (jsonObj.TryGetValue("$resources", out resourcesObj) ||
                    ((jsonObj.ContainsKey("$url") || jsonObj.ContainsKey("$deleteMissing")) &&
                     !jsonObj.ContainsKey("$key") && !jsonObj.ContainsKey("$uuid") && !jsonObj.ContainsKey("$lookup") && !jsonObj.ContainsKey("$descriptor") && !jsonObj.ContainsKey("$isDeleted")))
                {
                    var jsonResources = ContentHelper.AsDictionaries(resourcesObj);
                    return(ReadResourceCollection(jsonObj, jsonResources ?? new List <IDictionary <string, object> >()));
                }

                return(ReadResource(jsonObj));
            }

            var jsonItems = ContentHelper.AsCollection(obj);

            if (jsonItems != null)
            {
                return(ReadSimpleCollection(jsonItems));
            }

            return(obj);
        }
        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 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);
        }