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

using Serialize fails but the Newtonsoft version works #32294

Closed
Greypuma opened this issue Feb 14, 2020 · 2 comments
Closed

using Serialize fails but the Newtonsoft version works #32294

Greypuma opened this issue Feb 14, 2020 · 2 comments

Comments

@Greypuma
Copy link

I was trying to serialize an object into json. I get an empty string. below is the code i used.
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using System.Text.Json.Serialization;
using Newtonsoft.Json;

namespace test
{
public class AdbEmulator
{
//needs to be public otherwise wont work
public string name;
internal string attached;

}
public class AndroidJson
{
    public AndroidJson()
    {
        AddData();
    }
    public void AddData()
    {
        AdbEmulator adb1 = new AdbEmulator() { name = "emulator-5554", attached = "ll" };
        emulators.Add(adb1);
        string jsonAdb = System.Text.Json.JsonSerializer.Serialize<AdbEmulator>(adb1);
        string newtonjson = Newtonsoft.Json.JsonConvert.SerializeObject(adb1);
    }
}

Program.cs
static void Main(string[] args)
{ AndroidJson aj = new AndroidJson(); }

Output
jsonAdb = {}
newtonjson ={"name":"emulator-5554"}

I'm not sure why newtonsoft works and the microsoft one does not.
i'v'e tried the code in an dotnet core 3.1 project and windows application ( including the nuget package for that project).

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the untriaged New issue has not been triaged by the area owner label Feb 14, 2020
@GalaxiaGuy
Copy link

Serializing fields is not yet supported:
#876

@ahsonkhan
Copy link
Member

@Greypuma, if you turn your fields into properties it should work as expected. Please re-open if you still see issues.

Thanks.

using System.Text.Json;
using System.Text.Json.Serialization;

namespace test
{
    public class AdbEmulator
    {
        //needs to be public otherwise wont work
        public string name {get; set;}
        internal string attached;
    }
}

@ahsonkhan ahsonkhan added this to the 5.0 milestone Feb 15, 2020
@ahsonkhan ahsonkhan removed the untriaged New issue has not been triaged by the area owner label Feb 15, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
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