# Parsing ProtoBuf Text Format

**URL:** https://discourse.julialang.org/t/parsing-protobuf-text-format/84721
**Category:** General Usage
**Tags:** question, io, serialization, protobuf
**Created:** [July 24, 2022, 6:24pm UTC](https://discourse.julialang.org/t/parsing-protobuf-text-format/84721 "2022-07-24T18:24:13Z")
**Posts on this page:** 1
**Showing post:** 4

<div class="post-metadata">

### Author: ![FireCrumb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/firecrumb/32/35370_2.png) [@FireCrumb](https://discourse.julialang.org/u/FireCrumb)
#### Post date: [November 9, 2022, 8:59am UTC](https://discourse.julialang.org/t/parsing-protobuf-text-format/84721/4 "2022-11-09T08:59:17Z")

</div>

Updated code for [ProtoBuf.jl 1.0.0](https://discourse.julialang.org/t/ann-protobuf-jl-1-0-0/85885/11)

```julia
# important: .proto files are required in runtime!

const _proj_path = normpath(joinpath(@ __FILE__ , "..", ".."))
const _proto_path = "some_path"

const ENV_ = copy(ENV)
ENV_["JULIA"] = joinpath(Sys.BINDIR, Base.julia_exename())

function protobuf_from_text(
    input::String,
    proto_type::Type{T},
    proto_file = "my.proto"
) where {T}
    proto_type_name = string(Base.typename(proto_type).wrapper)
    args = `--proto_path=$_proto_path --encode=namespace.$proto_type_name $proto_file`
    protoc_jll.protoc() do protoc_path
        inbuf = PipeBuffer()
        write(inbuf, input)
        outbuf = PipeBuffer()
        run(pipeline(setenv(`$protoc_path $args`, ENV_); stdin = inbuf, stdout = outbuf))
        d = ProtoDecoder(outbuf)
        return decode(d, proto_type)::T
    end
end

function protobuf_to_text(input::T, proto_file = "my.proto") where {T}
    proto_type = typeof(input)
    proto_type_name = string(Base.typename(proto_type).wrapper)
    args = `--proto_path=$_proto_path --decode=namespace.$proto_type_name $proto_file`
    protoc_jll.protoc() do protoc_path
        inbuf = PipeBuffer()
        e = ProtoEncoder(inbuf)
        encode(e, input)
        outbuf = PipeBuffer()
        run(pipeline(setenv(`$protoc_path $args`, ENV_); stdin = inbuf, stdout = outbuf))
        return read(outbuf, String)
    end
end

```

---

_[View the full topic](https://discourse.julialang.org/t/parsing-protobuf-text-format/84721)._
