0

I am making a ToDoList-Console app that needs serialisation of multiple objects. There are 2 task classes:

  • Simple task
  • Complex task

I need a method that serialises and desirealize these tasks, something like WriteAndReadComplexTask, for both the classes that does not rewrite the whole tasks.json file, but adds it up. This is what I am aiming for as an end result:

[
 {
    "SimpleTask": "do the dishes"
 }
 {
    "ComplexTask": "buy groceries"
      { "subtask": "bananas",
        "subtask": "apples"}
 }
 {
    "simple task": "do some coding"
 }
]
6
  • 1
    That's not valid JSON. Each property name should be surrounded by double quotes, as well as each text value. Commented Nov 22, 2021 at 6:44
  • thanks @jason.kaisersmith , I am new to json so I didnt knew Commented Nov 22, 2021 at 6:50
  • To be blunt, if you decide on using a text file for your data storage then yes, you can write code to handle partial rewrites but it's going to be very hard to do. If you need to be able to do random access updates to your storage, don't use JSON or other text file formats, use a database like sqlite. Commented Nov 22, 2021 at 8:02
  • Do you need to use text based serialization? I know protobuf .net can use SerializeWithLengthPrefix to append arbitrary number of messages to a stream. Commented Nov 22, 2021 at 9:37
  • i don't need to, but since im a beginner and i did not understand anything you said I think i should stick to the easier stuff. Sorry @JonasH Commented Nov 22, 2021 at 9:39

1 Answer 1

1

This isn't too hard to do. But I think you are approaching the problem from a different angle I suggest you try getting this to work with just 1 class which contains all fields already and using 1 todoitem task. This saves time and complex logic because otherwise you might need to convert items from simple tasks to complex tasks in the future.

  1. First of all, your JSON is still not correct. you are missing comma's after each object.

  2. I suggest changing your JSON to the following:

[
     {
        "name": "SimpleTask"
        "description": "do the dishes"
     },
     {
        "name": "Simple task",
        "description": "buy groceries",
         "subtasks":[ 
             {"name": "Simple task",
        "description": "buy groceries"},{.... repeat another item}]
     },
     {
         ..another item here 
     }
]
  1. Create a new class where which looks like the object in your JSON like this:
public class TodoItem 
{
    public string name {get;set;}
    public string description {get;set;}
    public List<TodoItem> subtasks {get;set;}
}
  1. Now in your code load the file and use the following to load to C#
var todoitems = JsonSerializer.Deserialize<List<TodoItem>>(jsonString);
Sign up to request clarification or add additional context in comments.

2 Comments

okay so, after much trial and error i got it working, kind of. Im a beginner so correct me if im wrong but i think the class ToDoItem needs a Public Override string ToString(), so when I deserialise them i can print out strings. Also, how would you go about the subtasks list? i have troubles printing them out. I tried implementing Strin.Join directly into the return statement of ToString(). It manages to serialise and deserialise it but it cannot print it with a foreach loop
@Rarowcun You don't need the override but it's one way of doing things, no problem. You could also create a new function which allows a ToDoItem and print inside the function. You can Print it for example with Console.Writeline($"name: {ToDoItem.name} description: {ToDoItem.description}") and now you can add an foreach(var subtask in ToDoItem.subtasks) { ToDoItem.ToString() }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.