JamesNK / Newtonsoft.Json

Json.NET is a popular high-performance JSON framework for .NET
https://www.newtonsoft.com/json
MIT License
10.71k stars 3.24k forks source link

.net project output {} when serializing the model in publish mode (v9-v13.0.3) #2883

Open zd08513 opened 1 year ago

zd08513 commented 1 year ago

User userInfo = new User { id="11011", name="zhangsan", age=12,sex= false,address= "sicuan" }; Console.WriteLine(userInfo.id); string json=JsonConvert.SerializeObject(userInfo); Console.WriteLine(json);

image

elgonzo commented 1 year ago

Since i don't know what settings you used for the publish mode, i would like to ask you to narrow down the problem space.

First, in case you use AOT compiling, disable it and see whether the json serialization will start behaving. If not, also disable code trimming for publishing and see whether this will help.

If that doesn't seem to make the code behave either, you'll need to tell us more details about your problem: What is the build/platform target you are publishing for? What are the publish settings you are using? What build environment/tools are you using?

zd08513 commented 1 year ago

The problem found is the use of 'Obfuscar' plugin caused by the bug

PeeJay commented 12 months ago

I just had a similar problem, I was using JObject.ToString() and was also getting "{}" as the output. Disabling "Trim unused code" in publish options fixed the problem. Publish options were Release | Any CPU, net7.0, Self-Contained, linux-x64, produce single file