Skip to content

Instantly share code, notes, and snippets.

@bjorg
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save bjorg/e3169f46580128c1ecfc to your computer and use it in GitHub Desktop.

Select an option

Save bjorg/e3169f46580128c1ecfc to your computer and use it in GitHub Desktop.
Concept for a JDoc implementation that is fluent and well-typed.
// ----------
// 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