Пример #1
0
 /// <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))));
 }
Пример #2
0
 /// <summary>
 /// Convert JSON string to DynamicJson
 /// </summary>
 /// <param name="json">JSON字符串</param>
 /// <returns>DynamicJson</returns>
 public static dynamic Parse(string json)
 {
     using (var reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.Unicode.GetBytes(json), XmlDictionaryReaderQuotas.Max))
     {
         return(DynamicJsonHelper.ConvertElementToValue(XElement.Load(reader)));
     }
 }
Пример #3
0
        private dynamic DeserializeValue(XElement element, Type elementType)
        {
            var value = DynamicJsonHelper.ConvertElementToValue(element);

            if (value is DynamicJson)
            {
                value = ((DynamicJson)value).Deserialize(elementType);
            }
            return(Convert.ChangeType(value, elementType));
        }
Пример #4
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())));
 }
Пример #5
0
        private bool TryGet(XElement element, out object result)
        {
            if (element == null)
            {
                result = null;
                return(false);
            }

            result = DynamicJsonHelper.ConvertElementToValue(element);
            return(true);
        }
Пример #6
0
 /// <summary>
 /// Provides implementation for type conversion operations. Classes derived from the <see cref="T:System.Dynamic.DynamicObject"/> class can override this method to specify dynamic behavior for operations that convert an object from one type to another.
 /// </summary>
 /// <param name="binder">Provides information about the conversion operation. The binder.Type property provides the type to which the object must be converted. For example, for the statement (String)sampleObject in C# (CType(sampleObject, Type) in Visual Basic), where sampleObject is an instance of the class derived from the <see cref="T:System.Dynamic.DynamicObject"/> class, binder.Type returns the <see cref="T:System.String"/> type. The binder.Explicit property provides information about the kind of conversion that occurs. It returns true for explicit conversion and false for implicit conversion.</param>
 /// <param name="result">The result of the type conversion operation.</param>
 /// <returns>
 /// true if the operation is successful; otherwise, false. If this method returns false, the run-time binder of the language determines the behavior. (In most cases, a language-specific run-time exception is thrown.)
 /// </returns>
 public override bool TryConvert(ConvertBinder binder, out object result)
 {
     // Deserialize or foreach(IEnumerable)
     if (binder.Type == typeof(IEnumerable) || binder.Type == typeof(object[]))
     {
         var ie = (IsArray)
             ? xml.Elements().Select(x => DynamicJsonHelper.ConvertElementToValue(x))
             : xml.Elements().Select(x => (dynamic) new KeyValuePair <string, object>(x.Name.LocalName, DynamicJsonHelper.ConvertElementToValue(x)));
         result = (binder.Type == typeof(object[])) ? ie.ToArray() : ie;
     }
     else
     {
         result = Deserialize(binder.Type);
     }
     return(true);
 }
Пример #7
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);
        }
Пример #8
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);
        }
Пример #9
0
 /// <summary>
 /// Dynamic JSON
 /// </summary>
 public DynamicJson()
 {
     xml      = new XElement("root", DynamicJsonHelper.CreateTypeAttr(DynamicJsonType.@object));
     jsonType = DynamicJsonType.@object;
 }