0

I am having trouble in serializing an Object using Newton.Json, when I used breakpoints to debug, I found my serialized object is empty while the object provided to serializer is intact.

I have attached the Screen Shots. Object To Be Serialized Intact

Serialized Object Empty

EDIT : Here is the Class whose list I am trying to serialize.

class ArgumentGroup
{
    String argumentGroupName = "";
    String argumentDescription = "";
    int argumentCount = 0;
    List<Argument> argumentList = new List<Argument>();

    public ArgumentGroup(string argumentGroupName, string argumentDescription, int argumentCount, List<Argument> argumentList)
    {
        this.argumentGroupName = argumentGroupName;
        this.argumentDescription = argumentDescription;
        this.argumentCount = argumentCount;
        this.argumentList = argumentList;
    }
}
Reshma Suresh
  • 165
  • 1
  • 4
  • 13
  • 2
    Can you show us the class you are serializing? – EylM Oct 14 '19 at 11:04
  • @EylM I have updated with the code snippet. – Reshma Suresh Oct 14 '19 at 11:12
  • add this: `var obj = new ExampleClass(); var jsons = JsonConvert.SerializeObject(obj, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore });` – Mojtaba Nava Oct 14 '19 at 11:12
  • 4
    There are no public properties to serialize in this object – Panagiotis Kanavos Oct 14 '19 at 11:15
  • 4
    Use public properties, not fields. Fields are implementation details, only *properties* are part of the class's interface. Serializers (all of them) work with properties. They can be configured to work with fields as well, even private fields, but they *shouldn't* unless there's a very important reason – Panagiotis Kanavos Oct 14 '19 at 11:17
  • This - `String argumentGroupName = "";` is *private*, and thus won't be serialized. Make it `public` and it should work. – Lasse V. Karlsen Oct 14 '19 at 11:41

2 Answers2

1

Make your fields public, because default NewtonSoft.Json will only serialize public members or for some reason you really don't want to make your fields public, you can use the JsonPropertyAttribute to allow them to be serialize and deserialize.

0

Does JsonProperty have each class property?

[JsonProperty("argumentGroupName")]
public string argumentGroupName { get; set; }

and import:

using System;
using System.Collections.Generic;

using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
Ahmed Msaouri
  • 316
  • 1
  • 10