Coder Social home page Coder Social logo

hienduyph / protoc-gen-go-json Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mitchellh/protoc-gen-go-json

0.0 1.0 0.0 56 KB

Protobuf compiler plugin to generate Go JSON Marshal/Unmarshal implementations for messages using jsonpb.

License: MIT License

Go 77.40% Makefile 4.33% Nix 18.27%

protoc-gen-go-json's Introduction

protoc-gen-go-json

This is a plugin for the Google Protocol Buffers compiler protoc that generates code to implement json.Marshaler and json.Unmarshaler using protojson.

This enables Go-generated protobuf messages to be embedded directly within other structs and encoded with the standard JSON library, since the standard encoding/json library can't encode certain protobuf messages such as those that contain oneof fields.

Install

go get github.com/mitchellh/protoc-gen-go-json

Also required:

Usage

Define your messages like normal:

syntax = "proto3";

message Request {
  oneof kind {
    string name = 1;
    int32  code = 2;
  }
}

The example message purposely uses a oneof since this won't work by default with encoding/json. Next, generate the code:

protoc --go_out=. --go-json_out=. request.proto

Your output should contain a file request.pb.json.go which contains the implementation of json.Marshal/Unmarshal for all your message types. You can then encode your messages using standard encoding/json:

import "encoding/json"

// Marshal
bs, err := json.Marshal(&Request{
  Kind: &Kind_Name{
    Name: "alice",
  },
}

// Unmarshal
var result Request
json.Unmarshal(bs, &result)

Options

The generator supports options you can specify via the command-line:

  • enums_as_ints={bool} - Render enums as integers instead of strings.
  • emit_defaults={bool} - Render fields with zero values.
  • orig_name={bool} - Use original (.proto file) name for fields.
  • allow_unknown={bool} - Allow messages to contain unknown fields when unmarshaling

It also includes the "standard" options available to all protogen-based plugins:

  • import_path={path} - Override the import path
  • paths=source_relative - Derive the output path from the input path
  • etc.

These can be set as part of the --go-json_out value:

protoc --go-json_opt=emit_defaults=true:.

You can specify multiple using a ,:

protoc --go-json_out=enums_as_ints=true,emit_defaults=true:.

Alternatively, you may also specify options using the --go-json_opt value:

protoc --go-json_out:. --go-json_opt=emit_defaults=true,enums_as_ints=true

protoc-gen-go-json's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.