Skip to content
This repository has been archived by the owner on May 16, 2022. It is now read-only.

Not the entire structure is converted to JSON #233

Open
unkprog opened this issue Sep 9, 2020 · 0 comments
Open

Not the entire structure is converted to JSON #233

unkprog opened this issue Sep 9, 2020 · 0 comments

Comments

@unkprog
Copy link

unkprog commented Sep 9, 2020

Example:

    [DataContract]
    public class BaseModel
    {
        public BaseModel()
        {
            Name = "BaseModel";
        }

        [DataMember]
        public string Name { get; set; }

    }

    [DataContract]
    public class OneModel :BaseModel
    {
        public OneModel()
        {
            Name = "OneModel";
        }
        public List<BaseModel> OneItems { get; set; } = new List<BaseModel>(new BaseModel[] { new BaseModel() { Name = "One" }, new BaseModel() { Name = "Two" } });
    }

    [DataContract]
    public class TwoModel : OneModel
    {
        public TwoModel()
        {
            Name = "TwoModel";
        }

        [DataMember]
        public List<BaseModel> Items { get; set; } = new List<BaseModel>(new BaseModel[] { new BaseModel() { Name = "One BaseModel" }, new OneModel() { Name = "Two OneModel" } });
    }

Converting to JSON

            string jsonString = System.Text.Encoding.UTF8.GetString(JsonSerializer.Serialize(new TwoModel(), Utf8Json.Resolvers.StandardResolver.Default)); 
            System.Console.WriteLine(jsonString);

We get the following:

{
	"Name":"TwoModel",
	"Items":[
				{"Name":"One BaseModel"},{"Name":"Two OneModel"}
			],
    "OneItems":[{"Name":"One"},{"Name":"Two"}]
}

and it should be the following JSON:

{
	"Name":"TwoModel",
	"Items":[
				{"Name":"One BaseModel"},
				{
				  "Name":"Two OneModel",
				  "OneItems":[{"Name":"One"},{"Name":"Two"}]
				}
			],
    "OneItems":[{"Name":"One"},{"Name":"Two"}]
}
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant