Skip to content

Instantly share code, notes, and snippets.

@keleshev
Last active March 25, 2016 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keleshev/eb89a14b8a1bc4a89cc8 to your computer and use it in GitHub Desktop.
Save keleshev/eb89a14b8a1bc4a89cc8 to your computer and use it in GitHub Desktop.

Brainstorming a better Quark API for handling JSON…

namespace json {
  interface JSON {
    String render();
  }

  class Null implements JSON {
    String render() {}
  }

  class Boolean implements JSON {
    Boolean(builtin.Boolean boolean) {}
    String render() {}
  }

  class Number implements JSON {
    Number(builtin.Number number) {}
    String render() {}
  }

  class String implements JSON {
    String(builtin.String string) {}
    String render() {}
  }

  class Array implements JSON {
    @delegate('size')
    private List<JSON> list;

    Array(List<JSON> list) {}
    String render() {}

    Optional<JSON> get(Number index) {}
    List<JSON> toList() {}
  }

  class Object implements JSON {
    @delegate('keys', 'values', 'size')
    private Map<String,JSON>;

    Object(Map<String,JSON> map) {}
    String render() {}

    Optional<JSON> get(String key) {}
    Map<String,JSON> toMap() {}
  }

  Optional<JSON> parse(builtin.String source) {}
}
@rhs
Copy link

rhs commented Mar 22, 2016

What does 'JSON?' mean?

@keleshev
Copy link
Author

@rhs I updated the gist not to get distracted with this unrelated feature I had in mind. But basically T? is a shortcut for Optional<T> a type for explicitly handling "nullability". Swift and Kotlin use the T? syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment