question

PaulRyan-4569 avatar image
0 Votes"
PaulRyan-4569 asked PaulRyan-4569 commented

C# Serialing a List to JON creates empty string

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 = [{},{},{},{}]

dotnet-csharpwindows-uwp
image.png (22.9 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

NicoZhu-MSFT avatar image
0 Votes"
NicoZhu-MSFT answered PaulRyan-4569 commented

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.






· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Adding [JsonProperty] and using Newtonsoft worked


thank you

0 Votes 0 ·
karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered PaulRyan-4569 commented

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));
     }
 }
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you for the quick reply, however using Newtonsoft gave the same result

string outFileText = JsonConvert.SerializeObject(day30List);

[{},{},{},{}]

FYI -- this is UWP

0 Votes 0 ·

Both Jsons' worked using CLI
System.Text.Joson and Newtonsoft with Net 6 under VS 2022 Version 17.2.1

c# forgot how to find version - whatever the default is

0 Votes 0 ·
karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered PaulRyan-4569 commented

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

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks, for some reason the serialization works in the CLI, but not in UWP. As the list is small I just listed each in the class and used a switch /case, I am in a time crunch.

I thought I was doing something stupid/wrong

again thanks for the help

Paul




0 Votes 0 ·

@PaulRyan-4569. I want to know what is the CLI and the UWP you mentioned. Could you tell me the full name for them?

0 Votes 0 ·

Console App -- 205208-image.png


Universal Windows
205229-image.png


0 Votes 0 ·
image.png (8.8 KiB)
image.png (11.1 KiB)