How to Inherit JsonSerializerContext class in VB.net ? (.net6/7)

Haibo Yang 1 Reputation point
2022-11-02T13:01:27.35+00:00

In the C# project(.net6/7), I can run the following code and successfully output the serialized content.

 public class Test {  
     public string AA { get; set; } = "A";  
     public string BB { get; set; } = "B";  
 }          
 [JsonSerializable(typeof(Test))]      
 public partial class TestContext : JsonSerializerContext  
 {       
 }  
  
var T = new Test();  
var utf8Json = JsonSerializer.SerializeToUtf8Bytes(T, TestContext.Default.Test);  
var s = Encoding.UTF8.GetString(utf8Json);  
Console.WriteLine(s);  
//{"AA":"A","BB":"B"}  

I must Inherit the JsonSerializerContext class,
Because I need to compile it as a Windows native code application (aot). When I not use JsonSerializerContext to directly serialize custom classes under dotnet6/7, The result is null. eg:{}。

Inherit JsonSerializerContext class works well in C #, but it doesn't seem to work in VB.
How can I achieve this in vb.net project(.net6/7)?

Please help me, please post the vb sample code. thanks!

try-the-new-system-text-json-source-generator

Developer technologies VB
{count} votes

1 answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-11-03T08:40:56.35+00:00

    Hi @Haibo Yang ,
    You can refer to the document How to use source generation in System.Text.Json.
    It is also recommended that you post your problem on the github issue if the document does not help.
    https://github.com/dotnet/runtime/issues
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.