Skip to content

Instantly share code, notes, and snippets.

@noctarius
Created March 19, 2017 20:18
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 noctarius/7bc20b2f7560406090110af8457f1656 to your computer and use it in GitHub Desktop.
Save noctarius/7bc20b2f7560406090110af8457f1656 to your computer and use it in GitHub Desktop.
Parser parser = Parser.newBuilder().build();
// extracts id into mid, internalDate into date, payload/headers into headers, returns the new object
// Query query = "(mid: #{'id'}, date: #{'internalDate'}, headers: #{'payload'}{'headers'})";
// alternative version to define it
Query query = parser.newQueryBuilder().asDictionary()
.putEntry("mid").stream(0).dictionary("id").endEntry()
.putEntry("date").stream(0).dictionary("internalDate").endEntry()
.putEntry("headers").stream(0).dictionary("payload").dictionary("headers").endEntry()
.endDictionary().build();
Input input = Input.fromByteArray(...);
Value value = parser.read(input, query);
Dictionary dictionary = value.dictionary();
Value idValue = dictionary.get("mid");
long id = idValue.number().longValue(); // Java Number type
Value dateValue = dictionary.get("date");
Instant date = dateValue.tag(); // Date extension type in CBOR
Value headersValue = dictionary.get("headers");
Dictionary headers = headersValue.dictionary(); // Just another directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment