Beispiel #1
0
        private Expression CreateDeserializeReaderMethod()
        {
            var jsonReader = Expression.Parameter(FickleType.Define("JsonReader"), "reader");

            var conditionNull = Expression.MakeBinary(ExpressionType.Equal, FickleExpression.Call(jsonReader, typeof(Enum), "peek", null),
                                                      FickleExpression.Variable(typeof(Enum), "JsonToken.NULL"));

            var actionNull = FickleExpression.Block(
                new Expression[]
            {
                FickleExpression.Call(jsonReader, "skipValue", null),
                Expression.Continue(Expression.Label())
            });

            var self = FickleExpression.Variable(currentType, "this");

            var whileStatements = new List <Expression>
            {
                Expression.IfThen(conditionNull, actionNull),
                FickleExpression.Call(self, "deserializeElement", jsonReader)
            };

            var whileBody = FickleExpression.Block(whileStatements.ToArray());

            var whileExpression = FickleExpression.While(FickleExpression.Call(jsonReader, "hasNext", null), whileBody);

            var methodVariables = new List <ParameterExpression>();

            var methodStatements = new List <Expression>
            {
                FickleExpression.Call(jsonReader, "beginObject", null),
                whileExpression,
                FickleExpression.Call(jsonReader, "endObject", null),
            };

            var body = FickleExpression.Block(methodVariables.ToArray(), methodStatements.ToArray());

            return(new MethodDefinitionExpression("deserialize", new List <Expression>()
            {
                jsonReader
            }, AccessModifiers.Public, typeof(void), body, false, null, null, new List <Exception>()
            {
                new Exception()
            }));
        }
Beispiel #2
0
        private Expression CreateDeserializeArrayMethod()
        {
            var jsonReader = Expression.Parameter(FickleType.Define("JsonReader"), "reader");

            var result = FickleExpression.Variable(new FickleListType(currentType), "result");

            var resultNew = FickleExpression.New(new FickleListType(currentType), "FickleListType", null);

            var jsonReaderNextString = FickleExpression.Call(jsonReader, "nextString", null);

            var whileBody = FickleExpression.Block(FickleExpression.Call(result, "add", FickleExpression.StaticCall(currentType, "deserialize", jsonReaderNextString)));

            var whileExpression = FickleExpression.While(FickleExpression.Call(jsonReader, "hasNext", null), whileBody);

            var returnResult = Expression.Return(Expression.Label(), result).ToStatement();

            var methodVariables = new List <ParameterExpression>
            {
                result
            };

            var methodStatements = new List <Expression>
            {
                Expression.Assign(result, resultNew).ToStatement(),
                FickleExpression.Call(jsonReader, "beginArray", null),
                whileExpression,
                FickleExpression.Call(jsonReader, "endArray", null),
                returnResult
            };

            var body = FickleExpression.Block(methodVariables.ToArray(), methodStatements.ToArray());

            return(new MethodDefinitionExpression("deserializeArray", new List <Expression>()
            {
                jsonReader
            }, AccessModifiers.Public | AccessModifiers.Static, new FickleListType(currentType), body, false, null, null, new List <Exception>()
            {
                new Exception()
            }));
        }
Beispiel #3
0
        private Expression CreateDeserializeElementMethod()
        {
            var jsonReader = Expression.Parameter(FickleType.Define("JsonReader"), "reader");

            var jsonElementName = Expression.Variable(typeof(String), "elementName");

            Expression ifThenElseExpression;

            if (currentTypeDefinition.Type.BaseType != null && currentTypeDefinition.Type.BaseType != typeof(Object))
            {
                var superType = currentTypeDefinition.Type.BaseType;
                var super     = Expression.Variable(superType, "super");

                ifThenElseExpression = FickleExpression.Block(FickleExpression.Call(super, "deserializeElement", jsonReader));
            }
            else
            {
                ifThenElseExpression = FickleExpression.Block(FickleExpression.Call(jsonReader, "skipValue", null));
            }

            var self = Expression.Variable(currentType, "this");

            foreach (var serviceProperty in ((FickleType)currentTypeDefinition.Type).ServiceClass.Properties)
            {
                Expression action = null;

                var propertyType = codeGenerationContext.ServiceModel.GetTypeFromName(serviceProperty.TypeName);

                if (propertyType is FickleListType)
                {
                    var listItemType = ((FickleListType)propertyType).ListElementType;

                    if (listItemType is FickleNullable)
                    {
                        listItemType = listItemType.GetUnderlyingType();
                    }

                    if (listItemType.IsEnum)
                    {
                        var convertDtoCall = FickleExpression.StaticCall(listItemType.Name, "deserializeArray", jsonReader);
                        action = FickleExpression.Block(FickleExpression.Call(self, "set" + serviceProperty.Name, convertDtoCall));
                    }
                    else
                    {
                        var result = FickleExpression.Variable(new FickleListType(listItemType), serviceProperty.Name.Uncapitalize());

                        var resultNew = FickleExpression.New(new FickleListType(listItemType), "FickleListType", null);

                        var jsonReaderNextString = FickleExpression.Call(jsonReader, typeof(String), "nextString", null);

                        Expression whileBody;

                        if (TypeSystem.IsPrimitiveType(listItemType))
                        {
                            whileBody = FickleExpression.Block(
                                FickleExpression.Call(result, "add", Expression.Convert(Expression.Convert(jsonReaderNextString, typeof(Object)), listItemType))
                                );
                        }
                        else
                        {
                            var objectToDeserialize = FickleExpression.Variable(listItemType, listItemType.Name.Uncapitalize());

                            var objectNew = Expression.Assign(objectToDeserialize, Expression.New(listItemType));

                            var whileVariables = new List <ParameterExpression>
                            {
                                objectToDeserialize
                            };

                            var whileStatements = new List <Expression>
                            {
                                objectNew,
                                FickleExpression.Call(objectToDeserialize, "deserialize", jsonReader),
                                FickleExpression.Call(result, "add", objectToDeserialize)
                            };

                            whileBody = FickleExpression.Block(whileVariables.ToArray(), whileStatements.ToArray());
                        }

                        var whileExpression = FickleExpression.While(FickleExpression.Call(jsonReader, "hasNext", null), whileBody);

                        var setResult = FickleExpression.Call(self, "set" + serviceProperty.Name, result).ToStatement();

                        var variables = new List <ParameterExpression>
                        {
                            result
                        };

                        var statements = new List <Expression>
                        {
                            Expression.Assign(result, resultNew).ToStatement(),
                            FickleExpression.Call(jsonReader, "beginArray", null),
                            whileExpression,
                            FickleExpression.Call(jsonReader, "endArray", null),
                            setResult
                        };

                        action = FickleExpression.Block(variables.ToArray(), statements.ToArray());
                    }
                }
                else if (TypeSystem.IsNotPrimitiveType(propertyType))
                {
                    var value = FickleExpression.Variable(propertyType, "value");

                    var valueNew = Expression.Assign(value, Expression.New(propertyType));

                    var convertDtoCall = FickleExpression.Call(value, "deserialize", jsonReader);

                    var variables = new List <ParameterExpression>
                    {
                        value
                    };

                    var statements = new List <Expression>
                    {
                        valueNew,
                        convertDtoCall,
                        FickleExpression.Call(self, "set" + serviceProperty.Name, value)
                    }.ToStatementisedGroupedExpression();

                    action = Expression.Block(variables.ToArray(), statements);
                }
                else if (propertyType.GetUnwrappedNullableType().IsEnum)
                {
                    var getPrimitiveElementCall = FickleExpression.Call(jsonReader, "nextString", null);
                    var convertDtoCall          = FickleExpression.StaticCall(serviceProperty.TypeName, "deserialize", getPrimitiveElementCall);
                    action = FickleExpression.Block(FickleExpression.Call(self, "set" + serviceProperty.Name, convertDtoCall));
                }
                else
                {
                    var getPrimitiveElementCall = FickleExpression.Call(jsonReader, "nextString", null);
                    var convertPrimitiveCall    = FickleExpression.StaticCall(propertyType, SourceCodeGenerator.ToObjectMethod, getPrimitiveElementCall);
                    action = FickleExpression.Block(FickleExpression.Call(self, "set" + serviceProperty.Name, convertPrimitiveCall));
                }

                var condition = FickleExpression.Call(jsonElementName, typeof(Boolean), "equals", Expression.Constant(serviceProperty.Name, typeof(String)));

                var currentExpression = Expression.IfThenElse(condition, action, ifThenElseExpression);

                ifThenElseExpression = currentExpression;
            }

            var methodVariables = new List <ParameterExpression>
            {
                jsonElementName
            };

            var methodStatements = new List <Expression>
            {
                Expression.Assign(jsonElementName, FickleExpression.Call(jsonReader, typeof(String), "nextName", null)).ToStatement(),
                ifThenElseExpression
            };

            var body = FickleExpression.Block(methodVariables.ToArray(), methodStatements.ToArray());

            return(new MethodDefinitionExpression("deserializeElement", new List <Expression>()
            {
                jsonReader
            }, AccessModifiers.Public, typeof(void), body, false, null, null, new List <Exception>()
            {
                new Exception()
            }));
        }