-
-
Save bjorg/e3169f46580128c1ecfc to your computer and use it in GitHub Desktop.
Concept for a JDoc implementation that is fluent and well-typed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ---------- | |
| // Definition | |
| // ---------- | |
| class JDoc { | |
| //--- Types --- | |
| class ArrayBuilder<TOuter> { | |
| //--- Methods --- | |
| ArrayBuilder<TOuter> Null(); | |
| ArrayBuilder<TOuter> Value(bool value); | |
| ArrayBuilder<TOuter> Value(string value); | |
| ArrayBuilder<TOuter> Value(int value); | |
| ArrayBuilder<TOuter> Value(double value); | |
| ArrayBuilder<ArrayBuilder<TOuter>> Array(); | |
| ObjectBuilder<ArrayBuilder<TOuter>> Object(); | |
| TOuter End(); | |
| } | |
| class ObjectBuilder<TOuter> { | |
| //--- Methods --- | |
| ObjectKeyBuilder<TOuter> Key(string); | |
| TOuter End(); | |
| } | |
| class ObjectKeyBuilder<TOuter> { | |
| //--- Methods --- | |
| ObjectBuilder<TOuter> Null(); | |
| ObjectBuilder<TOuter> Value(bool value); | |
| ObjectBuilder<TOuter> Value(string value); | |
| ObjectBuilder<TOuter> Value(int value); | |
| ObjectBuilder<TOuter> Value(double value); | |
| ArrayBuilder<Object<TOuter>> Array(); | |
| ObjectBuilder<ObjectBuilder<TOuter>> Object(); | |
| } | |
| //--- Class Methods --- | |
| static JDoc Null(); | |
| static JDoc Value(bool value); | |
| static JDoc Value(string value); | |
| static JDoc Value(int value); | |
| static JDoc Value(double value); | |
| static ArrayBuilder<JDoc> Array(); | |
| static ObjectBuilder<JDoc> Object(); | |
| //--- Methods --- | |
| // TBD | |
| } | |
| // -------- | |
| // Examples | |
| // -------- | |
| // { "a": 123, "b": true, "c": [123.456, "hi"], "d": { "e": null }} | |
| JDoc.Object() | |
| .Key("a").Value(123) | |
| .Key("b").Value(true) | |
| .Key("c").Array() | |
| .Value(123.456) | |
| .Value("hi") | |
| .End() | |
| .Key("d").Object() | |
| .Key("e").Null() | |
| .End() | |
| .End(); | |
| // { } | |
| JDco.Object().End(); | |
| // [123, "hi"] | |
| JDoc.Array().Value(123).Value("hi").End(); | |
| // [ ] | |
| JDoc.Array().End(); | |
| // 123 | |
| JDoc.Value(123); | |
| // null | |
| JDoc.Null(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment