Star me on GitHub

This page will explain how to encode a parser.

Currently katydid supports several encoders:

  • protobuf
  • json
  • reflected go structures
  • xml

This assumes you have already constructed a parser, if not see the page on parser usage.

Using an Encoder

Encoding json and xml is quite easy.

import ""
import ""
import ""

func encodeToJson(p parser.Interface) ([]byte, error) {
    return json.Encode(p)

func encodeToXml(p parser.Interface) ([]byte, error) {
    return xml.Encode(p)

Encoding a reflect go structure requires the structure into which will be encoded, or rather unmarshaled.

import ""
import reflectencoder ""
import "reflect"

func unmarshal(p parser.Interface, a *A) error {
    v := reflect.ValueOf(a)
    return reflectencoder.Encode(p, v)

Finally encoding into a protocol buffer (marshaling/dynamic transcoding) is a little bit more involved.

import ""
import ""
import ""

func encodeToProto(p parser.Interafce, fileDescriptorSet *descriptor.FileDescriptorSet, packageName, messageName string) ([]byte, error) {
    enc, err := proto.NewEncoder(fileDescriptorSet, packageName, messageName)
    if err != nil {
        return nil, err
    return enc.Encode(make([]byte, 1024*1024), p)

Dynamic encoding means that the protocol buffer does not necessarily need to be compiled in the program. Parsing the .proto files with protoc can give you a fileDescriptorSet which specifies the protocol buffer message. The packageName and messageName are used to locate the message in the fileDescriptorSet. The fileDescriptorSet can also be generated by gogoprotobuf as a method on the message.

The encoder can be reused and providing a large enough buffer to encode means that the buffer can also be reused.