private void GenerateDeserializeMethodBody(StringBuilder sb, DeserializeInvocation invocation) { DzTreeContext treeContext = new DzTreeContext(); treeContext.IndentCSharp(+3); string ct = treeContext.CSharpIndent; List <MethodNode> nodes = new List <MethodNode>(); string invocationTypeStr = invocation.TypeArg.ToString(); DzJsonNode dzTree = BuildDzTree(invocation.TypeArg, "obj"); if (dzTree is null) { nodes.Add(new CSharpLineNode($"{ct}obj = default({invocationTypeStr});")); nodes.Add(new CSharpLineNode($"{ct}// Type '{invocationTypeStr}' isn't marked as serializable!")); } else { nodes.Add(new CSharpLineNode($"{ct}ReadOnlySpan<char> json = content.AsSpan().TrimStart();")); nodes.Add(new CSharpLineNode($"{ct}if (json.IsEmpty)")); nodes.Add(new CSharpLineNode($"{ct}{{")); ct = treeContext.IndentCSharp(+1); nodes.Add(new CSharpLineNode($"{ct}obj = default({invocationTypeStr});")); nodes.Add(new CSharpLineNode($"{ct}return;")); ct = treeContext.IndentCSharp(-1); nodes.Add(new CSharpLineNode($"{ct}}}")); nodes.Add(new CSharpNode($"{ct}obj = ")); nodes.AddRange(dzTree.GetNodes(treeContext)); nodes.Add(new CSharpLineNode($";")); } //nodes.Add(new CSharpLineNode($"{ct}return new {invocationTypeStr}();")); foreach (CSharpNode node in nodes.OfType <CSharpNode>()) { sb.Append(node.CSharpCode); } }
public void GenerateDeserializeMethod(StringBuilder sb, DeserializeInvocation invocation) { string invocationTypeStr = invocation.TypeArg.ToString(); if (_generatedDeserializationMethods.Contains(invocationTypeStr)) { return; } _generatedDeserializationMethods.Add(invocationTypeStr); const string SPC = " "; sb.Append($@"{SPC}{SPC}internal static void Deserialize(string content, out {invocationTypeStr} obj)"); sb.Append(@" { "); GenerateDeserializeMethodBody(sb, invocation); sb.Append(@" } "); }