public static void WriteTo(Bundle bundle, JsonWriter writer, bool summary = false)
        {
            if (bundle == null)
            {
                throw new ArgumentException("Bundle cannot be null");
            }

            JObject result = new JObject();

            result.Add(new JProperty(JsonDomFhirReader.RESOURCETYPE_MEMBER_NAME, "Bundle"));

            if (!String.IsNullOrWhiteSpace(bundle.Title))
            {
                result.Add(new JProperty(BundleXmlParser.XATOM_TITLE, bundle.Title));
            }
            if (SerializationUtil.UriHasValue(bundle.Id))
            {
                result.Add(new JProperty(BundleXmlParser.XATOM_ID, bundle.Id));
            }
            if (bundle.LastUpdated != null)
            {
                result.Add(new JProperty(BundleXmlParser.XATOM_UPDATED, bundle.LastUpdated));
            }

            if (!String.IsNullOrWhiteSpace(bundle.AuthorName))
            {
                result.Add(jsonCreateAuthor(bundle.AuthorName, bundle.AuthorUri));
            }
            if (bundle.TotalResults != null)
            {
                result.Add(new JProperty(BundleXmlParser.XATOM_TOTALRESULTS, bundle.TotalResults.ToString()));
            }

            if (bundle.Links.Count > 0)
            {
                result.Add(new JProperty(BundleXmlParser.XATOM_LINK, jsonCreateLinkArray(bundle.Links)));
            }
            if (bundle.Tags != null && bundle.Tags.Count() > 0)
            {
                result.Add(TagListSerializer.CreateTagCategoryPropertyJson(bundle.Tags));
            }

            var entryArray = new JArray();

            foreach (var entry in bundle.Entries)
            {
                entryArray.Add(createEntry(entry, summary));
            }

            result.Add(new JProperty(BundleXmlParser.XATOM_ENTRY, entryArray));

            result.WriteTo(writer);
        }
        private static JObject createEntry(BundleEntry entry, bool summary)
        {
            JObject result = new JObject();

            if (entry is ResourceEntry)
            {
                ResourceEntry re = (ResourceEntry)entry;
                if (!String.IsNullOrEmpty(re.Title))
                {
                    result.Add(new JProperty(BundleXmlParser.XATOM_TITLE, re.Title));
                }
                if (SerializationUtil.UriHasValue(entry.Id))
                {
                    result.Add(new JProperty(BundleXmlParser.XATOM_ID, entry.Id.ToString()));
                }

                if (re.LastUpdated != null)
                {
                    result.Add(new JProperty(BundleXmlParser.XATOM_UPDATED, re.LastUpdated));
                }
                if (re.Published != null)
                {
                    result.Add(new JProperty(BundleXmlParser.XATOM_PUBLISHED, re.Published));
                }

                if (!String.IsNullOrWhiteSpace(re.AuthorName))
                {
                    result.Add(jsonCreateAuthor(re.AuthorName, re.AuthorUri));
                }
            }
            else
            {
                DeletedEntry de = (DeletedEntry)entry;
                if (de.When != null)
                {
                    result.Add(new JProperty(BundleJsonParser.JATOM_DELETED, de.When));
                }
                if (SerializationUtil.UriHasValue(entry.Id))
                {
                    result.Add(new JProperty(BundleXmlParser.XATOM_ID, entry.Id.ToString()));
                }
            }

            if (entry.Links != null && entry.Links.Count() > 0)
            {
                result.Add(new JProperty(BundleXmlParser.XATOM_LINK, jsonCreateLinkArray(entry.Links)));
            }

            if (entry.Tags != null && entry.Tags.Count() > 0)
            {
                result.Add(TagListSerializer.CreateTagCategoryPropertyJson(entry.Tags));
            }

            if (entry is ResourceEntry)
            {
                ResourceEntry re = (ResourceEntry)entry;
                if (re.Resource != null)
                {
                    result.Add(new JProperty(BundleXmlParser.XATOM_CONTENT,
                                             getContentsAsJObject(re.Resource, summary)));
                }

                // Note: this is a read-only property, so it is serialized but never parsed
                if (entry.Summary != null)
                {
                    result.Add(new JProperty(BundleXmlParser.XATOM_SUMMARY, entry.Summary));
                }
            }

            return(result);
        }