예제 #1
0
        /// <summary>
        /// 根据标签名获取标签处理的委托
        /// </summary>
        /// <param name="tickName">标签名</param>
        /// <returns></returns>
        public static DoDecode GetDecoder(string tickName)
        {
            DoDecode dec = null;

            if (dicDecoder.TryGetValue(tickName, out dec))
            {
                return(dec);
            }
            return(null);
        }
예제 #2
0
        public IEnumerable<DecodingInstruction> ExecuteDecode(DecodingContext context)
        {
            context.Input -= JsonToken.BeginArray;

            IList list;
            if (_listType == null || _listType.IsInterface || _listType.IsArray)
            {
                if (_genericArgument == null)
                {
                    list = new List<object>();
                }
                else
                {
                    list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(_genericArgument));
                }
            }
            else
            {
                // Create the desired instance directly
                list = (IList)Activator.CreateInstance(_listType);
            }

            for (int index = 0; context.Input.Peek() != JsonToken.EndArray; index++)
            {
                if (index > 0)
                {
                    context.Input -= JsonToken.ValueSeperator;
                }

                DoDecode instruction = new DoDecode(context.Input, _genericArgument);
                yield return instruction;

                list.Add(instruction.Value);
            }

            context.Input -= JsonToken.EndArray;

            if (_listType == null || !_listType.IsArray)
            {
                context.Value = list;
            }
            else
            {
                Array array = Array.CreateInstance(_listType.GetElementType(), list.Count);
                list.CopyTo(array, 0);

                context.Value = array;
            }
        }
예제 #3
0
        public IEnumerable<DecodingInstruction> ExecuteDecode(DecodingContext context)
        {
            if (_mapping.UsesReferencing && context.Input.Peek().Type == JsonTokenType.Number)
            {
                double reference = context.Input.Pop().Value<double>();
                context.Value = context.Process.References.FollowReference(reference);

                yield break;
            }

            context.Input -= JsonToken.BeginObject;

            object reflected = Activator.CreateInstance(context.KnownType);

            if (_mapping.UsesReferencing)
            {
                context.Process.References.Reference(reflected);
            }

            for (int index = 0; context.Input.Peek() != JsonToken.EndObject; index++)
            {
                if (index > 0)
                {
                    context.Input -= JsonToken.ValueSeperator;
                }

                string key = context.Input.Pop().Value<string>();

                if (key!=null && _mapping.FieldMappings.ContainsKey(key))
                {
                    JsonFieldMappingBase fieldMapping = _mapping.FieldMappings[key];

                    context.Input -= JsonToken.NameSeperator;

                    DoDecode instruction = new DoDecode(context.Input, fieldMapping.DesiredType);
                    yield return instruction;
                    object value = fieldMapping.Decode(instruction.Value);
                    fieldMapping.Set(reflected, value);
                }
                else
                {
                    //yield return null;
                }
            }

            context.Input -= JsonToken.EndObject;
            context.Value = reflected;
        }
예제 #4
0
        public IEnumerable<DecodingInstruction> ExecuteDecode(DecodingContext context)
        {
            context.Input -= JsonToken.BeginObject;

            IDictionary dict;

            #if NET40
            IDictionary<string, object> expando = null;
            #endif

            if (_dictType == null)
            {
                #if NET40
                dict = null;
                expando = new ExpandoObject();
                #else
                dict = (IDictionary)Activator.CreateInstance(typeof(Dictionary<,>).MakeGenericType(typeof(string), _genericArgument));
                #endif
            }
            else if(_dictType.IsInterface)
            {
                if (_genericArgument != null)
                    dict = (IDictionary)Activator.CreateInstance(typeof(Dictionary<,>).MakeGenericType(typeof(string), _genericArgument));
                else
                    dict = (IDictionary)Activator.CreateInstance(typeof(Dictionary<string, object>));

            }
            else
            {
                // Create the desired instance directly
                dict = (IDictionary)Activator.CreateInstance(_dictType);
            }

            for (int index = 0; context.Input.Peek() != JsonToken.EndObject; index++)
            {
                if (index > 0)
                {
                    context.Input -= JsonToken.ValueSeperator;
                }

                string key = context.Input.Pop().Value<string>();

                context.Input -= JsonToken.NameSeperator;

                DoDecode instruction = new DoDecode(context.Input, _genericArgument);
                yield return instruction;

                #if NET40
                if (dict == null)
                {
                    expando.Add(key, instruction.Value);
                }
                else
                {
                    dict.Add(key, instruction.Value);
                }
                #else
                dict.Add(key, instruction.Value);
                #endif
            }

            context.Input -= JsonToken.EndObject;

            #if NET40
            if (dict == null)
            {
                context.Value = expando;
            }
            else
            {
                context.Value = dict;
            }
            #else
            context.Value = dict;
            #endif
        }
예제 #5
0
 /// <summary>
 /// 添加UBB标签解释器
 /// </summary>
 /// <param name="tickName">标签名</param>
 /// <param name="info">标签信息</param>
 internal static void AddDecoder(string tickName, DoDecode info)
 {
     dicDecoder.Add(tickName, info);
 }
예제 #6
0
        /// <summary>
        /// 解释UBB字符串
        /// </summary>
        /// <returns></returns>
        public string ReadHtml()
        {
            StringBuilder tmpNumber = new StringBuilder();

            while (enums.MoveNext())
            {
                char chr = enums.Current;
                if (chr == '<') //如果是左括号就开始读取标签内容
                {
                    stkChr.Push('>');

                    string attStr = ReadHtml();//向下读取
                    if (stkTicks.Count > 0 && stkTicksEnd.Count > 0 && stkTicks.Peek() == stkTicksEnd.Peek())
                    {
                        tmpNumber.Append("<" + attStr);

                        stkChr.Pop();
                        break;
                    }
                    else if (stkChr.Count > 0 && stkChrEnd.Count > 0)
                    {
                        stkChr.Pop();
                        stkChrEnd.Pop();
                        HtmlTickInfo info     = new HtmlTickInfo(attStr);
                        bool         hasInner = false;

                        if (dicTick.TryGetValue(info.TickName, out hasInner))
                        {
                            if (!info.IsEnd)
                            {
                                if (hasInner) //如果有正文就继续读文本
                                {
                                    stkTicks.Push(info.TickName);
                                    string strInner = ReadHtml();
                                    if (stkTicks.Count > 0 && stkTicksEnd.Count > 0)
                                    {
                                        stkTicks.Pop();
                                        stkTicksEnd.Pop();
                                        info.InnerText = strInner;
                                        DoDecode decode = Decoder.GetDecoder(info.TickName);

                                        if (decode != null)
                                        {
                                            tmpNumber.Append(decode(info));
                                        }
                                    }
                                    else
                                    {
                                        string str = "<" + info.TickName;
                                        foreach (TickValue tvalue in info.TickAttributes)
                                        {
                                            str += " " + tvalue.AttributeName + "=\"" + tvalue.AttributeValue + "\"";
                                        }
                                        str += ">" + strInner;
                                        tmpNumber.Append(str);
                                    }
                                }
                                else
                                {
                                    DoDecode decode = Decoder.GetDecoder(info.TickName);
                                    tmpNumber.Append(decode(info));
                                }
                            }
                            else if (info.IsEnd && stkTicks.Count > 0 && stkTicks.Peek() == info.TickName)
                            {
                                stkTicksEnd.Push(info.TickName);//添加结束标记
                                break;
                            }
                            else
                            {
                                tmpNumber.Append("</" + attStr + ">");
                                //break;
                            }
                        }
                        else
                        {
                            //string str = "<" ;
                            //if (info.IsEnd)
                            //{
                            //    str += "/";
                            //}
                            //str += info.TickName;
                            //foreach (TickValue tvalue in info.TickAttributes)
                            //{
                            //    str += " " + tvalue.AttributeName + "=\"" + tvalue.AttributeValue + "\"";
                            //}
                            //str += ">";
                            tmpNumber.Append("<" + attStr + ">");
                        }
                    }
                    else
                    {
                        tmpNumber.Append("<" + attStr);

                        stkChr.Pop();
                    }
                }
                else if (chr == '>' && stkChr.Count > 0)//标签结束,跳出循环
                {
                    stkChrEnd.Push('>');
                    break;
                }
                else
                {
                    tmpNumber.Append(chr);
                }
            }
            return(tmpNumber.ToString());
        }
예제 #7
0
 /// <summary>
 /// 添加UBB标签解释器
 /// </summary>
 /// <param name="tickName">标签名</param>
 /// <param name="info">标签信息</param>
 /// <param name="hasInnerText)">是否含有文本</param>
 public static void AddDecoder(string tickName, DoDecode info, bool hasInnerText)
 {
     Decoder.AddDecoder(tickName, info);
     dicTick.Add(tickName, hasInnerText);
 }