-3

I feel like a dingdong not being able to figure this out, but hoping someone can either help me or point me in the right direction.

I'm currently converting individual files for a game i'm working on into a JSON Object. Currently, the structure of the song is as follows:

        public readonly int Id = 1;

        public readonly int BPM = 120;
        public readonly string Name = "Baby's";

        public readonly string Artist = "Brian";

        protected Song SongFile;

        public Queue<NoteChart> Chart = new Queue<NoteChart>();

        Chart.Enqueue(new NoteChart { gemType = 1, Key = "D", Lane = 8, beatTime = new TimeSpan(0, 0, 0, 5, 84) });
 

Pretty standard stuff I presume. Anyways, i'm having a iddue turning that Chart.Enqueue into a JSON object so I can parse it that way instead of having it typed out. So far I have:

  "Id": 1,
  "BPM": 120,
  "Name": "Baby",
  "Artist": "Brian",
  "SongFile": "Songs/Baby",
  "Chart": {
  }

But as you can see, i'm stuck on the Chart portion. How would I succesfully map it into JSON to be converted into C#? Or is there a better way of doing this?

Thank you!

sylargaf
  • 346
  • 1
  • 6
  • 19
  • 1
    Visual Studio can "paste JSON as classes", also look at System.Text.Json to deserialize it – aepot Nov 25 '22 at 00:42
  • Thanks aepot, I understand that part and I apologize if my question wasn't worded clearly; I was more wondering how to turn the Chart.Enque line in the top code into the JSON equivalent. Thank you though! – sylargaf Nov 25 '22 at 00:43
  • `string json = JsonSerializer.Serialize(Chart)` https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/how-to – aepot Nov 25 '22 at 00:45
  • Is there anyway to skip the step of creating it in C# first and creating it all within JSON instead? – sylargaf Nov 25 '22 at 00:55
  • 1
    Sure, implement manually pattern "State Machine" with `Utf8JsonReader`/`*Writer`. Btw, i can't figure out how you can do it in C# without C#. – aepot Nov 25 '22 at 00:57

1 Answers1

1

I can not find any problem

var chartItem = new ChartItem();

var json = JsonConvert.SerializeObject(chartItem, Newtonsoft.Json.Formatting.Indented);

public class ChartItem
{
    public readonly int Id = 1;

    public readonly int BPM = 120;
    public readonly string Name = "Baby's";

    public readonly string Artist = "Brian";

    protected string SongFile;

    public Queue<object> Chart =new ();
    
    public ChartItem ()
    {
        Chart.Enqueue(new  { gemType = 1, Key = "D", Lane = 8, beatTime = new TimeSpan(0, 0, 0, 5, 84) });
    }
    
}

json

{
  "Id": 1,
  "BPM": 120,
  "Name": "Baby's",
  "Artist": "Brian",
  "Chart": [
    {
      "gemType": 1,
      "Key": "D",
      "Lane": 8,
      "beatTime": "00:00:05.0840000"
    }
  ]
}
Serge
  • 40,935
  • 4
  • 18
  • 45