Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Text.Json does not serialize most of the properties that Newtonsoft does. #45764

Closed
StingyJack opened this issue Dec 8, 2020 · 5 comments

Comments

@StingyJack
Copy link

Im trying to use the new Json package and finding its doing an incomplete job. I checked the MaxDepth and its using the default of 99. Why wont this serialize correctly?

This RoslynPad example will illustrate

#r "nuget:Newtonsoft.Json/12.0.3"
#r "nuget:System.Text.Json/5.0.0"
#r "nuget:GraphQL-Parser/5.3.3"
using System.Text.Json;
using GraphQLParser;
using Newtonsoft.Json;

var gql = "{" +
            "     human(id: \"1000\") " +
            "     {" +
            "         name," +
            "         height(unit: FOOT)" +
            "     }" +
            "}";

var source = new Source(gql);
var lexer = new Lexer();
var parser = new Parser(lexer);
var result = parser.Parse(source);


System.Text.Json.JsonSerializer.Serialize<object>(result, new JsonSerializerOptions { WriteIndented = true }).Dump();
Newtonsoft.Json.JsonConvert.SerializeObject(result, Formatting.Indented).Dump();

the first result is

{
  "Definitions": [
    {
      "Kind": 2,
      "Location": {
        "Start": 0,
        "End": 78
      },
      "Comment": null
    }
  ],
  "Kind": 1,
  "Location": {
    "Start": 0,
    "End": 78
  },
  "Comment": null
}

the second is

{
  "Definitions": [
    {
      "Directives": null,
      "Kind": 2,
      "Name": null,
      "Operation": 0,
      "SelectionSet": {
        "Kind": 5,
        "Selections": [
          {
            "Alias": null,
            "Arguments": [
              {
                "Kind": 7,
                "Name": {
                  "Kind": 0,
                  "Value": "id",
                  "Location": {
                    "Start": 12,
                    "End": 14
                  },
                  "Comment": null
                },
                "Value": {
                  "Kind": 13,
                  "Value": "1000",
                  "Location": {
                    "Start": 16,
                    "End": 22
                  },
                  "Comment": null
                },
                "Location": {
                  "Start": 12,
                  "End": 22
                },
                "Comment": null
              }
            ],
            "Directives": null,
            "Kind": 6,
            "Name": {
              "Kind": 0,
              "Value": "human",
              "Location": {
                "Start": 6,
                "End": 11
              },
              "Comment": null
            },
            "SelectionSet": {
              "Kind": 5,
              "Selections": [
                {
                  "Alias": null,
                  "Arguments": null,
                  "Directives": null,
                  "Kind": 6,
                  "Name": {
                    "Kind": 0,
                    "Value": "name",
                    "Location": {
                      "Start": 39,
                      "End": 43
                    },
                    "Comment": null
                  },
                  "SelectionSet": null,
                  "Location": {
                    "Start": 39,
                    "End": 43
                  },
                  "Comment": null
                },
                {
                  "Alias": null,
                  "Arguments": [
                    {
                      "Kind": 7,
                      "Name": {
                        "Kind": 0,
                        "Value": "unit",
                        "Location": {
                          "Start": 60,
                          "End": 64
                        },
                        "Comment": null
                      },
                      "Value": {
                        "Kind": 15,
                        "Value": "FOOT",
                        "Location": {
                          "Start": 66,
                          "End": 70
                        },
                        "Comment": null
                      },
                      "Location": {
                        "Start": 60,
                        "End": 70
                      },
                      "Comment": null
                    }
                  ],
                  "Directives": null,
                  "Kind": 6,
                  "Name": {
                    "Kind": 0,
                    "Value": "height",
                    "Location": {
                      "Start": 53,
                      "End": 59
                    },
                    "Comment": null
                  },
                  "SelectionSet": null,
                  "Location": {
                    "Start": 53,
                    "End": 71
                  },
                  "Comment": null
                }
              ],
              "Location": {
                "Start": 29,
                "End": 77
              },
              "Comment": null
            },
            "Location": {
              "Start": 6,
              "End": 77
            },
            "Comment": null
          }
        ],
        "Location": {
          "Start": 0,
          "End": 78
        },
        "Comment": null
      },
      "VariableDefinitions": null,
      "Location": {
        "Start": 0,
        "End": 78
      },
      "Comment": null
    }
  ],
  "Kind": 1,
  "Location": {
    "Start": 0,
    "End": 78
  },
  "Comment": null
}
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Dec 8, 2020
@ericstj
Copy link
Member

ericstj commented Dec 8, 2020

Here's the type being serialized: https://github.com/graphql-dotnet/parser/blob/master/src/GraphQLParser/AST/GraphQLDocument.cs

Looks to me like this contains a List<ASTNode> where ASTNode is an abstract base and the values are more derived types. I believe this is due to lack of support for polymorhpic serialization / deserialization. #29937

cc @layomia @steveharter

@StingyJack
Copy link
Author

Whatever the reason is, the effect means that i cant trust the System.Text.Json package to handle serialization properly. I cant use something like that.

@Symbai
Copy link

Symbai commented Dec 8, 2020

Then you should up vote the requests about this, to show your interest #45189 otherwise we won't get support for it in .NET 6. Because .NET team believes this is an edge case scenario, when it isn't.

@ericstj
Copy link
Member

ericstj commented Dec 8, 2020

Upvotes help. This one is on our radar for .NET 6.0. https://themesof.net/?q=Polymorphic%20is:open%20kinds:teui /cc @terrajobst

@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Dec 9, 2020
@layomia
Copy link
Contributor

layomia commented Dec 9, 2020

Closing as a dup of #29937.

@layomia layomia closed this as completed Dec 9, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Jan 8, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants