diff --git a/README.md b/README.md index 43fcf14..e7ecaf4 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ Example: ```crystal require "protobuf" +require "tempfile" enum Foo FOO @@ -56,15 +57,23 @@ struct MyMessage # write your methods like you normally would here, if you like. end -proto_io = File.read("path/to/encoded/protobuf") # get your IO in some way +# MyMessage is just a normal struct - let's create one +msg = MyMessage.new prop_name: 42, prop2: Foo::FOO, optional_prop_name: "Bar" +puts "Before serialization: #{msg}" -msg = MyMessage.from_protobuf(proto_io) # returns a an instance of MyMessage - # from a valid protobuf encoded message +io_memory = msg.to_protobuf # io_memory is an IO::Memory of the encoded message -msg.to_protobuf # return a IO::Memory filled with the encoded message +# Or write the encoded message into any IO object - such as network, or a file +tmpfile = Tempfile.open("my_message") do |output_file| + msg.to_protobuf output_file # In this case we use a temporary file +end + +# Now let's decode the message... +input_file = File.open(tmpfile.path) # open an IO object (the file we just wrote) +decoded_msg = MyMessage.from_protobuf(input_file) # return an instance of MyMessage -some_io = IO::Memory.new -msg.to_protobuf(some_io) # fills up the provided IO with the encoded message +puts "After serialization: #{decoded_msg}" +tmpfile.delete # clean up the temporary file ``` #### Field types