private string GenerateReadArray(JensonPropertyInfo p) { var w = new SourceWriter(); w.Line("if (reader.TokenType != JsonTokenType.StartArray) throw new JsonException();"); w.Line(); w.Line($"{p.Name} = JsonSerializer.Deserialize<{p.TypeName}>(ref reader);"); return(w.ToString()); w.Line("var restore = reader;"); w.Line("var length = 0;"); w.Line(); w.Line("while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)"); w.Line("{"); w.Indent(); w.Line("if (reader.TokenType != JsonTokenType.Comment) length++;"); w.Line("reader.Skip();"); w.Dedent(); w.Line("}"); w.Line(); w.Line("reader = restore;"); w.Line($"{p.Name} = new {p.TypeName.Substring(0, p.TypeName.Length - 2)}[length];"); var itemType = p.ArrayItemType; if (itemType.EndsWith("[]")) { throw new NotImplementedException("Nested arrays are not implemented."); } //var attribList = new List<OpAttribute>(); //while (reader.Read()) //{ // if (reader.TokenType == JsonTokenType.EndArray) // { // attributes = attribList.ToArray(); // break; // } return(w.ToString()); }
private string GenerateReadBoolean(JensonPropertyInfo p) { var w = new SourceWriter(); w.Line($"if (reader.TokenType == JsonTokenType.True) {p.Name} = true;"); w.Line($"else if (reader.TokenType == JsonTokenType.False) {p.Name} = false;"); w.Line("else throw new JsonException(\"Boolean property must have true or false value.\");"); return(w.ToString()); }
public void Execute(SourceGeneratorContext generatorContext) { var ct = generatorContext.CancellationToken; var compilation = generatorContext.Compilation; var jensonSerializeAttribute = compilation.GetTypeByMetadataName("Jenson.Attributes.JensonSerializeAttribute"); var jensonPropertyAttribute = compilation.GetTypeByMetadataName("Jenson.Attributes.JensonPropertyAttribute"); var jensonTypeDiscriminatorAttribute = compilation.GetTypeByMetadataName("Jenson.Attributes.JensonTypeDiscriminatorAttribute"); var jsonPropertyNameAttribute = compilation.GetTypeByMetadataName("System.Text.Json.Serialization.JsonPropertyNameAttribute"); var jsonIgnoreAttribute = compilation.GetTypeByMetadataName("System.Text.Json.Serialization.JsonIgnoreAttribute"); if (jensonSerializeAttribute is null) { throw new Exception("Missing JensonSerializeAttribute in compilation."); } if (jensonPropertyAttribute is null) { throw new Exception("Missing JensonPropertyAttribute in compilation."); } if (jensonTypeDiscriminatorAttribute is null) { throw new Exception("Missing JensonTypeDiscriminatorAttribute in compilation."); } if (jsonPropertyNameAttribute is null) { throw new Exception("Missing JsonPropertyName in compilation."); } if (jsonIgnoreAttribute is null) { throw new Exception("Missing JsonIgnore in compilation."); } var context = new JensonContext( generatorContext, jensonSerializeAttribute, jensonPropertyAttribute, jensonTypeDiscriminatorAttribute, jsonPropertyNameAttribute, jsonIgnoreAttribute); var rootTypes = compilation.GlobalNamespace.GetAllTypes(ct) .Where(t => t.GetAttributes().Any(attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, jensonSerializeAttribute))); ct.ThrowIfCancellationRequested(); var sb = new StringBuilder(); foreach (var t in rootTypes) { var typeDeclarations = t.DeclaringSyntaxReferences.Select(sr => { return((TypeDeclarationSyntax)sr.GetSyntax()); }); var isPartial = typeDeclarations.Any(td => td.Modifiers.Any(SyntaxKind.PartialKeyword)); if (!isPartial) { foreach (var td in typeDeclarations) { var diagnostic = Diagnostic.Create(MissingPartialModifier, td.GetLocation(), new object[] { t.Name }); generatorContext.ReportDiagnostic(diagnostic); } } var w = new SourceWriter(); GenerateTypeSerializer(context, w, t); generatorContext.AddSource($"{t.Name}.jenson", SourceText.From(w.ToString(), Encoding.UTF8)); // for debugging sb.Append(w.ToString()); sb.AppendLine(); sb.AppendLine(); } // for debugging File.WriteAllText("C:\\Users\\jesse\\Desktop\\converter.cs", sb.ToString()); }