JsonContextGenerator.VisualBasic 1.0.1

dotnet add package JsonContextGenerator.VisualBasic --version 1.0.1
                    
NuGet\Install-Package JsonContextGenerator.VisualBasic -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JsonContextGenerator.VisualBasic" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JsonContextGenerator.VisualBasic" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="JsonContextGenerator.VisualBasic" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add JsonContextGenerator.VisualBasic --version 1.0.1
                    
#r "nuget: JsonContextGenerator.VisualBasic, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package JsonContextGenerator.VisualBasic@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=JsonContextGenerator.VisualBasic&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=JsonContextGenerator.VisualBasic&version=1.0.1
                    
Install as a Cake Tool

JsonContextGenerator.VisualBasic

A source generator that utilizes JsonSerializerContext in Visual Basic .NET, filling a major gap in the toolchain of the language. The JsonSerializerContext source generation has been a feature officially supported by the .NET SDK only for C#.

It automatically generates strongly-typed JsonSerializerContext classes for any VB.NET class annotated with GenerateJsonContextAttribute, enabling high-performance, source-generated JSON serialization with System.Text.Json.

v1.0.1 Stable Release: Initial version with stable working features, despite a few known limitations (see Current Limitations). Struct support is not available and might be planned for a future release.

Requirements

  • .NET 6.0 or higher
  • Visual Basic project

Why This Project Exists

The .NET SDK's native source generators for JSON (JsonSerializerContext) are C# only. VB.NET developers are left without an easy way to use source-generated serialization - until now.

This generator brings full JSON context support to VB.NET projects, with:

  • Attribute-driven configuration
  • Proper JsonSerializerOptions setup (Default / Strict / Web modes)
  • Compile-time type-safe property mapping
  • Runtime-friendly JsonTypeInfo resolution

Features

  • Works with VB classes (not structs, because structs are immutable)
  • Generates a dedicated {TypeName}JsonContext class
  • Supports JsonContextOption: Default, Strict, Web
  • Handles public properties automatically
  • Provides primitive type fallback in GetTypeInfo
  • Produces clean, readable, auto‑generated code
  • Fully incremental for fast builds

Installation

Add this generator to your VB.NET project via NuGet:

dotnet add package JsonContextGenerator.VisualBasic

Or reference the project directly:

<ProjectReference Include="..\JsonContextGenerator.VisualBasic\JsonContextGenerator.VisualBasic.vbproj"
    OutputItemType="Analyzer" ReferenceOutputAssembly="false" />

Usage

1. Annotate your type

Imports System.Text.Json.Serialization

' Specify a Web-mode context for Product:
<GenerateJsonContext(JsonContextOption.Web)>
Public Class Product
    Public Property Name As String
    Public Property Quantity As Integer
    Public Property Price As Decimal
End Class

' Or, for default JSON context:
<GenerateJsonContext>
Public Class Product
    Public Property Name As String
    Public Property Quantity As Integer
    Public Property Price As Decimal
End Class

2. Use the generated context

Dim apple As New Product With {.Name = "Apple", .Quantity = 10, .Price = 1.5D}
Dim productTypeInfo = ProductJsonContext.Default.ProductTypeInfo
Dim json = JsonSerializer.Serialize(apple, productTypeInfo)
Dim product = JsonSerializer.Deserialize(json, productTypeInfo)

Generated Code Example

For a class Product, the generator produces:

Partial Public Class ProductJsonContext
    Inherits JsonSerializerContext

    Public Shared Shadows ReadOnly Property [Default] As ProductJsonContext
        Get
            Return _defaultInstance.Value
        End Get
    End Property

    Public ReadOnly Property PersonTypeInfo As JsonTypeInfo(Of Person)
        Get
            If _person_TypeInfo Is Nothing Then _person_TypeInfo = CreatePersonTypeInfo(Options)
            Return _person_TypeInfo
        End Get
    End Property

    ' Full property mapping, options loading, and type resolution...
End Class

Configuration Options

Option Effect
Default WriteIndented = True
Strict Case‑sensitive, ignore read‑only properties
Web Case‑insensitive + ignore null values in output

Current Limitations

  • Only public properties are included in the JSON contract
  • No support for structs due to their immutability (even the boxing-unboxing approach does not work as expected)
  • No support for custom naming policies per property so far
  • Nested generic types are supported, but the context must be generated for the exact closed type

Contributing

Issues and pull requests are welcome. This generator aims to keep VB on par with C# for modern System.Text.Json features.

License

This project is licensed under the MIT License. See the LICENSE file for details.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.1 95 4/29/2026
1.0.0 86 4/29/2026