Пример #1
0
        private static Expression GenerateKeyValueObjectReadKeyWithIgnoreCase(CharTries charTries, ParameterExpression newModel)
        {
            return(Expression.Block(

                       /*
                        * _ReadQuotes();
                        */
                       Expression.Call(ExpressionMembers.Reader, JsonReader._ReadQuotes),

                       /*
                        * Switch(getChar())
                        *   case 'N'
                        *         Switch(getChar())
                        *               case 'a'
                        *                     Switch(getChar())
                        *                           case 'm'
                        *                                 Switch(getChar())
                        *                                       case 'e'
                        *                                             Switch(getChar())
                        *                                                   case '"':
                        *                                                       ReadValue()...
                        */
                       GenerateSwitchCodeByChar(null, charTries, newModel, true)
                       ));
        }
Пример #2
0
        internal void Insert(string str, MemberExtension mem)
        {
            CharTries charTries = this;

            foreach (var c in str)
            {
                var @case = charTries.Childrens.Find(e => e.Val == c);
                if (@case == null)
                {
                    @case = new CharTries()
                    {
                        Val = c, Parent = charTries
                    };
                    charTries.Childrens.Add(@case);
                }
                charTries = @case;
            }
            charTries.IsValue = true;
            charTries.Member  = mem;
        }
Пример #3
0
        private static SwitchExpression GenerateSwitchCodeByChar(ParameterExpression firstChar, CharTries charTries, ParameterExpression newModel, bool isToLower = false)
        {
            List <SwitchCase> switchCases = new List <SwitchCase>();

            if (charTries.IsValue)
            {
                //case '"'
                var caseQuotes = Expression.SwitchCase(

                    Expression.Block(
                        typeof(void),

                        /*
                         * ReadColon();
                         */
                        Expression.Call(ExpressionMembers.Reader, JsonReader._ReadColon),
                        GenerateKeyValueObjectReadValue(charTries.Member, newModel),

                        /*
                         * isArrive=true;
                         */
                        ExpressionMembers.IsArriveAssignTrue
                        )
                    , Expression.Constant('"', typeof(char)));

                switchCases.Add(caseQuotes);
            }

            if (charTries.Childrens.Count > 0)
            {
                foreach (var item in charTries.Childrens)
                {
                    char       c            = item.Val;
                    SwitchCase caseOrdinary = Expression.SwitchCase( //When there are two duplicate case items, the expression takes the first one automatically
                        GenerateSwitchCodeByChar(null, item, newModel, isToLower),
                        Expression.Constant(isToLower ? char.ToLower(c) : c, typeof(char)));

                    switchCases.Add(caseOrdinary);
                }
            }

            return(Expression.Switch(isToLower ? Expression.Call(typeof(char).GetMethod("ToLower", new[] { typeof(char) }), ExpressionMembers.GetChar) : firstChar ?? ExpressionMembers.GetChar, switchCases.ToArray()));
        }
Пример #4
0
        private static Expression GenerateKeyValueObjectReadKeyWithInitial(ParameterExpression firstChar, CharTries charTries, ParameterExpression newModel)
        {
            return(Expression.Block(

                       /*
                        * _ReadQuotes();
                        */
                       Expression.Call(ExpressionMembers.Reader, JsonReader._ReadQuotes),

                       /*
                        * switch (handler.Option.InitialReadState)
                        *  {
                        *      case InitialReadStateEnum.None:
                        *          firstChar = reader.GetChar();
                        *          break;
                        *      case InitialReadStateEnum.Upper:
                        *          firstChar = char.ToUpper(reader.GetChar());
                        *          break;
                        *      case InitialReadStateEnum.Lower:
                        *          firstChar = char.ToLower(reader.GetChar());
                        *          break;
                        *  }
                        */

                       Expression.Switch(typeof(void), ExpressionMembers.JsonCharacterReadState,
                                         null, null,
                                         Expression.SwitchCase(Expression.Assign(firstChar, ExpressionMembers.GetChar), Expression.Constant(JsonCharacterReadStateEnum.None, typeof(JsonCharacterReadStateEnum))),
                                         Expression.SwitchCase(Expression.Assign(firstChar, Expression.Call(typeof(char).GetMethod("ToUpper", new[] { typeof(char) }), ExpressionMembers.GetChar)), Expression.Constant(JsonCharacterReadStateEnum.InitialUpper, typeof(JsonCharacterReadStateEnum))),
                                         Expression.SwitchCase(Expression.Assign(firstChar, Expression.Call(typeof(char).GetMethod("ToLower", new[] { typeof(char) }), ExpressionMembers.GetChar)), Expression.Constant(JsonCharacterReadStateEnum.InitialLower, typeof(JsonCharacterReadStateEnum)))
                                         ),

                       /*
                        * Switch(firstChar)
                        *   case 'N'
                        *         Switch(getChar())
                        *               case 'a'
                        *                     Switch(getChar())
                        *                           case 'm'
                        *                                 Switch(getChar())
                        *                                       case 'e'
                        *                                             Switch(getChar())
                        *                                                   case '"':
                        *                                                       ReadValue()...
                        */
                       GenerateSwitchCodeByChar(firstChar, charTries, newModel)
                       ));
        }
Пример #5
0
 private static Expression GenerateKeyValueObjectReadKey(ParameterExpression firstChar, CharTries charTries, ParameterExpression newModel)
 {
     return
         (Expression.IfThenElse(
              Expression.NotEqual(ExpressionMembers.JsonCharacterReadState, Expression.Constant(JsonCharacterReadStateEnum.IgnoreCase, typeof(JsonCharacterReadStateEnum))),
              GenerateKeyValueObjectReadKeyWithInitial(firstChar, charTries, newModel),
              GenerateKeyValueObjectReadKeyWithIgnoreCase(charTries, newModel)));
 }
Пример #6
0
        private static BlockExpression GenerateWhile(ParameterExpression newModel, Type type, CharTries charTries)
        {
            //(state):0==The initial state does not change,1==Capital letters,2==Initial lowercase

            /*
             * char c;
             * while(moveNext-->0)
             * {
             * }
             */
            ParameterExpression firstChar      = Expression.Variable(typeof(char), "firstChar");
            LabelTarget         whileBreak     = Expression.Label();
            LoopExpression      loopExpression = Expression.Loop(Expression.IfThenElse(ExpressionMembers.MoveNextDecrement,
                                                                                       ReturnFunc <Expression>(() =>
            {
                Expression[] expressions = new Expression[4];

                /*
                 * isArrive=false;
                 */
                expressions[0] = ExpressionMembers.IsArriveAssignFalse;

                /*
                 * if(handler.option.GlobalKeyFormat!=null)
                 *     GenerateGlobalKeyFormat()
                 * else
                 *     ReadKey()
                 */
                expressions[1] = Expression.IfThenElse(
                    Expression.NotEqual(Expression.MakeMemberAccess(ExpressionMembers.JsonDeserializeOption, JsonDeserializeOption._GlobalKeyFormat), Expression.Constant(null, JsonDeserializeOption._GlobalKeyFormat.FieldType)),
                    GenerateGlobalKeyFormat(type, newModel),
                    GenerateKeyValueObjectReadKey(firstChar, charTries, newModel)
                    );

                /*
                 *  if(!isArrive)
                 *       throw
                 */
                expressions[2] = Expression.IfThen(Expression.Equal(ExpressionMembers.IsArrive, Expression.Constant(false, typeof(bool))), Expression.Throw(Expression.New(JsonDeserializationTypeResolutionException._JsonDeserializationTypeResolutionExceptionCtor, ExpressionMembers.Reader, type.IsInterface ? (Expression)Expression.Constant(type, typeof(Type)) : Expression.Call(newModel, type.GetMethod("GetType")))));

                /*
                 *  if(reader.ReadComma()==true)
                 *      moveNext++;
                 */
                expressions[3] = ExpressionMembers.IfReadBoolCommaIsTrueSoMoveNextIncrement;

                return(Expression.Block(expressions));
            })
                                                                                       , Expression.Break(whileBreak)));

            return(Expression.Block(new[] { firstChar }, loopExpression, Expression.Label(whileBreak)));
        }