C# Serialing a List to JON creates empty string

Paul Ryan 321 Reputation points
2022-05-24T15:04:52.36+00:00

I am missing something -- that I cannot figure out, when I serialize to text, it does pick up the text

Using VS 2022 C# 10 Net 6

Model Class
internal class Day30Model
{
internal string FileName { get; set; }
}

Initialization
List<Day30Model> day30List = new List<Day30Model>();

205135-image.png

string outFileText = JsonSerializer.Serialize<List<Day30Model>>(day30List);

OutFileText = [{},{},{},{}]

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,277 questions
0 comments No comments
{count} votes

Accepted answer
  1. Nico Zhu (Shanghai Wicresoft Co,.Ltd.) 12,856 Reputation points
    2022-05-25T05:50:42.273+00:00

    Hello,
    Welcome to Microsoft Q&A!

    Serialing a List to JON creates empty string

    The problem is FileName is internal getters/setters to prevent the user from accessing this functionality. If you want JsonConvert access to the internal functionality, you need add [JsonProperty] attribute for FileName proeprty

    internal class Day30Model  
    {  
        [JsonProperty]  
        internal string FileName { get; set; }  
    }  
    

    And then call newton json JsonConvert to serialize list items as string.

    var jsonString  =     JsonConvert.SerializeObject(day30List);  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Karen Payne MVP 35,036 Reputation points
    2022-05-24T16:23:26.623+00:00

    Try this public string FileName { get; set; }

    using System;
    using System.Collections.Generic;
    using System.IO;
    using Newtonsoft.Json;
    
    namespace Example1
    {
        partial class Program
        {
            static void Main(string[] args)
            {
                DataOperations.Create();
                var list = DataOperations.List();
                foreach (var model in list)
                {
                    Console.WriteLine(model.FileName);
                }
                Console.ReadLine();
            }
        }
        internal class Day30Model
        {
            public string FileName { get; set; }
        }
    
        class DataOperations
        {
            private static string _fileName = "Data.json";
            public static void Create()
            {
                List<Day30Model> list = new()
                {
                    new Day30Model() {FileName = "First"},
                    new Day30Model() {FileName = "Second"},
                    new Day30Model() {FileName = "Third"},
                };
    
                File.WriteAllText(_fileName, JsonConvert.SerializeObject(list));
            }
    
            public static List<Day30Model> List() 
                => JsonConvert.DeserializeObject<List<Day30Model>>(
                    File.ReadAllText(_fileName));
        }
    }
    
    1 person found this answer helpful.

  2. Karen Payne MVP 35,036 Reputation points
    2022-05-24T17:53:35.307+00:00

    Here is the json file

    [
      {},
      {},
      {}
    ]
    

    Using

    internal class Day30Model
    {
        internal string FileName { get; set; }
    }
    

    Results

    [
      {
        "FileName": "First"
      },
      {
        "FileName": "Second"
      },
      {
        "FileName": "Third"
      }
    ]
    

    Using

    internal class Day30Model
    {
        public string FileName { get; set; }
    }
    

    Project type plays no role with this issue.

    Full source
    https://github.com/karenpayneoregon/closet-code/tree/master/JsonNetAndNativeJsonDemo