public override IEnumerable <MethodNode> GetNodes(DzTreeContext context) { string ct = context.CSharpIndent; yield return(new CSharpLineNode($"{ct}json = json.TrimStart();")); yield return(new CSharpLineNode($"{ct}var NULL_SPAN = \"null\".AsSpan();")); yield return(new CSharpLineNode($"{ct}if (json.StartsWith(NULL_SPAN))")); yield return(new CSharpLineNode($"{ct}{{")); ct = context.IndentCSharp(+1); yield return(new CSharpLineNode($"{ct}json = json.Slice(4);")); yield return(new CSharpLineNode($"{ct}return default({Type});")); ct = context.IndentCSharp(-1); yield return(new CSharpLineNode($"{ct}}}")); string errmsg = "Invalid JSON at position: {content.Length - json.Length}. Expected '['"; yield return(new CSharpLineNode($"{ct}if (json[0] != '[') throw new Exception($\"{errmsg}\");")); yield return(new CSharpLineNode($"{ct}json = json.Slice(1);")); // Parse list items yield return(new CSharpLineNode($"{ct}System.Collections.Generic.List<{Type}> obj = new System.Collections.Generic.List<{Type}>();")); yield return(new CSharpLineNode($"{ct}while (true)")); yield return(new CSharpLineNode($"{ct}{{")); ct = context.IndentCSharp(+1); yield return(new CSharpLineNode($"{ct}json = json.TrimStart();")); errmsg = "Invalid JSON at position: {content.Length - json.Length}. Expected object content or ']'"; yield return(new CSharpLineNode($"{ct}if (json.IsEmpty) throw new Exception($\"{errmsg}\");")); yield return(new CSharpLineNode($"{ct}if (json[0] == ']') break;")); // list item foreach (MethodNode methodNode in Property.GetNodes(context)) { yield return(methodNode); } yield return(new CSharpLineNode($"{ct}json = json.TrimStart();")); yield return(new CSharpLineNode($"{ct}if (json[0] == ',') json = json.Slice(1);")); ct = context.IndentCSharp(-1); yield return(new CSharpLineNode($"{ct}}}")); yield return(new CSharpLineNode($"{ct}json = json.Slice(1);")); yield return(new CSharpLineNode($"{ct}return obj;")); }
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); } }
private void WriteDeserializeList(StringBuilder sb, DzListNode listNode) { string objectTypeStrValid = listNode.Type.Replace(".", "_").Replace("<", "_").Replace(">", ""); const string SPC = " "; sb.Append($@"{SPC}{SPC}private static System.Collections.Generic.List<{listNode.Type}> DeserializeList_{objectTypeStrValid}(ref string content, ref ReadOnlySpan<char> json) {{ "); DzTreeContext treeContext = new DzTreeContext(); treeContext.IndentCSharp(+3); foreach (CSharpNode node in listNode.GetNodes(treeContext).OfType <CSharpNode>()) { sb.Append(node.CSharpCode); } sb.Append($@" }} "); }
private void WriteDeserializeObject(StringBuilder sb, DzObjectNode objNode) { string objectTypeStrValid = objNode.Type.Replace(".", "_"); const string SPC = " "; sb.Append($@"{SPC}{SPC}private static {objNode.Type} Deserialize_{objectTypeStrValid}(ref string content, ref ReadOnlySpan<char> json) {{ "); DzTreeContext treeContext = new DzTreeContext(); treeContext.IndentCSharp(+3); foreach (CSharpNode node in objNode.GetNodes(treeContext).OfType <CSharpNode>()) { sb.Append(node.CSharpCode); } sb.Append($@" }} "); }