Newtonsoft.Json icon indicating copy to clipboard operation
Newtonsoft.Json copied to clipboard

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

Open zd08513 opened this issue 2 years ago • 3 comments

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

zd08513 avatar Aug 14 '23 09:08 zd08513

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?

elgonzo avatar Aug 14 '23 11:08 elgonzo

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

zd08513 avatar Aug 15 '23 01:08 zd08513

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

PeeJay avatar Sep 04 '23 02:09 PeeJay