Share via

SourceGenerationContext is not declared. It may be inaccessible due to its protection level. (JSON Deserializing)

Fiq 31 Reputation points
2024-01-06T12:46:25.99+00:00

Hello.

I'm creating a .NET 8 Library using VB and working with System.Text.Json.

    Public Function FetchUserData() As BirthPlaceData
         Dim jOption As JsonSerializerOptions = New JsonSerializerOptions With {             
			 .PropertyNameCaseInsensitive = True,
             .TypeInfoResolver = SourceGenerationContext.Default
         }
          Dim POBList As List(Of BirthPlaceData) = JsonSerializer.Deserialize(Of List(Of BirthPlaceData))(pPOBDatabase, jOption)
         Dim POBData As BirthPlaceData = POBList.Find(Function(u) u.BirthPlaceCode = pBirthPlaceCode)
         Return POBData
     End Function 

Here is my JSON data.

[
    {
        "BirthPlaceCode": "01",
        "POBState": "Johor",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "02",
        "POBState": "Kedah",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "03",
        "POBState": "Kelantan",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "04",
        "POBState": "Melaka",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "05",
        "POBState": "Negeri Sembilan",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "06",
        "POBState": "Pahang",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "07",
        "POBState": "Pulau Pinang",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "08",
        "POBState": "Perak",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "09",
        "POBState": "Perlis",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "10",
        "POBState": "Selangor",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "11",
        "POBState": "Terengganu",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "12",
        "POBState": "Sabah",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "13",
        "POBState": "Sarawak",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "14",
        "POBState": "Wilayah Persekutuan Kuala Lumpur",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "15",
        "POBState": "Wilayah Persebutuan Labuan",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "16",
        "POBState": "Wilayah Persekutuan Putrajaya",
        "POBCountry": "Malaysia"
    },
    {
        "BirthPlaceCode": "17",
        "POBState": " ",
        "POBCountry": " "
    },
    {
        "BirthPlaceCode": "18",
        "POBState": " ",
        "POBCountry": " "
    },
    {
        "BirthPlaceCode": "19",
        "POBState": " ",
        "POBCountry": " "
    },
    {
        "BirthPlaceCode": "20",
        "POBState": " ",
        "POBCountry": " "
    }
]

I am getting "SourceGenerationContext is not declared. It may be inaccessible due to its protection level." The same error occurred when I test in C#.

If I omitted the SourceGenerationContext , it also raised an error "Reflection-based serialization has been disabled for this application. Either use the source generator APIs or explicitly configure the 'JsonSerializerOptions.TypeInfoResolver' property."

I have been searching from the web for the past 2 days but no result. The only result I found is

Any help is really appreciated. Thanks.

Developer technologies | .NET | Other
Developer technologies | VB
0 comments No comments

Answer accepted by question author

Peter Fleischer (former MVP) 19,351 Reputation points
2024-01-06T19:15:35.57+00:00

HI,
I cannot reproduce your problem with simple code:

Imports System
Imports System.IO
Imports System.Text.Json
Imports System.Text.Json.Serialization
Imports System.Text.Json.Serialization.Metadata

Module Program01
  Sub Main()
    Try
      Call (New Demo).Execute()
    Catch ex As Exception
      Console.WriteLine(ex.ToString)
    End Try
    Console.WriteLine("Continue enter key")
    Console.ReadKey()
  End Sub

  Friend Class Demo

    Friend Sub Execute()

      Dim result = FetchUserData()
      Console.WriteLine(result.POBState)
    End Sub

    Dim pBirthPlaceCode As String = "05"
    Dim pPOBDatabase As New FileStream("Module86.json", FileMode.Open)

    Public Function FetchUserData() As BirthPlaceData
      Dim jOption As New JsonSerializerOptions With {
        .PropertyNameCaseInsensitive = True,
        .TypeInfoResolver = SourceGenerationContext.Default
      }
      Dim POBList As List(Of BirthPlaceData) = JsonSerializer.Deserialize(Of List(Of BirthPlaceData))(pPOBDatabase, jOption)
      Dim POBData As BirthPlaceData = POBList.Find(Function(u) u.BirthPlaceCode = pBirthPlaceCode)
      Return POBData
    End Function

  End Class

  Public Class BirthPlaceData
    Public Property BirthPlaceCode As String
    Public Property POBState As String
    Public Property POBCountry As String
  End Class

  <JsonSourceGenerationOptions(WriteIndented:=True), JsonSerializable(GetType(BirthPlaceData))>
  Public Class SourceGenerationContext
    Inherits JsonSerializerContext

    Public Sub New(options As JsonSerializerOptions)
      MyBase.New(options)
    End Sub

    Public Shared Property [Default] As IJsonTypeInfoResolver

    Protected Overrides ReadOnly Property GeneratedSerializerOptions As JsonSerializerOptions
      Get
        Throw New NotImplementedException()
      End Get
    End Property

    Public Overrides Function GetTypeInfo(type As Type) As JsonTypeInfo
      Throw New NotImplementedException()
    End Function
  End Class

End Module

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

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