private static void WriteSingleton(IndentedStringBuilder sb, SerializerClassesName names) { sb.AppendLineInvariant($"private static IStaticSerializer<{names.TypeName}> _instance = null;"); sb.AppendLineInvariant($"public static IStaticSerializer<{names.TypeName}> Instance => _instance ?? (_instance = new {names.SerializerName}());"); sb.AppendLineInvariant($"private static IStaticSerializer<{names.BuilderTypeFullName}> _builder => {names.BuilderSerializerName}.Instance;"); sb.AppendLine(); }
private void WriteConstructor(IndentedStringBuilder sb, SerializerClassesName names) { if (!_disableToUpperCtor) { sb.AppendLineInvariant("// If the next line fails to compile, you're probably running an older revision of Umbrella which does not support"); sb.AppendLineInvariant("// configurable use of the toUpper parameter. If you cannot update to the a release that supports it, set the"); sb.AppendLineInvariant("// DisableToUpperConstructor configuration parameter to true, in the SerializationConfig.xml file."); sb.AppendLineInvariant("// Note that disabling this feature has a performance in impact, which adds pressure to the GC."); sb.AppendLineInvariant("public {0}() : base(false){{ }}", names.SerializerName); } }
private void WriteDeserialization(IndentedStringBuilder sb, SerializerClassesName names) { sb.AppendLine( $@"object IStaticSerializer.Deserialize(JsonReader {SerializerConstants.ReaderParameterName}, char firstChar, out char? {_overChar}) {{ return Deserialize({SerializerConstants.ReaderParameterName}, firstChar, out {_overChar}); }} public {names.TypeName} Deserialize(JsonReader {SerializerConstants.ReaderParameterName}, char firstChar, out char? {_overChar}) {{ Uno.IImmutableBuilder<{names.TypeName}> builderInstance = _builder.Deserialize({SerializerConstants.ReaderParameterName}, firstChar, out {_overChar}); return builderInstance?.ToImmutable(); }}"); sb.AppendLine(); }
private void WriteSerialization(IndentedStringBuilder sb, SerializerClassesName names, DeserializationPropertyInfo[] propertyInfos) { sb.AppendLine($@" protected override void SerializeCore(JsonWriter {_writer}, {names.TypeName} entity) {{ if (entity == null) {{ {_writer}.WriteNullValue(); }} else {{ using(var {_object} = {_writer}.OpenObject()) {{ {propertyInfos.Select(property => _propertyGenerator().GetWrite(property.PropertyName, "entity", property.Property)).JoinBy(Environment.NewLine)} }} }} }} " ); }
private void WriteConstructor(IndentedStringBuilder sb, SerializerClassesName names) { sb.AppendLine($"private {names.SerializerName}(){{ }}"); }
private void WriteDeserialization(IndentedStringBuilder sb, SerializerClassesName names, DeserializationPropertyInfo[] propertyInfos) { // Generate the switch/case mapping manually, to be able to use OrdinalIgnoreCase comparison using (sb.BlockInvariant("private static readonly Dictionary<string, int> _propertyMap = new Dictionary<string, int>({0}, StringComparer.OrdinalIgnoreCase)", propertyInfos.Length)) { // For each properties for this type foreach (var propertyInfo in propertyInfos.Select((v, i) => new { Value = v, Index = i })) { sb.AppendLineInvariant("{{ \"{0}\", {1} }},", propertyInfo.Value.PropertyName.ToUpperInvariant(), propertyInfo.Index); } } sb.AppendLine(";"); sb.AppendLine(); // Read property method (switch case over property names) using (sb.BlockInvariant("protected override void ReadProperty({0} {1}, string {2}, JsonReader {3}, out char? {4})", names.TypeName, SerializerConstants.EntityParameterName, SerializerConstants.PropertyNameParameterName, SerializerConstants.ReaderParameterName, _overChar)) { using (sb.BlockInvariant("switch (_propertyMap.UnoGetValueOrDefault({0}, -1))", SerializerConstants.PropertyNameParameterName)) { // For each properties for this type foreach (var pair in propertyInfos.Select((v, i) => new { Value = v, Index = i })) { var propertyInfo = pair.Value; // Write the "case" sb.AppendLineInvariant("case {0}:", pair.Index); using (sb.Indent()) { sb.AppendLineInvariant("Read{2}Value({1}, {3}, out {4});", String.Empty, SerializerConstants.EntityParameterName, propertyInfo.Property.Name, SerializerConstants.ReaderParameterName, _overChar); sb.AppendLineInvariant("break;"); } } sb.AppendLineInvariant("default:"); using (sb.Indent()) { sb.AppendLineInvariant("{0}.SkipItem(out {1});", SerializerConstants.ReaderParameterName, _overChar); sb.AppendLineInvariant("break;"); } } } sb.AppendLine(); // Create the deserizalition methods foreach (var property in propertyInfos) { using (sb.BlockInvariant("private void Read{2}Value({0} {1}, JsonReader {3}, out char? {4})", names.TypeName, SerializerConstants.EntityParameterName, property.Property.Name, SerializerConstants.ReaderParameterName, _overChar)) { sb.AppendLine(_propertyGenerator().GetRead(SerializerConstants.EntityParameterName, property.Property)); sb.AppendLine(); } sb.AppendLine(); } }
private static void WriteSingleton(IndentedStringBuilder sb, SerializerClassesName names) { sb.AppendLineInvariant("private static IStaticSerializer<{0}> _instance = null;", names.TypeName); sb.AppendLineInvariant("public static IStaticSerializer<{0}> Instance => _instance ?? (_instance = new {1}());", names.TypeName, names.SerializerName); sb.AppendLine(); }