当前位置: 首页>>代码示例>>C#>>正文


C# JArray.AddIfNotNull方法代码示例

本文整理汇总了C#中Newtonsoft.Json.Linq.JArray.AddIfNotNull方法的典型用法代码示例。如果您正苦于以下问题:C# JArray.AddIfNotNull方法的具体用法?C# JArray.AddIfNotNull怎么用?C# JArray.AddIfNotNull使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Newtonsoft.Json.Linq.JArray的用法示例。


在下文中一共展示了JArray.AddIfNotNull方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InternalToJToken

        /// <summary>
        /// To the JToken.
        /// </summary>
        /// <param name="xml">The XML.</param>
        /// <returns>JToken.</returns>
        internal static JToken InternalToJToken(this XElement xml)
        {
            JToken result = null;
            JTokenType tokenType;

            if (Enum.TryParse<JTokenType>(xml.GetAttributeValue(attributeType), out tokenType))
            {
                if (xml != null)
                {
                    switch (tokenType)
                    {
                        case JTokenType.Array:
                            var array = new JArray();

                            foreach (var one in xml.Elements())
                            {
                                array.AddIfNotNull(InternalToJToken(one));
                            }

                            result = array;
                            break;
                        //Simple types
                        case JTokenType.Date:
                            result = JToken.FromObject(Convert.ToDateTime(xml.Value));
                            break;
                        case JTokenType.Integer:
                        case JTokenType.Float:
                            result = JToken.Parse(xml.Value);
                            break;
                        case JTokenType.Boolean:
                            result = JToken.FromObject(xml.Value.ToBoolean(false));
                            break;
                        //Complex types
                        case JTokenType.Object:
                            var jObject = new JObject();
                            foreach (var one in xml.Elements())
                            {
                                // Keep compatibility for those which not use nodeNamePrefix.
                                var name = one.Name.LocalName.StartsWith(nodeNamePrefix) ? one.Name.LocalName.Substring(nodeNamePrefix.Length) : one.Name.LocalName;
                                jObject.Add(name, InternalToJToken(one));
                            }
                            result = jObject;
                            break;
                        case JTokenType.Property:
                        case JTokenType.Raw:
                            break;
                        case JTokenType.TimeSpan:
                            result = JToken.FromObject(new TimeSpan(xml.Value.ToInt64()));
                            break;
                        case JTokenType.String:
                            result = JToken.FromObject(xml.Value);
                            break;
                        case JTokenType.Uri:
                            result = JToken.FromObject(new Uri(xml.Value));
                            break;
                        case JTokenType.Bytes:
                            result = JToken.FromObject(Encoding.UTF8.GetBytes(xml.Value));
                            break;
                        case JTokenType.Null:
                            result = NullJToken;
                            break;
                        case JTokenType.Undefined:
                            result = UndefinedJToken;
                            break;
                        //Following option should be ignored.
                        case JTokenType.Comment:
                        case JTokenType.Constructor:
                        case JTokenType.None:
                        default:
                            break;
                    }
                }

            }
            return result;
        }
开发者ID:rynnwang,项目名称:CommonSolution,代码行数:81,代码来源:JsonXmlSerializer.cs

示例2: InternalDexmlize

        /// <summary>
        /// Internals the dexmlize.
        /// </summary>
        /// <param name="xml">The XML.</param>
        /// <returns></returns>
        private static JToken InternalDexmlize(this XElement xml)
        {
            JToken result = null;
            JTokenType tokenType;

            if (Enum.TryParse<JTokenType>(xml.GetAttributeValue(attributeType), out tokenType))
            {
                if (xml != null)
                {
                    switch (tokenType)
                    {
                        case JTokenType.Array:
                            var array = new JArray();

                            foreach (var one in xml.Elements())
                            {
                                array.AddIfNotNull(InternalDexmlize(one));
                            }

                            result = array;
                            break;
                        //Simple types
                        case JTokenType.Date:
                            result = JToken.FromObject(Convert.ToDateTime(xml.Value));
                            break;
                        case JTokenType.Integer:
                        case JTokenType.Float:
                            result = JToken.Parse(xml.Value);
                            break;
                        case JTokenType.Boolean:
                            result = JToken.FromObject(xml.Value.ToBoolean(false));
                            break;
                        //Complex types
                        case JTokenType.Object:
                            var jObject = new JObject();
                            foreach (var one in xml.Elements())
                            {
                                if (one.Name.LocalName.Equals(nodeProperty, StringComparison.OrdinalIgnoreCase))
                                {
                                    var name = one.GetAttributeValue(attributeName);

                                    if (!string.IsNullOrWhiteSpace(name))
                                    {
                                        jObject.Add(name, InternalDexmlize(one));
                                    }
                                }
                            }
                            result = jObject;
                            break;
                        case JTokenType.Property:
                        case JTokenType.Raw:
                            break;
                        case JTokenType.TimeSpan:
                            result = JToken.FromObject(new TimeSpan(xml.Value.ToInt64()));
                            break;
                        case JTokenType.String:
                            result = JToken.FromObject(xml.Value);
                            break;
                        case JTokenType.Uri:
                            result = JToken.FromObject(new Uri(xml.Value));
                            break;
                        case JTokenType.Bytes:
                            result = JToken.FromObject(Encoding.UTF8.GetBytes(xml.Value));
                            break;
                        case JTokenType.Null:
                            result = NullJToken;
                            break;
                        case JTokenType.Undefined:
                            result = UndefinedJToken;
                            break;
                        //Following option should be ignored.
                        case JTokenType.Comment:
                        case JTokenType.Constructor:
                        case JTokenType.None:
                        default:
                            break;
                    }
                }

            }
            return result;
        }
开发者ID:rynnwang,项目名称:CommonSolution,代码行数:87,代码来源:JsonXmlizer.cs


注:本文中的Newtonsoft.Json.Linq.JArray.AddIfNotNull方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。