11// Example demonstrating StreamParser with a Reader over a fixed-size array
22
3- use picojson:: { Event , ParseError , PullParser , Reader , StreamParser } ;
4-
5- /// Simple Reader implementation that reads from a fixed-size byte array
6- /// This simulates reading from a stream, network socket, or any other byte source
7- struct ArrayReader < ' a > {
8- data : & ' a [ u8 ] ,
9- position : usize ,
10- chunk_size : usize , // Simulate streaming by reading in chunks
11- }
12-
13- impl < ' a > ArrayReader < ' a > {
14- /// Create a new ArrayReader from a byte slice
15- /// chunk_size controls how many bytes are read at once (simulates network packets)
16- fn new ( data : & ' a [ u8 ] , chunk_size : usize ) -> Self {
17- Self {
18- data,
19- position : 0 ,
20- chunk_size,
21- }
22- }
23- }
24-
25- impl < ' a > Reader for ArrayReader < ' a > {
26- type Error = std:: io:: Error ;
27-
28- fn read ( & mut self , buf : & mut [ u8 ] ) -> Result < usize , Self :: Error > {
29- let remaining = self . data . len ( ) . saturating_sub ( self . position ) ;
30- if remaining == 0 {
31- return Ok ( 0 ) ; // EOF
32- }
33-
34- // Read at most chunk_size bytes to simulate streaming behavior
35- let to_read = remaining. min ( buf. len ( ) ) . min ( self . chunk_size ) ;
36- let end_pos = self . position + to_read;
37-
38- buf[ ..to_read] . copy_from_slice ( & self . data [ self . position ..end_pos] ) ;
39- self . position = end_pos;
40-
41- println ! (
42- " π Reader: read {} bytes (pos: {}/{})" ,
43- to_read,
44- self . position,
45- self . data. len( )
46- ) ;
47- Ok ( to_read)
48- }
49- }
3+ use picojson:: { ChunkReader , Event , ParseError , PullParser , StreamParser } ;
504
515fn main ( ) -> Result < ( ) , ParseError > {
52- println ! ( "π StreamParser Demo with ArrayReader " ) ;
6+ println ! ( "π StreamParser Demo with ChunkReader " ) ;
537 println ! ( "=====================================" ) ;
548
559 // Test JSON with various data types including escape sequences
@@ -59,15 +13,15 @@ fn main() -> Result<(), ParseError> {
5913 println ! ( "π Total size: {} bytes" , json. len( ) ) ;
6014 println ! ( ) ;
6115
62- // Create ArrayReader that reads in small chunks (simulates network streaming)
63- let reader = ArrayReader :: new ( json, 8 ) ; // Read 8 bytes at a time
16+ // Create ChunkReader that reads in small chunks (simulates network streaming)
17+ let reader = ChunkReader :: new ( json, 8 ) ; // Read 8 bytes at a time
6418
6519 // Create StreamParser with a reasonably sized buffer
6620 let mut buffer = [ 0u8 ; 256 ] ;
6721 let buffer_size = buffer. len ( ) ;
6822 let mut parser = StreamParser :: new ( reader, & mut buffer) ;
6923
70- println ! ( "π Starting StreamParser with streaming ArrayReader :" ) ;
24+ println ! ( "π Starting StreamParser with streaming ChunkReader :" ) ;
7125 println ! ( " Buffer size: {} bytes" , buffer_size) ;
7226 println ! ( " Chunk size: 8 bytes (simulates small network packets)" ) ;
7327 println ! ( ) ;
0 commit comments