Search Unity

Question Getting Source Generators (System.Text.Json) to work in Unity 2021.3

Discussion in 'Testing & Automation' started by jni97, Mar 28, 2023.

  1. jni97

    jni97

    Joined:
    May 26, 2015
    Posts:
    3
    Hey, in the past few days I've been trying to get the System.Text.Json package from nuget to work with the included source generators to generate serialization code for some classes.
    The System.Text.Json package was added as net.standard 2.0 dll (and all dependencies too) and I'm using Unity 2021.3.18.

    To get the source generator to work, I followed this page from the official docs:
    https://docs.unity3d.com/2021.3/Documentation/Manual/roslyn-analyzers.html
    which states that:
    I tried pretty much all versions from 6.0.0-preview.1.21102.12 to 7.0.2

    However, while the custom source generator and the example with ErrorProne.NET work without any issues, when I try to use any version of the System.Text.Json generators, it only works within visual studio, but unity doesnt recognize the generated files and therefore complains about missing implementations which prevents compilation. It's weird because the custom source generator from the example also generates a file, which is detected and compiled.

    This is the code that uses the generator to generate the serialization code for a simple class:
    (Works in a regular .net application and visual studio can also compile without problems)
    Code (CSharp):
    1. [System.Serializable]
    2. public class Data
    3. {
    4.     public int x = 7;
    5. }
    6.  
    7. [JsonSerializable(typeof(Data))]
    8. internal partial class MyJsonContext : JsonSerializerContext
    9. {
    10.  
    11. }
    I can also see the generated code in visual studio which contains the required overrides for the abstract JsonSerializerContext class and build it there.

    However, the errors in Unity's console indicate that unity doesnt 'see' the generated file, because the generated overrides are missing:
    Is there any way to hint the generated files to unity or something else I could try?

    Example project which contains both example from the docs and System.Text.Json version 6.0.0 is attached.
    Oh, and switching to Unity 2022.2.12 fixes the problem, but unfortunately I have to use the 2021.3 LTS version
     

    Attached Files:

  2. stephyswe

    stephyswe

    Joined:
    Aug 23, 2017
    Posts:
    2
    Can you tell more specific what you trying to do?
    I'm asking, cause, I'm using JSON in Saving data. And I been able to handle JSON data load/fetch from file. Plus added it into a test. I might be able to help you if you can tell what you trying to do.

    A Hint: I'm using NewtonSoft.Json.dll
     

    Attached Files:

  3. a_p_u_r_o

    a_p_u_r_o

    Joined:
    May 27, 2015
    Posts:
    23
    I was in the same situation as the OP and rigorously searching for the answer to this.
    I came here only to find out that no answer has been posted yet.

    I will post my findings here.

    With Unity version 2021.3, tested 2021.3.18 and others up to 2021.3.32, System.Text.Json 6.0.0-preview.7.21377.19 is the version to use.
    System.Text.Json 6.0.0-preview has a slight limitation compared to other later stable releases. That is both your serialization target type and the json context type must be declared in a namespace.

    Actual set of packages you will be using would be like this.
    System.Text.Json 6.0.0-preview.7.21377.19
    System.Text.Encodings.Web 6.0.0-preview.7.21377.19
    System.Runtime.CompilerServices.Unsafe 6.0.0-preview.7.21377.19
    Microsoft.Bcl.AsyncInterfaces 6.0.0-preview.7.21377.19

    edit: There is another hitch which I forgot to mention.
    When either Engineering feature set com.unity.feature.development
    or Code Coverage Package com.unity.testtools.codecoverage
    is installed, System.Text.Json.SourceGeneration.dll won't work for unknown reason.
     
    Last edited: Dec 4, 2023
  4. a_p_u_r_o

    a_p_u_r_o

    Joined:
    May 27, 2015
    Posts:
    23
    Above version requirement is because the version of Microsoft.CodeAnalysis.CSharp.dll that Unity 2021.3 includes is 3.9 .
    C:\Program Files\Unity\Hub\Editor\2021.3.18f1\Editor\Data\DotNetSdkRosly\Microsoft.CodeAnalysis.CSharp.dll

    There is absolutely no warranty but you could replace these dlls inside Unity to newer version, say to 3.11 .
    Microsoft.CodeAnalysis.dll
    Microsoft.CodeAnalysis.CSharp.dll
    Microsoft.CodeAnalysis.VisualBasic.dll
    which you can grab from following packages.

    Microsoft.CodeAnalysis.Common 3.11.0
    Microsoft.CodeAnalysis.CSharp 3.11.0
    Microsoft.CodeAnalysis.VisualBasic 3.11.0

    Then project archive uploaded by OP, SourceGenerators.zip, should work fine.
     
  5. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    which version of CodeAnalysis.CSharp is required by System.Text.Json ?
     
  6. a_p_u_r_o

    a_p_u_r_o

    Joined:
    May 27, 2015
    Posts:
    23