Coder Social home page Coder Social logo

burtbeckwith / grails-json-api-variants Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gregopet/grails-json-apis

0.0 2.0 0.0 272 KB

Grails plugin for managing multiple JSON api variants

License: Apache License 2.0

Groovy 87.13% HTML 12.87%

grails-json-api-variants's Introduction

json-apis

Grails plugin for managing multiple JSON apis using domain class annotations

The goal of this plugin is to help convert Grails domain classes into various JSON representations needed in different parts of your web application or to support various API versions.

Features:

  • Allows you to declare multiple named JSON configurations for different use cases
  • Configuration is very straightforward: all that is required is to mark domain class properties with a single annotation naming the configurations under which that property should be included in the serialized JSON object
  • Works for collections as well as belongsTo properties
  • Enables developers to avoid the circular object reference problem elegantly by defining appopriate namespaces - this way it is possible to start the serialization either in a parent or child entity, depending on the use case
  • Works for objects serialized inside a JSON builder
  • Uses the Grails' ObjectMarshaller mechanism under the hood
  • Detects API changes during development time live reloads

Example of use

Several API variants can be easily defined in domain classes by annotating properties with JsonApi and providing a list of API profile names under which that property should appear in the resulting JSON. Marking a property with the JsonApi annotation but providing no API names will include that property in all APIs. The database identity property will always be included automatically. One could for instance define the following domain class:

import grails.plugins.jsonapis.JsonApi

class User {
	@JsonApi
	String screenName

	@JsonApi('userSettings')
	String email

	@JsonApi(['userSettings', 'detailedInformation'])
	String twitterUsername
}

Then in the controller one would call the desired named JsonApi configuration to get only the fields defined for that API. The following code:

JSON.use("detailedInformation")
render person as JSON

...would convert the person object into JSON containing the id, screenName and twitterUsername properties but not the email. It works for collections as well, converting each collection member using the same API profile that was used to convert the parent:

static hasMany = [
	pets: Pet
]
@JsonApi('detailedInformation')
Set pets

To include a domain object's parent in a JSON API, declare a belongsTo property explicitly and annotate it with JsonApi (but be careful not to create circular paths by including both ends of a belongsTo/hasMany pair):

static belongsTo = [
	user:User
]

@JsonApi('petDetails') 
User user

JSONBuilder is supported, too:

JSON.use("userSettings")
render(contentType: "text/json") {
    user = User.first()
    pet = Pet.first()
}

Future plans

  • Detect circular APIs and display a warning on startup, perhaps disable them entirely
  • Add a script/controller that would document the registered APIs in one or more formats
  • Read the domain class annotations and produce configurations for those 3rd party JSON renderers which currently seem to perform better than the native Grails implementation

grails-json-api-variants's People

Contributors

gregopet avatar burtbeckwith avatar

Watchers

 avatar  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.