Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hey I'm making a saving system that saves my 20 client's progress in a json file but the progresses are stored in a list and I'm trying to save them based on the client number
C#
public class User
{
    [JsonProperty("username")]
    public string Username { get; set; }

    [JsonProperty("client-progresses")]
    public int[] ClientProgresses { get; set; }
}

C#
public static void SaveProgress(int progress, int clientNumber)
{
    var user = Program.User;

    user.ClientProgresses[clientNumber] = progress;

    var json = JsonConvert.SerializeObject(user, Formatting.Indented);
    var dir = $"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Saves")}";

    if (!Directory.Exists(dir))
    {
        Directory.CreateDirectory(dir);
    }

    dir = Path.Combine(dir, $"{user.Username}.json");

    lock (_fileLocker)
    {
        Task.Delay(100);
        File.WriteAllText(dir, json, new UTF8Encoding(false));
    }
}

The problem is that the list is empty so I can't do
user.ClientProgresses[clientNumber] = progress;

so any ideas how I would set it?

What I have tried:

<pre lang="c#">
    public class User
    {
        [JsonProperty("username")]
        public string Username { get; set; }

        [JsonProperty("client-progresses")]
        public int[] ClientProgresses { get; set; }
    }

C#
public static void SaveProgress(int progress, int clientNumber)
{
    var user = Program.User;

    user.ClientProgresses[clientNumber] = progress;

    var json = JsonConvert.SerializeObject(user, Formatting.Indented);
    var dir = $"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Saves")}";

    if (!Directory.Exists(dir))
    {
        Directory.CreateDirectory(dir);
    }

    dir = Path.Combine(dir, $"{user.Username}.json");

    lock (_fileLocker)
    {
        Task.Delay(100);
        File.WriteAllText(dir, json, new UTF8Encoding(false));
    }
}
Posted
Updated 16-May-20 5:51am
v3
Comments
Richard Deeming 18-May-20 12:01pm    
Removing the content of your question after it has been answered is extremely rude.

Try this, and you will see that the value is present:
//using Newtonsoft.Json;
//using System.IO;

var user = new User();
user.Username = "Test1";
user.ClientProgresses = new int[10];
user.ClientProgresses[1] = 1;

var json = JsonConvert.SerializeObject(user, Formatting.Indented);
File.WriteAllText("test.json", json, new UTF8Encoding(false));
 
Share this answer
 
v2
The most important thing you need to do is prepopulate your string array with values. You can do this with the constructor to your class, like so:

public class User
{
    [JsonProperty("username")]
    public string Username { get; set; }

    [JsonProperty("client-progresses")]
    public string[] ClientProgresses { get; set; }

    public User(int customerCount, string defaultState)
    {
        List<string> clientProgresses = new List<string>();

        for (int i = 0; i < customerCount; i++)
        {
            clientProgresses.Add(defaultState);
        }
        
        ClientProgresses = clientProgresses.ToArray();
    }
}


This way, when you construct your user, you pass in both the number of clients you want to initialize, and the default progress state for those clients, like so:

var user = new User(20, "init");


So that when you call your SaveProgress routine, you can test to see if the client exists before you update the value in the array, like so:

if (clientNumber >= user.ClientProgresses.Length)
{
    throw new Exception($"Client number '{clientNumber}' is outside the range of the user client progress array '{user.ClientProgresses.Length}'");
}
 
Share this answer
 
Use a dictionary instead of a list or an array:
C#
public class User
{
    [JsonProperty("username")]
    public string Username { get; set; }

    [JsonProperty("client-progresses")]
    public Dictionary<int, int> ClientProgresses { get; set; } = new Dictionary<int, int>();
}
The JSON will look something like:
JavaScript
{
    "username": "...",
    "client-progresses": {
        "42": 10,
        "64": 90
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900