annotate json/base-n/README.md @ 66:6f160dee1192

Instead of using separate values and b64values entries in JSON serialisations, allow numeric arrays to be replaced by b64 variants wherever they appear (discriminating by type). Also rename values to featureValues in feature throughout, as values turns out to be a hazardous name in a JS context. Finally use Array instead of Text for array encoding (seems clearer).
author Chris Cannam <c.cannam@qmul.ac.uk>
date Tue, 27 Sep 2016 15:04:59 +0100
parents 6e8607ebad03
children
rev   line source
c@5 1 base-n provides encoding/decoding support for BaseX encoding schemes accessible through a standard STL-like iterator interface. Standard Base16, Base32, and Base64 are available out-of-the-box. Adding or modifying custom schemes is supported.
c@5 2
c@5 3 # Usage overview #
c@5 4
c@5 5 base-n is a small, single-header library which provides standard Base16, Base32, Base64, and custom Base-N encoding support.
c@5 6
c@5 7 The main functionality is delivered by the following functions in `bn` namespace:
c@5 8 ```
c@5 9 template<class Iter1, class Iter2>
c@5 10 void encode_b16(Iter1 start, Iter1 end, Iter2 out);
c@5 11
c@5 12
c@5 13 template<class Iter1, class Iter2>
c@5 14 void encode_b32(Iter1 start, Iter1 end, Iter2 out);
c@5 15
c@5 16
c@5 17 template<class Iter1, class Iter2>
c@5 18 void encode_b64(Iter1 start, Iter1 end, Iter2 out);
c@5 19
c@5 20
c@5 21 template<class Iter1, class Iter2>
c@5 22 void decode_b16(Iter1 start, Iter1 end, Iter2 out);
c@5 23
c@5 24
c@5 25 template<class Iter1, class Iter2>
c@5 26 void decode_b32(Iter1 start, Iter1 end, Iter2 out);
c@5 27
c@5 28
c@5 29 template<class Iter1, class Iter2>
c@5 30 void decode_b64(Iter1 start, Iter1 end, Iter2 out);
c@5 31 ```
c@5 32
c@5 33 In order to encode and decode data in `std::string` variable `in`, you can do the following:
c@5 34 ```
c@5 35 bn::encode_b64(in.begin(), in.end(), back_inserter(encoded));
c@5 36 bn::decode_b64(encoded.begin(), encoded.end(), ostream_iterator<char>(cout, ""));
c@5 37 ```
c@5 38
c@5 39 Should you find yourself lacking some encoding scheme or the default character mapping rules are not good for your use case, you can easily provide your own encoder. For that, you need to define a struct type which will describe the new encoding. Sample below:
c@5 40 ```
c@5 41 struct b8_custom
c@5 42 {
c@5 43 static size_t group_length()
c@5 44 {
c@5 45 return 3;
c@5 46 }
c@5 47
c@5 48 static char encode(int index)
c@5 49 {
c@5 50 const char* const dictionary = "01234567";
c@5 51 assert(index < strlen(dictionary));
c@5 52 return dictionary[index];
c@5 53 }
c@5 54
c@5 55 static char decode(char c)
c@5 56 {
c@5 57 if (c >= '0' && c <= '7') {
c@5 58 return c - '0';
c@5 59 }
c@5 60 return -1;
c@5 61 }
c@5 62 };
c@5 63 ...
c@5 64 string encoded;
c@5 65 bn::impl::encode<b8_custom>(in.begin(), in.end(), back_inserter(encoded));
c@5 66 bn::impl::decode<b8_custom>(encoded.begin(), encoded.end(), ostream_iterator<char>(cout, ""));
c@5 67 ```
c@5 68
c@5 69 For a full working example, see `basen_example.cpp` file in `example` directory.