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.JsonSerializer.Serialize Empty #656

Closed
ryanryanorient opened this issue Dec 17, 2022 · 3 comments
Closed

System.Text.Json.JsonSerializer.Serialize Empty #656

ryanryanorient opened this issue Dec 17, 2022 · 3 comments
Assignees
Labels

Comments

@ryanryanorient
Copy link

ryanryanorient commented Dec 17, 2022

Here is what to include in your request to make sure we implement a solution as quickly as possible.

1. Description

System.Text.Json.JsonSerializer.Serialize return {} .

2. Exception

System.Text.Json.JsonSerializer.Serialize return {} .

3. Fiddle or Project

           // .NET 5 ~ .NET 7

            IList<DynamicProperty> properties = new List<DynamicProperty>();

            properties.Add(new DynamicProperty("A",typeof(string)));

            DynamicClassFactory.CreateType(properties);

            var type = DynamicClassFactory.CreateType(properties);

            var list = new List<DynamicClass>();

            DynamicClass dynamicClass = Activator.CreateInstance(type) as DynamicClass;

            dynamicClass.SetDynamicPropertyValue("A", "A");

            var text = System.Text.Json.JsonSerializer.Serialize(dynamicClass);  // return {},expected {"A":"A"} 

           var text = System.Text.Json.JsonSerializer.Serialize(dynamicClass,typeof(object));  //return {"A":"A"}  OK  What happened?
            
            var text2 = Newtonsoft.Json.JsonConvert.SerializeObject(dynamicClass) //return {"A":"A"}  OK
@StefH
Copy link
Collaborator

StefH commented Feb 4, 2023

@ryanryanorient
This seems like a (breaking) change in System.Text.Json?
Did also post your question there?

@StefH
Copy link
Collaborator

StefH commented Mar 3, 2023

Solutions for this are:

var text = System.Text.Json.JsonSerializer.Serialize(dynamicClass, typeof(object));

var text = System.Text.Json.JsonSerializer.Serialize(dynamicClass, dynamicClass.GetType());

var text = System.Text.Json.JsonSerializer.Serialize(dynamicClass, type);

#679

@StefH StefH self-assigned this Mar 3, 2023
@StefH StefH added the question label Mar 3, 2023
@StefH
Copy link
Collaborator

StefH commented Mar 3, 2023

Hello @ryanryanorient,

I'm closing this issue for now, can you maybe create an issue in the System.Text.Json GitHub for this?

@StefH StefH closed this as completed Mar 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants