6

Given this class, I am trying to serialize but it isn't working.

using System.Text.Json;
public class Service
{
    public Service() { }
    public string service;
    public string description;
}

        Service c = new Service();
        c.description = "desc";
        c.service = "serv";
        string x = JsonSerializer.Serialize<Service>(c);

Debugging I see x == "{}". What am I missing?

C# .net core 3.1

Alan Baljeu
  • 2,383
  • 4
  • 25
  • 40
  • 4
    Those are fields, not properties. Change them to properties. – madreflection Jan 17 '20 at 16:40
  • There's a few things wrong here. First, your class `Service` is not well formed. You need getters and setters. But second, this line: `string x = JsonSerializer.Serialize(c);` is not making sense to me. You don't need a type when serializing. In other words, you don't need `` – Casey Crookston Jan 17 '20 at 16:44
  • @CaseyCrookston Serialize can take a generic. You're making an assumption that this is JSON.Net or the .Net serializer. Kentico, for instance, has a [serialize method that does take a generic type parameter](https://devnet.kentico.com/docs/8_2/api/html/M_CMS_DataCom_JsonSerializer_Serialize__1.htm). The `System.Text.Json` serializer has generic overloads, but they take additional parameters not seen here. That said, you're right, its not necessary to specify the type. –  Jan 17 '20 at 16:54
  • @Amy, ok, good to know! I looked at the def for `JsonSerializer.Serialize` before making this comment, and didn't see an option for using a type. – Casey Crookston Jan 17 '20 at 16:56
  • @CaseyCrookston perhaps the template argument is optional, but MS provided that version and for symmetry with the Deserialize function, why not ? – Alan Baljeu Jan 17 '20 at 16:57
  • @CaseyCrookston You can find them [here on MSDN](https://learn.microsoft.com/en-us/dotnet/api/system.text.json.jsonserializer.serialize?view=netcore-3.1). The 3rd and 4th overloads are generic. But like I said, they take more than one parameter. –  Jan 17 '20 at 16:57
  • Fields are not supported by `System.Text.Json`, see [How to use class fields with System.Text.Json.JsonSerializer?](https://stackoverflow.com/q/58139759/3744182). Use properties instead. – dbc Jan 19 '20 at 02:16

3 Answers3

10

You are missing setter and getter

public class Service
{
    public Service() { }
    public string service {get; set;}
    public string description {get; set;}
}
Roman.Pavelko
  • 1,555
  • 2
  • 15
  • 18
3
public class Service
{
    public Service() { }
    public string SomethingElse { get; set; }
    public string Description { get; set; }
}

A couple of things about your class.

1) You will need getters and setters on your properties (as others have pointed out)

2) C# best practice is to capitalize the first letter of class properties.

3) You really don't want to use a property with the same name as the class name.

Service c = new Service
{
    Description = "desc",
    SomethingElse = "serv"
};
string x = JsonConvert.SerializeObject(c);

This code gives you this for x:

{"SomethingElse":"serv","Description":"desc"}

Note that when you create a new instance of a class and then immediately fill it up and assign values to the properties, it's best to use the Object Initializer approach.

So instead of this:

Service c = new Service();
c.description = "desc";
c.service = "serv";

It's best to do this:

Service c = new Service
{
    Description = "desc",
    SomethingElse = "serv"
};
Casey Crookston
  • 13,016
  • 24
  • 107
  • 193
0

this is a correct example:

using Newtonsoft.Json;
using System;
using Microsoft;

public class Program
{
    public static void Main()
    {
        Service c = new Service();
        c.description = "desc";
        c.service = "serv";
        string json = JsonConvert.SerializeObject(c, Formatting.Indented);
        Console.WriteLine(json);

    }

    public class Service
    {
        public Service() { }
        public string service;
        public string description;
    }


}
CoolLife
  • 1,419
  • 4
  • 17
  • 43
  • Newtonsoft.Json. I guess I needed to specify I have System.Text.Json. Didn't realize other libraries existed besides the default. – Alan Baljeu Jan 17 '20 at 16:59
  • @AlanBaljeu Several exists. `System.Text.Json` is actually the youngest of the bunch. –  Jan 17 '20 at 17:00
  • young = new and better? – Alan Baljeu Jan 17 '20 at 17:00
  • I still prefer Newtonsoft.Json. All CoolLife did here is switch from `JsonSerializer.Serialize` to `JsonConvert.SerializeObject`. I tested his code. It works. – Casey Crookston Jan 17 '20 at 17:01
  • @AlanBaljeu Newer anyway. The *original* .Net serializer was pretty slow and didn't have a lot of features, so Newtonsoft was made, which was *the* JSON serialization library for a long time. Microsoft learned from Newtonsoft and made the new API. I don't know if it's as feature-rich. Newtonsoft is a very powerful library, and hard to beat, in my opinion. –  Jan 17 '20 at 17:03
  • @AlanBaljeu Kentico and Unity have their own JSON serialization utilities, but they aren't general purpose like the others are, nor are they are robust. –  Jan 17 '20 at 17:05