Beispiel #1
0
        private void RenderJson(JsonTextWriter json, IReadOnlyElement elem, bool root)
        {
            if (!elem.Attributes().Any() && !elem.Elements().Any())
            {
                json.WriteValue(elem.Value);
            }
            else
            {
                json.WriteStartObject();
                if (root && elem.Attribute("type").Exists)
                {
                    json.WriteProperty("@odata.type", "http://host/odata/$metadata#" + elem.Attribute("type").Value);
                }
                foreach (var attr in elem.Attributes())
                {
                    if (!(root && (attr.Name == "type" || attr.Name == "action")))
                    {
                        json.WriteProperty("@" + attr.Name, attr.Value);
                    }
                }

                if (elem.Elements().Any())
                {
                    foreach (var group in elem.Elements().GroupBy(e => e.Name))
                    {
                        json.WritePropertyName(group.Key);
                        var needsArray = group.Skip(1).Any();
                        if (needsArray)
                        {
                            json.WriteStartArray();
                        }
                        foreach (var child in group)
                        {
                            RenderJson(json, child, false);
                        }
                        if (needsArray)
                        {
                            json.WriteEndArray();
                        }
                    }
                }
                else if (!string.IsNullOrEmpty(elem.Value))
                {
                    json.WriteProperty("#text", elem.Value);
                }


                json.WriteEndObject();
            }
        }
Beispiel #2
0
 /// <summary>
 /// Copies data from <paramref name="elem"/> into this instance.
 /// </summary>
 /// <param name="elem">The elem to copy from.</param>
 protected void CopyData(IReadOnlyElement elem)
 {
     Add(elem.Attributes());
     Add(elem.Elements());
     if (elem.Value != null)
     {
         Add(elem.Value);
     }
 }
Beispiel #3
0
        /// <summary>
        /// Copies data from <paramref name="elem"/> into this instance.
        /// </summary>
        /// <param name="elem">The elem to copy from.</param>
        protected void CopyData(IReadOnlyElement elem)
        {
            Add(elem.Attributes());
            var elements = elem.Elements();

            if (elements.Any())
            {
                Add(elements);
            }
            else if (elem.Value != null)
            {
                Add(elem.Value); // Only set the value if there are no elements as it will overwrite the elements
            }
        }