Created
March 19, 2017 20:18
-
-
Save noctarius/7bc20b2f7560406090110af8457f1656 to your computer and use it in GitHub Desktop.
This file contains 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
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