olayk
Creating a Printer class for my own type.
I am doing this for learning and am aware that if I want to work with JSON I can use System.Text.Json
This is the type to extend:
And want to define a class like:
How should I extend the functionality of the JSON types. I had a ToString() method, but it didnt deal with indents and single responsibility principle.
50 replies
How to implement default indexing for my own JSONValue class
In another file I have a Parser class with Parser.parse() returning a JSONValue or throwing an error.
Q1) If I have an abstract class as a return value / value does it enforces that it returns an instance of a child of this class?
Q2) If I want to be able to index an unknown JSONValue (e.g. json['key1]['key2']) how should I do this? Would it work to have an abstract method for indexing, which returns an error on types such as JSONNumber?
Q3) How could I enforce a Val in the JSONValue class? I would like to have a default ToString where I return the Val ToString which I would override on the JSONArray and JSONObject types, but I am not sure what type the Val could even be.
Q4) Would it be a good idea to create my own error types for parsing, lexing and json indexing?
Many thanks for any help! I'd appreciate any input on the code shown here also as I am new to C#. I'm not sure if this is the best way to create a type, so I'd appreciate any comments on a better way to implement this.
14 replies