/// <summary>
 /// 序列化指定的对象至JSON字符串
 /// </summary>
 /// <param name="target">指定的对象</param>
 /// <returns>JSON字符串</returns>
 public static string SerializeObject(object target)
 {
     return(DynamicJsonHelper.CreateJsonString(
                new XStreamingElement("root",
                                      DynamicJsonHelper.CreateTypeAttr(DynamicJsonHelper.GetDynamicJsonType(target)),
                                      DynamicJsonHelper.CreateJsonNode(target))));
 }
Example #2
0
 /// <summary>
 /// Returns a <see cref="System.String"/> that represents this instance.
 /// </summary>
 /// <returns>
 /// A <see cref="System.String"/> that represents this instance.
 /// </returns>
 public override string ToString()
 {
     // Serialize to JsonString
     // <foo type="null"></foo> is can't serialize. replace to <foo type="null" />
     foreach (var elem in xml.Descendants().Where(x => x.Attribute("type").Value == "null"))
     {
         elem.RemoveNodes();
     }
     return(DynamicJsonHelper.CreateJsonString(new XStreamingElement("root", DynamicJsonHelper.CreateTypeAttr(jsonType), xml.Elements())));
 }
Example #3
0
        private bool TrySet(int index, object value)
        {
            var type = DynamicJsonHelper.GetDynamicJsonType(value);
            var e    = xml.Elements().ElementAtOrDefault(index);

            if (e == null)
            {
                xml.Add(new XElement("item", DynamicJsonHelper.CreateTypeAttr(type), DynamicJsonHelper.CreateJsonNode(value)));
            }
            else
            {
                e.Attribute("type").Value = type.ToString();
                e.ReplaceNodes(DynamicJsonHelper.CreateJsonNode(value));
            }

            return(true);
        }
Example #4
0
        private bool TrySet(string name, object value)
        {
            var type    = DynamicJsonHelper.GetDynamicJsonType(value);
            var element = xml.Element(name);

            if (element == null)
            {
                xml.Add(new XElement(name, DynamicJsonHelper.CreateTypeAttr(type), DynamicJsonHelper.CreateJsonNode(value)));
            }
            else
            {
                element.Attribute("type").Value = type.ToString();
                element.ReplaceNodes(DynamicJsonHelper.CreateJsonNode(value));
            }

            return(true);
        }
Example #5
0
 /// <summary>
 /// Dynamic JSON
 /// </summary>
 public DynamicJson()
 {
     xml      = new XElement("root", DynamicJsonHelper.CreateTypeAttr(DynamicJsonType.@object));
     jsonType = DynamicJsonType.@object;
 }