Skip to content

Commit c08504d

Browse files
committed
Binary data stringify/prettify
1 parent a31b691 commit c08504d

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

include/tao/json/events/jaxn/to_pretty_stream.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,16 @@ namespace tao
5656
after_key = true;
5757
}
5858

59-
// TODO: Binary strings!
59+
void binary( const std::vector< byte >& v )
60+
{
61+
static const char h[] = "0123456789ABCDEF";
62+
next();
63+
os.put( '$' );
64+
for( const auto b : v ) {
65+
os.put( h[ static_cast< unsigned char >( b ) >> 4 ] );
66+
os.put( h[ static_cast< unsigned char >( b ) & 0xF ] );
67+
}
68+
}
6069
};
6170

6271
} // namespace jaxn

include/tao/json/events/jaxn/to_stream.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,16 @@ namespace tao
5555
first = true;
5656
}
5757

58-
// TODO: Binary strings!
58+
void binary( const std::vector< byte >& v )
59+
{
60+
static const char h[] = "0123456789ABCDEF";
61+
next();
62+
os.put( '$' );
63+
for( const auto b : v ) {
64+
os.put( h[ static_cast< unsigned char >( b ) >> 4 ] );
65+
os.put( h[ static_cast< unsigned char >( b ) & 0xF ] );
66+
}
67+
}
5968
};
6069

6170
} // namespace jaxn

src/test/json/ostream_jaxn.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ namespace tao
4545
test_simple( INFINITY, "Infinity" );
4646
test_simple( -INFINITY, "-Infinity" );
4747
test_simple( "foo", "\"foo\"" );
48+
test_simple( std::vector< byte >( { byte( 1 ), byte( 2 ), byte( 3 ) } ), "$010203" );
4849
test_simple( empty_array, "[]" );
4950
test_simple( value::array( {} ), "[]" );
5051
test_simple( value::array( { 1 } ), "[1]" );
@@ -65,6 +66,7 @@ namespace tao
6566
test_pretty( 42, "42" );
6667
test_pretty( 42.1, "42.1" );
6768
test_pretty( "foo", "\"foo\"" );
69+
test_pretty( std::vector< byte >( { byte( 1 ), byte( 2 ), byte( 3 ) } ), "$010203" );
6870
test_pretty( empty_array, "[]" );
6971
test_pretty( value::array( {} ), "[]" );
7072
test_pretty( value::array( { 1 } ), "[\n 1\n]" );

0 commit comments

Comments
 (0)