Giter VIP home page Giter VIP logo

shapeshifter's Introduction

Shapeshifter

Shapeshifter is a library that bridges the gap between Protocol Buffers and JSON, for the purpose of building robust APIs whose payloads are well-defined and documented.

Protocol Buffers provide an extremely compact, backward and forward-compatible serialization mechanism that is well-suited for machine to machine communication. On the other hand, JSON is the native data format for all parts of the modern web toolchain.

Shapeshifter lets you re-use messages defined as protobufs in your web service by exposing them as JSON objects. We hope you'll find Shapeshifter as useful as we have when developing our latest-generation web services. Either way, feel free to drop a line on the mailing list or file a bug on the tracker.

Maven coordinates

You may add the following to your pom.xml

<dependency>
	<groupId>com.turn</groupId>
	<artifactId>shapeshifter</artifactId>
	<version>1.1.0</version>
</dependency>

Usage

Assuming there exists a Protocol Buffer message defined as such:

message Person {
	optional string name = 1;
	optional int32 age = 2;
	optional string city = 3;
}

You may declare a schema

NamedSchema personSchema = NamedSchema.of(Person.getDescriptor(), "Person");

Serialization

Once a schema is defined, you may want to create an object for generating JSON:

Serializer personSerializer = personSchema.getSerializer();
Person biebs = Person.newBuilder().setName("Justin Bieber").build();
JsonNode node = personSerializer.serialize(biebs, SchemaRegistry.EMPTY);

The resulting node's notation will be:

{
	"name": "Justin Bieber",
}

Parsing

Conversely, schemas are able to validate and parse JSON content into Protocol Buffers messages:

Parser personParser = personSchema.getParser();
Message message = personParser.parse(node, SchemaRegistry.EMPTY)
Person parsedBiebs = Person.newBuilder().mergeFrom(message).build();

Configurability

Schemas are configurable โ€” you may want to transform values and names, avoid surfacing certain confidential fields and even create map-like JSON constructs. Read the full Javadoc to find out the available options.

JSON-Schema

Moreover, Shapeshifter supports the JSON-Schema specification and is able to derive the set of schemas from your Protocol Buffer messages. The library contains a message definition that represents a JSON-Schema and, as a proof of concept, Shapeshifter uses itself to define a schema compliant with the specification:

JsonSchema personJsonSchema = personSchema.getJsonSchema();
JsonNode node = JsonSchemas.SCHEMA.getSerializer().serialize(personJsonSchema);

Will yield:

{
	"id": "Person",
	"type": "object",
	"properties": {
		"name": {
			"type": "string"
		},
		"age": {
			"type": "number"
		},
		"city": {
			"type": "string"
		}
	}
}

License

Shapeshifter is distributed under the terms of the Apache Software License version 2.0. See LICENSE file for more details.

Authors and contributors

  • Julien Silland <[email protected]> (Software Engineer at Turn, Inc)
    Original author, main developer and maintainer

shapeshifter's People

Contributors

jc-turn avatar jsilland avatar

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.