Ejemplo n.º 1
0
        public override void WriteDefinition(TextWriter writer)
        {
            writer.WriteLine("    using Newtonsoft.Json;");
            var converterName = Fullname.Replace(".", "") + "Converter";

            writer.Write("    [Newtonsoft.Json.JsonConverter(typeof(");
            writer.Write(converterName);
            writer.WriteLine("))]");
            base.WriteDefinition(writer);


            writer.Write("    internal class ");
            writer.Write(converterName);
            writer.Write(" : Newtonsoft.Json.Converters.CustomCreationConverter<");
            writer.Write(NetType);
            writer.WriteLine(">");
            writer.WriteLine("    {");
            writer.WriteLine("        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)");
            writer.WriteLine("        {");
            writer.WriteLine("            if (reader.TokenType == JsonToken.Null)");
            writer.WriteLine("                return null;");
            writer.WriteLine("");
            writer.WriteLine("            var jObject = (JObject)serializer.Deserialize(reader);");
            writer.WriteLine("            var localReader = new JTokenReader(jObject);");
            writer.Write("            var val = (");
            writer.Write(NetType);
            writer.WriteLine(")base.ReadJson(localReader, objectType, existingValue, serializer);");
            writer.WriteLine("");
            foreach (var baseType in _baseTypes)
            {
                writer.WriteLine("            localReader = new JTokenReader(jObject);");
                writer.Write("            val.As");
                writer.Write(baseType.TypeHandler.Fullname.Replace(".", ""));
                writer.Write(" = serializer.Deserialize<");
                writer.Write(baseType.TypeHandler.NetType);
                writer.WriteLine(">(localReader);");
            }
            writer.WriteLine("");
            writer.WriteLine("            return val;");
            writer.WriteLine("        }");
            writer.WriteLine("");
            writer.WriteLine("		  public override bool CanConvert(Type objectType)");
            writer.WriteLine("        {");
            writer.Write("            return objectType == typeof(");
            writer.Write(NetType);
            writer.WriteLine(");");
            writer.WriteLine("        }");
            writer.WriteLine("");
            writer.Write("        public override ");
            writer.Write(NetType);
            writer.WriteLine(" Create(Type objectType)");
            writer.WriteLine("        {");
            writer.Write("            return (");
            writer.Write(NetType);
            writer.WriteLine(") Activator.CreateInstance(objectType);");
            writer.WriteLine("        }");
            writer.WriteLine("    }");
        }